Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,40 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
from Gradio_UI import GradioUI
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
@tool
|
| 9 |
def my_custom_tool(prompt: str) -> str:
|
| 10 |
-
"""A tool that generates an image from a text prompt.
|
| 11 |
-
|
| 12 |
Args:
|
| 13 |
-
prompt: The description of the image to generate.
|
| 14 |
-
|
| 15 |
Returns:
|
| 16 |
-
A
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
-
# Load the image generation tool from Hugging Face Hub
|
| 20 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
return f"Error generating image: {str(e)}"
|
| 25 |
|
|
@@ -68,4 +83,3 @@ agent = CodeAgent(
|
|
| 68 |
)
|
| 69 |
|
| 70 |
GradioUI(agent).launch()
|
| 71 |
-
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
from Gradio_UI import GradioUI
|
| 7 |
+
import io
|
| 8 |
+
import base64
|
| 9 |
+
from PIL import Image
|
| 10 |
|
| 11 |
@tool
|
| 12 |
def my_custom_tool(prompt: str) -> str:
|
| 13 |
+
"""A tool that generates an image from a text prompt using a Hugging Face model.
|
| 14 |
+
|
| 15 |
Args:
|
| 16 |
+
prompt: The text description of the image to generate.
|
| 17 |
+
|
| 18 |
Returns:
|
| 19 |
+
A base64-encoded image string that can be displayed or a direct image URL.
|
| 20 |
"""
|
| 21 |
try:
|
| 22 |
+
# Load the text-to-image generation tool from Hugging Face Hub
|
| 23 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 24 |
+
|
| 25 |
+
# Generate an image
|
| 26 |
+
image = image_generation_tool(prompt)
|
| 27 |
+
|
| 28 |
+
# If the tool returns a PIL image, convert it to a displayable format
|
| 29 |
+
if isinstance(image, Image.Image):
|
| 30 |
+
buffered = io.BytesIO()
|
| 31 |
+
image.save(buffered, format="JPEG")
|
| 32 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 33 |
+
return f"data:image/jpeg;base64,{img_str}" # Base64 image format for direct display
|
| 34 |
+
|
| 35 |
+
# If the tool returns a URL, return it
|
| 36 |
+
return f"Generated Image URL: {image}"
|
| 37 |
+
|
| 38 |
except Exception as e:
|
| 39 |
return f"Error generating image: {str(e)}"
|
| 40 |
|
|
|
|
| 83 |
)
|
| 84 |
|
| 85 |
GradioUI(agent).launch()
|
|
|