Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -179,59 +179,58 @@ def suggest_next_scene_prompt(images):
|
|
| 179 |
"""
|
| 180 |
Suggests a Next Scene prompt based on the uploaded image(s).
|
| 181 |
"""
|
| 182 |
-
if images is None or len(images) == 0:
|
| 183 |
-
return ""
|
| 184 |
-
|
| 185 |
api_key = os.environ.get("HF_TOKEN")
|
| 186 |
if not api_key:
|
| 187 |
-
print("Warning: HF_TOKEN not set.
|
| 188 |
-
return
|
| 189 |
|
| 190 |
try:
|
| 191 |
-
#
|
| 192 |
-
|
| 193 |
-
for item in images:
|
| 194 |
-
try:
|
| 195 |
-
if isinstance(item[0], Image.Image):
|
| 196 |
-
pil_images.append(item[0].convert("RGB"))
|
| 197 |
-
elif isinstance(item[0], str):
|
| 198 |
-
pil_images.append(Image.open(item[0]).convert("RGB"))
|
| 199 |
-
elif hasattr(item, "name"):
|
| 200 |
-
pil_images.append(Image.open(item.name).convert("RGB"))
|
| 201 |
-
except Exception:
|
| 202 |
-
continue
|
| 203 |
-
|
| 204 |
-
if len(pil_images) == 0:
|
| 205 |
-
return ""
|
| 206 |
-
|
| 207 |
client = InferenceClient(
|
| 208 |
provider="cerebras",
|
| 209 |
api_key=api_key,
|
| 210 |
)
|
| 211 |
|
|
|
|
|
|
|
| 212 |
messages = [
|
| 213 |
-
{"role": "system", "content":
|
| 214 |
-
{"role": "user", "content": []}
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
messages[1]["content"].append(
|
| 219 |
-
{"image": f"data:image/png;base64,{encode_image(pil_images[0])}"}
|
| 220 |
-
)
|
| 221 |
-
messages[1]["content"].append({"text": NEXT_SCENE_PROMPT})
|
| 222 |
|
|
|
|
| 223 |
completion = client.chat.completions.create(
|
| 224 |
model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 225 |
messages=messages,
|
| 226 |
)
|
| 227 |
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 231 |
|
| 232 |
except Exception as e:
|
| 233 |
-
print(f"Error
|
| 234 |
-
|
|
|
|
|
|
|
| 235 |
|
| 236 |
|
| 237 |
def encode_image(pil_image):
|
|
@@ -454,7 +453,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 454 |
# gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 455 |
|
| 456 |
# Auto-suggest prompt when images are uploaded
|
| 457 |
-
input_images.
|
| 458 |
fn=suggest_next_scene_prompt,
|
| 459 |
inputs=[input_images],
|
| 460 |
outputs=[prompt]
|
|
|
|
| 179 |
"""
|
| 180 |
Suggests a Next Scene prompt based on the uploaded image(s).
|
| 181 |
"""
|
|
|
|
|
|
|
|
|
|
| 182 |
api_key = os.environ.get("HF_TOKEN")
|
| 183 |
if not api_key:
|
| 184 |
+
print("Warning: HF_TOKEN not set. Falling back to original prompt.")
|
| 185 |
+
return prompt
|
| 186 |
|
| 187 |
try:
|
| 188 |
+
# Initialize the client
|
| 189 |
+
prompt = f"{NEXT_SCENE_PROMPT}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 190 |
client = InferenceClient(
|
| 191 |
provider="cerebras",
|
| 192 |
api_key=api_key,
|
| 193 |
)
|
| 194 |
|
| 195 |
+
# Format the messages for the chat completions API
|
| 196 |
+
sys_promot = "you are a helpful assistant, you should provide useful answers to users."
|
| 197 |
messages = [
|
| 198 |
+
{"role": "system", "content": sys_promot},
|
| 199 |
+
{"role": "user", "content": []}]
|
| 200 |
+
for img in img_list:
|
| 201 |
+
messages[1]["content"].append(
|
| 202 |
+
{"image": f"data:image/png;base64,{encode_image(img)}"})
|
| 203 |
+
messages[1]["content"].append({"text": f"{prompt}"})
|
|
|
|
|
|
|
|
|
|
| 204 |
|
| 205 |
+
# Call the API
|
| 206 |
completion = client.chat.completions.create(
|
| 207 |
model="Qwen/Qwen3-235B-A22B-Instruct-2507",
|
| 208 |
messages=messages,
|
| 209 |
)
|
| 210 |
|
| 211 |
+
# Parse the response
|
| 212 |
+
result = completion.choices[0].message.content
|
| 213 |
+
|
| 214 |
+
# Try to extract JSON if present
|
| 215 |
+
if '{"Rewritten"' in result:
|
| 216 |
+
try:
|
| 217 |
+
# Clean up the response
|
| 218 |
+
result = result.replace('```json', '').replace('```', '')
|
| 219 |
+
result_json = json.loads(result)
|
| 220 |
+
polished_prompt = result_json.get('Rewritten', result)
|
| 221 |
+
except:
|
| 222 |
+
polished_prompt = result
|
| 223 |
+
else:
|
| 224 |
+
polished_prompt = result
|
| 225 |
+
|
| 226 |
+
polished_prompt = polished_prompt.strip().replace("\n", " ")
|
| 227 |
+
return polished_prompt
|
| 228 |
|
| 229 |
except Exception as e:
|
| 230 |
+
print(f"Error during API call to Hugging Face: {e}")
|
| 231 |
+
# Fallback to original prompt if enhancement fails
|
| 232 |
+
return prompt
|
| 233 |
+
|
| 234 |
|
| 235 |
|
| 236 |
def encode_image(pil_image):
|
|
|
|
| 453 |
# gr.Examples(examples=examples, inputs=[prompt], outputs=[result, seed], fn=infer, cache_examples=False)
|
| 454 |
|
| 455 |
# Auto-suggest prompt when images are uploaded
|
| 456 |
+
input_images.upload(
|
| 457 |
fn=suggest_next_scene_prompt,
|
| 458 |
inputs=[input_images],
|
| 459 |
outputs=[prompt]
|