Fix image rendering in GradioUI and avoid forced share=True on Hugging Face Spaces
#583
by
DheerajParkash
- opened
- Gradio_UI.py +12 -2
Gradio_UI.py
CHANGED
|
@@ -154,6 +154,9 @@ def stream_to_gradio(
|
|
| 154 |
yield message
|
| 155 |
|
| 156 |
final_answer = step_log # Last log is the run's final_answer
|
|
|
|
|
|
|
|
|
|
| 157 |
final_answer = handle_agent_output_types(final_answer)
|
| 158 |
|
| 159 |
if isinstance(final_answer, AgentText):
|
|
@@ -162,9 +165,16 @@ def stream_to_gradio(
|
|
| 162 |
content=f"**Final answer:**\n{final_answer.to_string()}\n",
|
| 163 |
)
|
| 164 |
elif isinstance(final_answer, AgentImage):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
yield gr.ChatMessage(
|
| 166 |
role="assistant",
|
| 167 |
-
content={"path":
|
| 168 |
)
|
| 169 |
elif isinstance(final_answer, AgentAudio):
|
| 170 |
yield gr.ChatMessage(
|
|
@@ -290,7 +300,7 @@ class GradioUI:
|
|
| 290 |
[stored_messages, text_input],
|
| 291 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 292 |
|
| 293 |
-
demo.launch(debug=True,
|
| 294 |
|
| 295 |
|
| 296 |
__all__ = ["stream_to_gradio", "GradioUI"]
|
|
|
|
| 154 |
yield message
|
| 155 |
|
| 156 |
final_answer = step_log # Last log is the run's final_answer
|
| 157 |
+
if hasattr(final_answer, "final_answer"):
|
| 158 |
+
final_answer = final_answer.final_answer # unwrap FinalAnswerStep
|
| 159 |
+
|
| 160 |
final_answer = handle_agent_output_types(final_answer)
|
| 161 |
|
| 162 |
if isinstance(final_answer, AgentText):
|
|
|
|
| 165 |
content=f"**Final answer:**\n{final_answer.to_string()}\n",
|
| 166 |
)
|
| 167 |
elif isinstance(final_answer, AgentImage):
|
| 168 |
+
path = final_answer.to_string()
|
| 169 |
+
# If to_string() isn't a real file path, try saving the PIL image
|
| 170 |
+
if not isinstance(path, str) or not os.path.exists(path):
|
| 171 |
+
if hasattr(final_answer, "image") and final_answer.image is not None:
|
| 172 |
+
os.makedirs("outputs", exist_ok=True)
|
| 173 |
+
path = os.path.join("outputs", "generated.png")
|
| 174 |
+
final_answer.image.save(path)
|
| 175 |
yield gr.ChatMessage(
|
| 176 |
role="assistant",
|
| 177 |
+
content={"path": path, "mime_type": "image/png"},
|
| 178 |
)
|
| 179 |
elif isinstance(final_answer, AgentAudio):
|
| 180 |
yield gr.ChatMessage(
|
|
|
|
| 300 |
[stored_messages, text_input],
|
| 301 |
).then(self.interact_with_agent, [stored_messages, chatbot], [chatbot])
|
| 302 |
|
| 303 |
+
demo.launch(debug=True, **kwargs)
|
| 304 |
|
| 305 |
|
| 306 |
__all__ = ["stream_to_gradio", "GradioUI"]
|