Update app.py
Browse files
app.py
CHANGED
@@ -2,10 +2,11 @@ from PIL import Image, ImageDraw, ImageFont
|
|
2 |
import tempfile
|
3 |
import gradio as gr
|
4 |
from smolagents import CodeAgent, InferenceClientModel
|
5 |
-
from smolagents import DuckDuckGoSearchTool
|
6 |
from huggingface_hub import InferenceClient
|
7 |
from diffusers import DiffusionPipeline
|
8 |
import torch
|
|
|
9 |
|
10 |
# =========================================================
|
11 |
# Utility functions
|
@@ -91,7 +92,7 @@ class TextToImageTool(Tool):
|
|
91 |
def forward(self, prompt):
|
92 |
return self.client.text_to_image(prompt)
|
93 |
'''
|
94 |
-
|
95 |
class TextToImageTool(Tool):
|
96 |
description = "This tool creates an image according to a prompt. Add details like 'high-res, photorealistic'."
|
97 |
name = "image_generator"
|
@@ -116,7 +117,32 @@ class TextToImageTool(Tool):
|
|
116 |
def forward(self, prompt):
|
117 |
image = self.pipe(prompt).images[0]
|
118 |
return image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
# =========================================================
|
122 |
# Tool and Agent Initialization
|
|
|
2 |
import tempfile
|
3 |
import gradio as gr
|
4 |
from smolagents import CodeAgent, InferenceClientModel
|
5 |
+
from smolagents import DuckDuckGoSearchTool #Tool
|
6 |
from huggingface_hub import InferenceClient
|
7 |
from diffusers import DiffusionPipeline
|
8 |
import torch
|
9 |
+
from autoagents.core import Tool
|
10 |
|
11 |
# =========================================================
|
12 |
# Utility functions
|
|
|
92 |
def forward(self, prompt):
|
93 |
return self.client.text_to_image(prompt)
|
94 |
'''
|
95 |
+
'''
|
96 |
class TextToImageTool(Tool):
|
97 |
description = "This tool creates an image according to a prompt. Add details like 'high-res, photorealistic'."
|
98 |
name = "image_generator"
|
|
|
117 |
def forward(self, prompt):
|
118 |
image = self.pipe(prompt).images[0]
|
119 |
return image
|
120 |
+
'''
|
121 |
+
|
122 |
+
# Use a public model (you can swap for a gated model with token later)
|
123 |
+
pipe = DiffusionPipeline.from_pretrained(
|
124 |
+
"stabilityai/stable-diffusion-2-1",
|
125 |
+
torch_dtype=torch.float16
|
126 |
+
).to("cuda" if torch.cuda.is_available() else "cpu")
|
127 |
+
|
128 |
+
# --------------------------------------------------------
|
129 |
+
# Define a simple image generation function
|
130 |
+
# --------------------------------------------------------
|
131 |
+
|
132 |
+
def generate_image(prompt: str) -> Image.Image:
|
133 |
+
"""Generate an image from a text prompt using a diffusion model."""
|
134 |
+
image = pipe(prompt).images[0]
|
135 |
+
return image
|
136 |
+
|
137 |
+
# --------------------------------------------------------
|
138 |
+
# Wrap it into a Tool using from_function
|
139 |
+
# --------------------------------------------------------
|
140 |
|
141 |
+
image_generation_tool = Tool.from_function(
|
142 |
+
name="image_generator",
|
143 |
+
description="Generate an image from a prompt, e.g. 'a dog on Mars in high-res'.",
|
144 |
+
func=generate_image
|
145 |
+
)
|
146 |
|
147 |
# =========================================================
|
148 |
# Tool and Agent Initialization
|