Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -97,72 +97,30 @@ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',#
|
|
| 97 |
custom_role_conversions=None,
|
| 98 |
)
|
| 99 |
@tool
|
| 100 |
-
def
|
| 101 |
-
"""Generate an image based on a text description
|
| 102 |
Args:
|
| 103 |
prompt: Detailed description of the image you want to generate
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
width: Image width in pixels (default: 512)
|
| 107 |
"""
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
"parameters": {
|
| 125 |
-
"negative_prompt": negative_prompt,
|
| 126 |
-
"height": height,
|
| 127 |
-
"width": width,
|
| 128 |
-
"num_inference_steps": 50
|
| 129 |
-
}
|
| 130 |
-
}
|
| 131 |
-
|
| 132 |
-
# Make the API call
|
| 133 |
-
response = requests.post(API_URL, json=payload)
|
| 134 |
-
|
| 135 |
-
if response.status_code != 200:
|
| 136 |
-
return f"Error generating image: {response.text}"
|
| 137 |
-
|
| 138 |
-
# Create output directory if it doesn't exist
|
| 139 |
-
output_dir = "generated_images"
|
| 140 |
-
os.makedirs(output_dir, exist_ok=True)
|
| 141 |
-
|
| 142 |
-
# Save the image
|
| 143 |
-
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 144 |
-
image_name = f"{output_dir}/image_{timestamp}.png"
|
| 145 |
-
|
| 146 |
-
# Convert response to image
|
| 147 |
-
image = Image.open(io.BytesIO(response.content))
|
| 148 |
-
image.save(image_name)
|
| 149 |
-
|
| 150 |
-
return f"Image generated successfully and saved as {image_name}"
|
| 151 |
|
| 152 |
-
except Exception as e:
|
| 153 |
-
return f"Image generation failed: {str(e)}"
|
| 154 |
-
#@tool
|
| 155 |
-
#def image_generation_tool(promt: str)->any:
|
| 156 |
-
# """A tool that generate image from user`s promt.
|
| 157 |
-
# Args:
|
| 158 |
-
# promt: description of image
|
| 159 |
-
# """
|
| 160 |
-
# try:
|
| 161 |
-
# image = image_generation_tool(promt)
|
| 162 |
-
# return image
|
| 163 |
-
|
| 164 |
-
# except Exception as e:
|
| 165 |
-
# return f'We have some problem {e}'
|
| 166 |
# Import tool from Hub
|
| 167 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 168 |
search_tool = DuckDuckGoSearchTool()
|
|
@@ -171,7 +129,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 171 |
|
| 172 |
agent = CodeAgent(
|
| 173 |
model=model,
|
| 174 |
-
tools=[final_answer,enhanced_translate,
|
| 175 |
max_steps=6,
|
| 176 |
verbosity_level=1,
|
| 177 |
grammar=None,
|
|
|
|
| 97 |
custom_role_conversions=None,
|
| 98 |
)
|
| 99 |
@tool
|
| 100 |
+
def image_generator(prompt: str) -> bytes:
|
| 101 |
+
"""Generate an image based on a text description.
|
| 102 |
Args:
|
| 103 |
prompt: Detailed description of the image you want to generate
|
| 104 |
+
Returns:
|
| 105 |
+
The image data as bytes that can be displayed
|
|
|
|
| 106 |
"""
|
| 107 |
+
import requests
|
| 108 |
+
|
| 109 |
+
API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
|
| 110 |
+
|
| 111 |
+
# Make API request without authentication headers
|
| 112 |
+
response = requests.post(
|
| 113 |
+
API_URL,
|
| 114 |
+
json={"inputs": prompt}
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Check if the request was successful
|
| 118 |
+
if response.status_code != 200:
|
| 119 |
+
raise Exception(f"Error generating image: {response.text}")
|
| 120 |
+
|
| 121 |
+
# Return the raw image bytes
|
| 122 |
+
return response.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
# Import tool from Hub
|
| 125 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 126 |
search_tool = DuckDuckGoSearchTool()
|
|
|
|
| 129 |
|
| 130 |
agent = CodeAgent(
|
| 131 |
model=model,
|
| 132 |
+
tools=[final_answer,enhanced_translate,image_generator,search_tool ], ## add your tools here (don't remove final answer)
|
| 133 |
max_steps=6,
|
| 134 |
verbosity_level=1,
|
| 135 |
grammar=None,
|