Update app.py

#6
by linoyts HF Staff - opened
Files changed (1) hide show
  1. app.py +52 -11
app.py CHANGED
@@ -22,6 +22,36 @@ from io import BytesIO
22
  import json
23
  import time # Added for history update delay
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  SYSTEM_PROMPT = '''
26
  # Edit Instruction Rewriter
27
  You are a professional edit instruction rewriter. Your task is to generate a precise, concise, and visually achievable professional-level edit instruction based on the user-provided instruction and the image to be edited.
@@ -141,7 +171,7 @@ Do NOT include JSON formatting or additional explanations.
141
  '''
142
 
143
  # --- Prompt Enhancement using Hugging Face InferenceClient ---
144
- def polish_prompt_hf(prompt, img_list):
145
  """
146
  Rewrites the prompt using a Hugging Face InferenceClient.
147
  """
@@ -149,13 +179,13 @@ def polish_prompt_hf(prompt, img_list):
149
  api_key = os.environ.get("HF_TOKEN")
150
  if not api_key:
151
  print("Warning: HF_TOKEN not set. Falling back to original prompt.")
152
- return prompt
153
 
154
  try:
155
  # Initialize the client
156
- prompt = f"{SYSTEM_PROMPT}\n\nUser Input: {prompt}\n\nRewritten Prompt:"
157
  client = InferenceClient(
158
- provider="cerebras",
159
  api_key=api_key,
160
  )
161
 
@@ -171,7 +201,7 @@ def polish_prompt_hf(prompt, img_list):
171
 
172
  # Call the API
173
  completion = client.chat.completions.create(
174
- model="Qwen/Qwen3-235B-A22B-Instruct-2507",
175
  messages=messages,
176
  )
177
 
@@ -179,7 +209,7 @@ def polish_prompt_hf(prompt, img_list):
179
  result = completion.choices[0].message.content
180
 
181
  # Try to extract JSON if present
182
- if '{"Rewritten"' in result:
183
  try:
184
  # Clean up the response
185
  result = result.replace('```json', '').replace('```', '')
@@ -196,7 +226,7 @@ def polish_prompt_hf(prompt, img_list):
196
  except Exception as e:
197
  print(f"Error during API call to Hugging Face: {e}")
198
  # Fallback to original prompt if enhancement fails
199
- return prompt
200
 
201
  def next_scene_prompt(original_prompt, img_list):
202
  """
@@ -445,7 +475,8 @@ def infer(
445
  ).images
446
 
447
  # Return images, seed, and make button visible
448
- return image, seed, gr.update(visible=True)
 
449
 
450
  # --- Examples and UI Layout ---
451
  examples = []
@@ -544,8 +575,10 @@ with gr.Blocks(css=css) as demo:
544
 
545
  with gr.Column():
546
  result = gr.Gallery(label="Result", show_label=False, type="pil")
547
- # Add this button right after the result gallery - initially hidden
548
- use_output_btn = gr.Button("↗️ Use as input", variant="secondary", size="sm", visible=False)
 
 
549
 
550
  with gr.Row():
551
  gr.Markdown("### 📜 History")
@@ -583,7 +616,8 @@ with gr.Blocks(css=css) as demo:
583
  width,
584
  rewrite_prompt,
585
  ],
586
- outputs=[result, seed, use_output_btn], # Added use_output_btn to outputs
 
587
  ).then(
588
  fn=update_history,
589
  inputs=[result, history_gallery],
@@ -615,5 +649,12 @@ with gr.Blocks(css=css) as demo:
615
 
616
  input_images.change(fn=suggest_next_scene_prompt, inputs=[input_images], outputs=[prompt])
617
 
 
 
 
 
 
 
 
618
  if __name__ == "__main__":
619
  demo.launch()
 
22
  import json
23
  import time # Added for history update delay
24
 
25
+ from gradio_client import Client, handle_file
26
+ import tempfile
27
+
28
+ def turn_into_video(input_images, output_images, prompt):
29
+ """Calls multimodalart/wan-2-2-first-last-frame space to generate a video."""
30
+ if not input_images or not output_images:
31
+ raise gr.Error("Please generate at least one result first.")
32
+
33
+ # Take the first input and first output frame
34
+ start_img = input_images[0][0] if isinstance(input_images[0], tuple) else input_images[0]
35
+ end_img = output_images[0]
36
+
37
+ # Save them temporarily
38
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_start, \
39
+ tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp_end:
40
+ start_img.save(tmp_start.name)
41
+ end_img.save(tmp_end.name)
42
+
43
+ client = Client("multimodalart/wan-2-2-first-last-frame")
44
+
45
+ # Run inference on the remote space
46
+ result = client.predict(
47
+ start_image_pil={"image": handle_file(tmp_start.name)},
48
+ end_image_pil={"image": handle_file(tmp_end.name)},
49
+ prompt=prompt or "generate smooth cinematic transition",
50
+ api_name="/generate_video" # must match the remote Space’s API function name
51
+ )
52
+ return result, gr.update(visible=True)
53
+
54
+
55
  SYSTEM_PROMPT = '''
56
  # Edit Instruction Rewriter
57
  You are a professional edit instruction rewriter. Your task is to generate a precise, concise, and visually achievable professional-level edit instruction based on the user-provided instruction and the image to be edited.
 
171
  '''
172
 
173
  # --- Prompt Enhancement using Hugging Face InferenceClient ---
174
+ def polish_prompt_hf(original_prompt, img_list):
175
  """
176
  Rewrites the prompt using a Hugging Face InferenceClient.
177
  """
 
179
  api_key = os.environ.get("HF_TOKEN")
180
  if not api_key:
181
  print("Warning: HF_TOKEN not set. Falling back to original prompt.")
182
+ return original_prompt
183
 
184
  try:
185
  # Initialize the client
186
+ prompt = f"{SYSTEM_PROMPT}\n\nUser Input: {original_prompt}\n\nRewritten Prompt:"
187
  client = InferenceClient(
188
+ provider="nebius",
189
  api_key=api_key,
190
  )
191
 
 
201
 
202
  # Call the API
203
  completion = client.chat.completions.create(
204
+ model="Qwen/Qwen2.5-VL-72B-Instruct",
205
  messages=messages,
206
  )
207
 
 
209
  result = completion.choices[0].message.content
210
 
211
  # Try to extract JSON if present
212
+ if '"Rewritten"' in result:
213
  try:
214
  # Clean up the response
215
  result = result.replace('```json', '').replace('```', '')
 
226
  except Exception as e:
227
  print(f"Error during API call to Hugging Face: {e}")
228
  # Fallback to original prompt if enhancement fails
229
+ return original_prompt
230
 
231
  def next_scene_prompt(original_prompt, img_list):
232
  """
 
475
  ).images
476
 
477
  # Return images, seed, and make button visible
478
+ return image, seed, gr.update(visible=True), gr.update(visible=True)
479
+
480
 
481
  # --- Examples and UI Layout ---
482
  examples = []
 
575
 
576
  with gr.Column():
577
  result = gr.Gallery(label="Result", show_label=False, type="pil")
578
+ with gr.Row():
579
+ use_output_btn = gr.Button("↗️ Use as input", variant="secondary", size="sm", visible=False)
580
+ turn_video_btn = gr.Button("🎬 Turn into Video", variant="secondary", size="sm", visible=False)
581
+ output_video = gr.Video(label="Generated Video", autoplay=True, visible=False)
582
 
583
  with gr.Row():
584
  gr.Markdown("### 📜 History")
 
616
  width,
617
  rewrite_prompt,
618
  ],
619
+ outputs=[result, seed, use_output_btn, turn_video_btn],
620
+
621
  ).then(
622
  fn=update_history,
623
  inputs=[result, history_gallery],
 
649
 
650
  input_images.change(fn=suggest_next_scene_prompt, inputs=[input_images], outputs=[prompt])
651
 
652
+ turn_video_btn.click(
653
+ fn=turn_into_video,
654
+ inputs=[input_images, result, prompt],
655
+ outputs=[output_video, output_video],
656
+ )
657
+
658
+
659
  if __name__ == "__main__":
660
  demo.launch()