Spaces:
Runtime error
Runtime error
Leonardo
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,10 +1,8 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import contextlib
|
| 3 |
import mimetypes
|
| 4 |
import os
|
| 5 |
import re
|
| 6 |
import shutil
|
| 7 |
-
from typing import
|
| 8 |
|
| 9 |
from dotenv import load_dotenv
|
| 10 |
from huggingface_hub import login
|
|
@@ -80,13 +78,14 @@ ALLOWED_FILE_TYPES = [
|
|
| 80 |
"text/plain",
|
| 81 |
"image/png",
|
| 82 |
"image/jpeg", # Added JPEG support
|
| 83 |
-
"image/gif",
|
| 84 |
"video/mp4",
|
| 85 |
"audio/mpeg", # Added MP3 support
|
| 86 |
-
"audio/wav",
|
| 87 |
-
"audio/ogg",
|
| 88 |
]
|
| 89 |
|
|
|
|
| 90 |
def setup_environment():
|
| 91 |
"""Initialize environment variables and authentication."""
|
| 92 |
load_dotenv(override=True)
|
|
@@ -96,6 +95,7 @@ def setup_environment():
|
|
| 96 |
else:
|
| 97 |
print("HF_TOKEN not found in environment variables.")
|
| 98 |
|
|
|
|
| 99 |
# ------------------------ Model and Tool Management ------------------------
|
| 100 |
class ModelManager:
|
| 101 |
"""Manages model loading and initialization."""
|
|
@@ -135,6 +135,7 @@ class ModelManager:
|
|
| 135 |
print(f"✗ Couldn't load model: {e}")
|
| 136 |
raise
|
| 137 |
|
|
|
|
| 138 |
class ToolRegistry:
|
| 139 |
"""Manages tool initialization and organization."""
|
| 140 |
|
|
@@ -178,6 +179,7 @@ class ToolRegistry:
|
|
| 178 |
print(f"✗ Couldn't initialize image generation tool: {e}")
|
| 179 |
raise
|
| 180 |
|
|
|
|
| 181 |
# ------------------------ Agent Creation and Execution ------------------------
|
| 182 |
def create_agent():
|
| 183 |
"""Creates a fresh agent instance with properly configured tools."""
|
|
@@ -214,6 +216,7 @@ def create_agent():
|
|
| 214 |
planning_interval=4,
|
| 215 |
)
|
| 216 |
|
|
|
|
| 217 |
def stream_to_gradio(
|
| 218 |
agent,
|
| 219 |
task: str,
|
|
@@ -239,18 +242,19 @@ def stream_to_gradio(
|
|
| 239 |
elif isinstance(final_answer, AgentImage):
|
| 240 |
yield gr.ChatMessage(
|
| 241 |
role="assistant",
|
| 242 |
-
content=
|
| 243 |
) # Send as Gradio-compatible file object:
|
| 244 |
elif isinstance(final_answer, AgentAudio):
|
| 245 |
yield gr.ChatMessage(
|
| 246 |
role="assistant",
|
| 247 |
-
content={
|
| 248 |
-
)
|
| 249 |
else:
|
| 250 |
yield gr.ChatMessage(
|
| 251 |
role="assistant", content=f"**Final answer:** {str(final_answer)}"
|
| 252 |
)
|
| 253 |
|
|
|
|
| 254 |
# ------------------------ Gradio UI Components ------------------------
|
| 255 |
class GradioUI:
|
| 256 |
"""A one-line interface to launch your agent in Gradio."""
|
|
@@ -327,11 +331,16 @@ class GradioUI:
|
|
| 327 |
sanitized_name = "".join(name_parts) + extension
|
| 328 |
|
| 329 |
# Limit File Size, and Throw Error
|
| 330 |
-
max_file_size_mb = 50
|
| 331 |
file_size_mb = os.path.getsize(file.name) / (1024 * 1024) # Size in MB
|
| 332 |
|
| 333 |
if file_size_mb > max_file_size_mb:
|
| 334 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
|
| 336 |
# Save the uploaded file to the specified folder
|
| 337 |
file_path = os.path.join(self.file_upload_folder, sanitized_name)
|
|
@@ -353,7 +362,7 @@ class GradioUI:
|
|
| 353 |
gr.Textbox(
|
| 354 |
value="",
|
| 355 |
interactive=False,
|
| 356 |
-
placeholder="Processing...",
|
| 357 |
),
|
| 358 |
gr.Button(interactive=False),
|
| 359 |
)
|
|
@@ -400,7 +409,9 @@ class GradioUI:
|
|
| 400 |
else:
|
| 401 |
return self._create_mobile_layout()
|
| 402 |
|
| 403 |
-
demo.queue(max_size=20).launch(
|
|
|
|
|
|
|
| 404 |
|
| 405 |
def _create_desktop_layout(self):
|
| 406 |
"""Create the desktop layout with sidebar."""
|
|
@@ -575,6 +586,7 @@ class GradioUI:
|
|
| 575 |
[text_input, launch_research_btn],
|
| 576 |
)
|
| 577 |
|
|
|
|
| 578 |
# ------------------------ Execution ------------------------
|
| 579 |
def main():
|
| 580 |
"""Main entry point for the application."""
|
|
@@ -587,5 +599,6 @@ def main():
|
|
| 587 |
# Launch UI
|
| 588 |
GradioUI(file_upload_folder="uploaded_files").launch()
|
| 589 |
|
|
|
|
| 590 |
if __name__ == "__main__":
|
| 591 |
main()
|
|
|
|
|
|
|
|
|
|
| 1 |
import mimetypes
|
| 2 |
import os
|
| 3 |
import re
|
| 4 |
import shutil
|
| 5 |
+
from typing import Optional
|
| 6 |
|
| 7 |
from dotenv import load_dotenv
|
| 8 |
from huggingface_hub import login
|
|
|
|
| 78 |
"text/plain",
|
| 79 |
"image/png",
|
| 80 |
"image/jpeg", # Added JPEG support
|
| 81 |
+
"image/gif", # Added GIF support
|
| 82 |
"video/mp4",
|
| 83 |
"audio/mpeg", # Added MP3 support
|
| 84 |
+
"audio/wav", # Added WAV support
|
| 85 |
+
"audio/ogg", # Added OGG support
|
| 86 |
]
|
| 87 |
|
| 88 |
+
|
| 89 |
def setup_environment():
|
| 90 |
"""Initialize environment variables and authentication."""
|
| 91 |
load_dotenv(override=True)
|
|
|
|
| 95 |
else:
|
| 96 |
print("HF_TOKEN not found in environment variables.")
|
| 97 |
|
| 98 |
+
|
| 99 |
# ------------------------ Model and Tool Management ------------------------
|
| 100 |
class ModelManager:
|
| 101 |
"""Manages model loading and initialization."""
|
|
|
|
| 135 |
print(f"✗ Couldn't load model: {e}")
|
| 136 |
raise
|
| 137 |
|
| 138 |
+
|
| 139 |
class ToolRegistry:
|
| 140 |
"""Manages tool initialization and organization."""
|
| 141 |
|
|
|
|
| 179 |
print(f"✗ Couldn't initialize image generation tool: {e}")
|
| 180 |
raise
|
| 181 |
|
| 182 |
+
|
| 183 |
# ------------------------ Agent Creation and Execution ------------------------
|
| 184 |
def create_agent():
|
| 185 |
"""Creates a fresh agent instance with properly configured tools."""
|
|
|
|
| 216 |
planning_interval=4,
|
| 217 |
)
|
| 218 |
|
| 219 |
+
|
| 220 |
def stream_to_gradio(
|
| 221 |
agent,
|
| 222 |
task: str,
|
|
|
|
| 242 |
elif isinstance(final_answer, AgentImage):
|
| 243 |
yield gr.ChatMessage(
|
| 244 |
role="assistant",
|
| 245 |
+
content={"image": final_answer.to_string(), "type": "file"},
|
| 246 |
) # Send as Gradio-compatible file object:
|
| 247 |
elif isinstance(final_answer, AgentAudio):
|
| 248 |
yield gr.ChatMessage(
|
| 249 |
role="assistant",
|
| 250 |
+
content={"audio": final_answer.to_string(), "type": "file"},
|
| 251 |
+
) # Send as Gradio-compatible file object
|
| 252 |
else:
|
| 253 |
yield gr.ChatMessage(
|
| 254 |
role="assistant", content=f"**Final answer:** {str(final_answer)}"
|
| 255 |
)
|
| 256 |
|
| 257 |
+
|
| 258 |
# ------------------------ Gradio UI Components ------------------------
|
| 259 |
class GradioUI:
|
| 260 |
"""A one-line interface to launch your agent in Gradio."""
|
|
|
|
| 331 |
sanitized_name = "".join(name_parts) + extension
|
| 332 |
|
| 333 |
# Limit File Size, and Throw Error
|
| 334 |
+
max_file_size_mb = 50 # Define the limit
|
| 335 |
file_size_mb = os.path.getsize(file.name) / (1024 * 1024) # Size in MB
|
| 336 |
|
| 337 |
if file_size_mb > max_file_size_mb:
|
| 338 |
+
return (
|
| 339 |
+
gr.Textbox(
|
| 340 |
+
f"File size exceeds {max_file_size_mb} MB limit.", visible=True
|
| 341 |
+
),
|
| 342 |
+
file_uploads_log,
|
| 343 |
+
)
|
| 344 |
|
| 345 |
# Save the uploaded file to the specified folder
|
| 346 |
file_path = os.path.join(self.file_upload_folder, sanitized_name)
|
|
|
|
| 362 |
gr.Textbox(
|
| 363 |
value="",
|
| 364 |
interactive=False,
|
| 365 |
+
placeholder="Processing...", # Changed placeholder.
|
| 366 |
),
|
| 367 |
gr.Button(interactive=False),
|
| 368 |
)
|
|
|
|
| 409 |
else:
|
| 410 |
return self._create_mobile_layout()
|
| 411 |
|
| 412 |
+
demo.queue(max_size=20).launch(
|
| 413 |
+
debug=True, **kwargs
|
| 414 |
+
) # Add queue with reasonable size
|
| 415 |
|
| 416 |
def _create_desktop_layout(self):
|
| 417 |
"""Create the desktop layout with sidebar."""
|
|
|
|
| 586 |
[text_input, launch_research_btn],
|
| 587 |
)
|
| 588 |
|
| 589 |
+
|
| 590 |
# ------------------------ Execution ------------------------
|
| 591 |
def main():
|
| 592 |
"""Main entry point for the application."""
|
|
|
|
| 599 |
# Launch UI
|
| 600 |
GradioUI(file_upload_folder="uploaded_files").launch()
|
| 601 |
|
| 602 |
+
|
| 603 |
if __name__ == "__main__":
|
| 604 |
main()
|