Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ from diffusers import QwenImageEditPipeline
|
|
| 10 |
import os
|
| 11 |
import base64
|
| 12 |
import json
|
|
|
|
| 13 |
|
| 14 |
SYSTEM_PROMPT = '''
|
| 15 |
# Edit Instruction Rewriter
|
|
@@ -71,12 +72,64 @@ Please strictly follow the rewriting rules below:
|
|
| 71 |
}
|
| 72 |
'''
|
| 73 |
|
| 74 |
-
def polish_prompt(
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
success=False
|
| 77 |
while not success:
|
| 78 |
try:
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
| 80 |
# print(f"Result: {result}")
|
| 81 |
# print(f"Polished Prompt: {polished_prompt}")
|
| 82 |
if isinstance(result, str):
|
|
@@ -91,8 +144,9 @@ def polish_prompt(prompt, img):
|
|
| 91 |
polished_prompt = polished_prompt.replace("\n", " ")
|
| 92 |
success = True
|
| 93 |
except Exception as e:
|
| 94 |
-
print(f"
|
| 95 |
-
|
|
|
|
| 96 |
|
| 97 |
|
| 98 |
def encode_image(pil_image):
|
|
|
|
| 10 |
import os
|
| 11 |
import base64
|
| 12 |
import json
|
| 13 |
+
from huggingface_hub import InferenceClient
|
| 14 |
|
| 15 |
SYSTEM_PROMPT = '''
|
| 16 |
# Edit Instruction Rewriter
|
|
|
|
| 72 |
}
|
| 73 |
'''
|
| 74 |
|
| 75 |
+
# def polish_prompt(original_prompt, system_prompt):
|
| 76 |
+
# """
|
| 77 |
+
# Rewrites the prompt using a Hugging Face InferenceClient.
|
| 78 |
+
# """
|
| 79 |
+
# # Ensure HF_TOKEN is set
|
| 80 |
+
# api_key = os.environ.get("HF_TOKEN")
|
| 81 |
+
# if not api_key:
|
| 82 |
+
# raise EnvironmentError("HF_TOKEN is not set. Please set it in your environment.")
|
| 83 |
+
|
| 84 |
+
# # Initialize the client
|
| 85 |
+
# client = InferenceClient(
|
| 86 |
+
# provider="cerebras",
|
| 87 |
+
# api_key=api_key,
|
| 88 |
+
# )
|
| 89 |
+
|
| 90 |
+
# # Format the messages for the chat completions API
|
| 91 |
+
# messages = [
|
| 92 |
+
# {"role": "system", "content": system_prompt},
|
| 93 |
+
# {"role": "user", "content": original_prompt}
|
| 94 |
+
# ]
|
| 95 |
+
|
| 96 |
+
# try:
|
| 97 |
+
# # Call the API
|
| 98 |
+
# completion = client.chat.completions.create(
|
| 99 |
+
# model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 100 |
+
# messages=messages,
|
| 101 |
+
# )
|
| 102 |
+
# polished_prompt = completion.choices[0].message.content
|
| 103 |
+
# polished_prompt = polished_prompt.strip().replace("\n", " ")
|
| 104 |
+
# return polished_prompt
|
| 105 |
+
# except Exception as e:
|
| 106 |
+
# print(f"Error during API call to Hugging Face: {e}")
|
| 107 |
+
# # Fallback to original prompt if enhancement fails
|
| 108 |
+
# return original_prompt
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def polish_prompt(prompt, system_prompt):
|
| 112 |
+
"""
|
| 113 |
+
Rewrites the prompt using a Hugging Face InferenceClient.
|
| 114 |
+
"""
|
| 115 |
+
# Ensure HF_TOKEN is set
|
| 116 |
+
api_key = os.environ.get("HF_TOKEN")
|
| 117 |
+
if not api_key:
|
| 118 |
+
raise EnvironmentError("HF_TOKEN is not set. Please set it in your environment.")
|
| 119 |
+
# Initialize the client
|
| 120 |
+
client = InferenceClient(
|
| 121 |
+
provider="cerebras",
|
| 122 |
+
api_key=api_key,
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
prompt = f"{system_prompt}\n\nUser Input: {prompt}\n\nRewritten Prompt:"
|
| 126 |
success=False
|
| 127 |
while not success:
|
| 128 |
try:
|
| 129 |
+
completion = client.chat.completions.create(
|
| 130 |
+
model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 131 |
+
messages=messages,
|
| 132 |
+
)
|
| 133 |
# print(f"Result: {result}")
|
| 134 |
# print(f"Polished Prompt: {polished_prompt}")
|
| 135 |
if isinstance(result, str):
|
|
|
|
| 144 |
polished_prompt = polished_prompt.replace("\n", " ")
|
| 145 |
success = True
|
| 146 |
except Exception as e:
|
| 147 |
+
print(f"Error during API call to Hugging Face: {e}")
|
| 148 |
+
# Fallback to original prompt if enhancement fails
|
| 149 |
+
return prompt
|
| 150 |
|
| 151 |
|
| 152 |
def encode_image(pil_image):
|