Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -9,6 +9,8 @@ from Gradio_UI import GradioUI
|
|
| 9 |
import fitz # PyMuPDF
|
| 10 |
from sentence_transformers import SentenceTransformer, util
|
| 11 |
from transformers import pipeline
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# API Key for weather
|
| 14 |
API_KEY = os.getenv("Weather_Token")
|
|
@@ -135,14 +137,42 @@ def document_qna_tool(pdf_path: str, question: str) -> str:
|
|
| 135 |
return f"[EXCEPTION] {type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
|
| 136 |
|
| 137 |
# -------------------- TOOL 4: Image Generation --------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
# -------------------- Other Components --------------------
|
| 148 |
final_answer = FinalAnswerTool()
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool, Tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 9 |
import fitz # PyMuPDF
|
| 10 |
from sentence_transformers import SentenceTransformer, util
|
| 11 |
from transformers import pipeline
|
| 12 |
+
from PIL import Image
|
| 13 |
+
import io
|
| 14 |
|
| 15 |
# API Key for weather
|
| 16 |
API_KEY = os.getenv("Weather_Token")
|
|
|
|
| 137 |
return f"[EXCEPTION] {type(e).__name__}: {str(e)}\n{traceback.format_exc()}"
|
| 138 |
|
| 139 |
# -------------------- TOOL 4: Image Generation --------------------
|
| 140 |
+
@tool
|
| 141 |
+
def generate_image(prompt: str) -> Image.Image:
|
| 142 |
+
"""
|
| 143 |
+
A tool that generates an image from a text prompt using a Hugging Face Space.
|
| 144 |
+
Args:
|
| 145 |
+
prompt (str): The text description of the image to generate.
|
| 146 |
+
Returns:
|
| 147 |
+
Image.Image: The generated image as a PIL Image object.
|
| 148 |
+
"""
|
| 149 |
+
try:
|
| 150 |
+
# Use the hosted image generation model from Hugging Face Spaces
|
| 151 |
+
image_generator = Tool.from_space(
|
| 152 |
+
"black-forest-labs/FLUX.1-schnell",
|
| 153 |
+
name="image_generator",
|
| 154 |
+
description="Generate an image from a prompt"
|
| 155 |
+
)
|
| 156 |
|
| 157 |
+
# Call the model with the prompt
|
| 158 |
+
result = image_generator(prompt=prompt)
|
| 159 |
|
| 160 |
+
# If the result is bytes → convert to PIL.Image
|
| 161 |
+
if isinstance(result, bytes):
|
| 162 |
+
return Image.open(io.BytesIO(result))
|
| 163 |
+
|
| 164 |
+
# If the result is already a PIL Image
|
| 165 |
+
if isinstance(result, Image.Image):
|
| 166 |
+
return result
|
| 167 |
+
|
| 168 |
+
# If the model gave back a path, open it
|
| 169 |
+
if isinstance(result, str):
|
| 170 |
+
return Image.open(result)
|
| 171 |
+
|
| 172 |
+
raise ValueError("Unexpected output format from image generator.")
|
| 173 |
+
|
| 174 |
+
except Exception as e:
|
| 175 |
+
raise RuntimeError(f"Error generating image: {str(e)}")
|
| 176 |
|
| 177 |
# -------------------- Other Components --------------------
|
| 178 |
final_answer = FinalAnswerTool()
|