Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -60,30 +60,8 @@ def generate_image_from_text(prompt: str) -> str:
|
|
60 |
# Save the PIL image to file
|
61 |
pil_image.save(filename)
|
62 |
|
63 |
-
#
|
64 |
-
|
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)}"
|
@@ -111,10 +89,45 @@ def search_web(query: str) -> str:
|
|
111 |
return f"Error searching the web: {str(e)}"
|
112 |
|
113 |
#############################################################
|
114 |
-
# FINAL ANSWER TOOL (REQUIRED)
|
115 |
#############################################################
|
116 |
# Initialize the final answer tool - required for the agent to provide final answers
|
117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
118 |
|
119 |
#############################################################
|
120 |
# VISIT WEBPAGE TOOL - From tools directory
|
|
|
60 |
# Save the PIL image to file
|
61 |
pil_image.save(filename)
|
62 |
|
63 |
+
# Return only the filename - the display formatting is now handled by the final_answer tool
|
64 |
+
return filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
except Exception as e:
|
66 |
# Handle any errors that occur during image generation
|
67 |
return f"Error generating image: {str(e)}"
|
|
|
89 |
return f"Error searching the web: {str(e)}"
|
90 |
|
91 |
#############################################################
|
92 |
+
# FINAL ANSWER TOOL (REQUIRED & CUSTOMIZED)
|
93 |
#############################################################
|
94 |
# Initialize the final answer tool - required for the agent to provide final answers
|
95 |
+
final_answer_base = FinalAnswerTool()
|
96 |
+
|
97 |
+
@tool
|
98 |
+
def final_answer(answer) -> str:
|
99 |
+
"""Provides a final answer to the given problem.
|
100 |
+
Args:
|
101 |
+
answer: The final answer to the problem
|
102 |
+
"""
|
103 |
+
# Check if the answer is a list of image paths
|
104 |
+
if isinstance(answer, list) and all(isinstance(item, str) and 'uploads/images/' in item for item in answer):
|
105 |
+
# Build HTML to display all images
|
106 |
+
html_output = "<div style='display: flex; flex-direction: column; gap: 20px;'>"
|
107 |
+
|
108 |
+
# Get the HuggingFace Space name from environment variables
|
109 |
+
space_name = os.environ.get("SPACE_ID", "unknown-space")
|
110 |
+
|
111 |
+
for i, image_path in enumerate(answer):
|
112 |
+
# Construct a URL that works in Hugging Face Spaces
|
113 |
+
file_url = f"https://huggingface.co/spaces/{space_name}/resolve/main/{image_path}"
|
114 |
+
|
115 |
+
html_output += f"""
|
116 |
+
<div style="margin-bottom: 20px; border: 1px solid #ddd; border-radius: 8px; padding: 15px; background-color: #f9f9f9;">
|
117 |
+
<h3 style="margin-top: 0;">Image {i+1}</h3>
|
118 |
+
<img src="{image_path}" alt="Generated image {i+1}" style="max-width:100%; height:auto; border-radius:4px;">
|
119 |
+
<div style="margin-top:10px;">
|
120 |
+
<a href="{file_url}" target="_blank" style="display:inline-block; padding:8px 16px; background-color:#4CAF50; color:white; text-decoration:none; border-radius:4px; margin-right:10px;">View Full Size</a>
|
121 |
+
<code style="display:block; margin-top:10px; padding:8px; background-color:#f0f0f0; border-radius:4px; font-size:12px; word-break:break-all;">{file_url}</code>
|
122 |
+
</div>
|
123 |
+
</div>
|
124 |
+
"""
|
125 |
+
|
126 |
+
html_output += "</div>"
|
127 |
+
return final_answer_base(html_output)
|
128 |
+
|
129 |
+
# Otherwise, just pass the answer to the regular final_answer tool
|
130 |
+
return final_answer_base(answer)
|
131 |
|
132 |
#############################################################
|
133 |
# VISIT WEBPAGE TOOL - From tools directory
|