Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,9 @@ import pytz
|
|
5 |
import yaml
|
6 |
import os
|
7 |
import sys
|
|
|
|
|
|
|
8 |
from tools.final_answer import FinalAnswerTool
|
9 |
from tools.web_search import DuckDuckGoSearchTool
|
10 |
from tools.visit_webpage import VisitWebpageTool
|
@@ -33,9 +36,8 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
33 |
# Handle any errors that might occur (invalid timezone, etc.)
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
-
|
37 |
#############################################################
|
38 |
-
# TOOL 2: IMAGE GENERATION TOOL
|
39 |
#############################################################
|
40 |
@tool
|
41 |
def generate_image_from_text(prompt: str) -> str:
|
@@ -44,15 +46,11 @@ def generate_image_from_text(prompt: str) -> str:
|
|
44 |
prompt: A detailed text description of the image you want to generate.
|
45 |
"""
|
46 |
try:
|
47 |
-
import os
|
48 |
-
import uuid
|
49 |
-
from datetime import datetime
|
50 |
-
|
51 |
# Create images directory if it doesn't exist
|
52 |
os.makedirs("uploads/images", exist_ok=True)
|
53 |
|
54 |
# Generate a unique filename
|
55 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
56 |
unique_id = str(uuid.uuid4())[:8]
|
57 |
filename = f"uploads/images/{timestamp}_{unique_id}.jpg"
|
58 |
|
@@ -62,8 +60,30 @@ def generate_image_from_text(prompt: str) -> str:
|
|
62 |
# Save the PIL image to file
|
63 |
pil_image.save(filename)
|
64 |
|
65 |
-
#
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
except Exception as e:
|
68 |
# Handle any errors that occur during image generation
|
69 |
return f"Error generating image: {str(e)}"
|
@@ -181,5 +201,4 @@ print("✓ Agent configured successfully")
|
|
181 |
#############################################################
|
182 |
print("Launching Gradio UI...")
|
183 |
# Start the Gradio interface with our configured agent and file upload directory
|
184 |
-
# Remove the debug parameter as it's already set in the GradioUI.launch method
|
185 |
GradioUI(agent, file_upload_folder="uploads").launch()
|
|
|
5 |
import yaml
|
6 |
import os
|
7 |
import sys
|
8 |
+
import uuid
|
9 |
+
import base64
|
10 |
+
from io import BytesIO
|
11 |
from tools.final_answer import FinalAnswerTool
|
12 |
from tools.web_search import DuckDuckGoSearchTool
|
13 |
from tools.visit_webpage import VisitWebpageTool
|
|
|
36 |
# Handle any errors that might occur (invalid timezone, etc.)
|
37 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
38 |
|
|
|
39 |
#############################################################
|
40 |
+
# TOOL 2: ENHANCED IMAGE GENERATION TOOL
|
41 |
#############################################################
|
42 |
@tool
|
43 |
def generate_image_from_text(prompt: str) -> str:
|
|
|
46 |
prompt: A detailed text description of the image you want to generate.
|
47 |
"""
|
48 |
try:
|
|
|
|
|
|
|
|
|
49 |
# Create images directory if it doesn't exist
|
50 |
os.makedirs("uploads/images", exist_ok=True)
|
51 |
|
52 |
# Generate a unique filename
|
53 |
+
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
54 |
unique_id = str(uuid.uuid4())[:8]
|
55 |
filename = f"uploads/images/{timestamp}_{unique_id}.jpg"
|
56 |
|
|
|
60 |
# Save the PIL image to file
|
61 |
pil_image.save(filename)
|
62 |
|
63 |
+
# Get the HuggingFace Space name from environment variables
|
64 |
+
# This is specific to HF Spaces environment
|
65 |
+
space_name = os.environ.get("SPACE_ID", "unknown-space")
|
66 |
+
|
67 |
+
# Construct a URL that works in Hugging Face Spaces
|
68 |
+
file_url = f"https://huggingface.co/spaces/{space_name}/resolve/main/{filename}"
|
69 |
+
|
70 |
+
# Also create a data URL as fallback
|
71 |
+
buffered = BytesIO()
|
72 |
+
pil_image.save(buffered, format="JPEG")
|
73 |
+
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
74 |
+
data_url = f"data:image/jpeg;base64,{img_str}"
|
75 |
+
|
76 |
+
# Return an HTML representation with both the file URL and the data URL as fallback
|
77 |
+
html_output = f"""
|
78 |
+
<div style="margin-bottom: 20px;">
|
79 |
+
<p><strong>Image generated from prompt:</strong> "{prompt}"</p>
|
80 |
+
<img src="{filename}" alt="Generated image" style="max-width:100%; height:auto; border:1px solid #ddd; border-radius:4px;">
|
81 |
+
<p>
|
82 |
+
<a href="{file_url}" target="_blank" style="display:inline-block; margin-top:10px; padding:8px 16px; background-color:#4CAF50; color:white; text-decoration:none; border-radius:4px;">View Full Size Image</a>
|
83 |
+
</p>
|
84 |
+
</div>
|
85 |
+
"""
|
86 |
+
return html_output
|
87 |
except Exception as e:
|
88 |
# Handle any errors that occur during image generation
|
89 |
return f"Error generating image: {str(e)}"
|
|
|
201 |
#############################################################
|
202 |
print("Launching Gradio UI...")
|
203 |
# Start the Gradio interface with our configured agent and file upload directory
|
|
|
204 |
GradioUI(agent, file_upload_folder="uploads").launch()
|