asmaab commited on
Commit
eb232d2
·
verified ·
1 Parent(s): 73efcdd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -63
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 generate_image_from_prompt(prompt: str, negative_prompt: str = "", height: int = 512, width: int = 512) -> str:
101
- """Generate an image based on a text description using AI.
102
  Args:
103
  prompt: Detailed description of the image you want to generate
104
- negative_prompt: Elements to avoid in the generated image (optional)
105
- height: Image height in pixels (default: 512)
106
- width: Image width in pixels (default: 512)
107
  """
108
- try:
109
- import os
110
- import requests
111
- import io
112
- import base64
113
- from PIL import Image
114
- from datetime import datetime
115
-
116
- API_URL = "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5"
117
- # You need to set your Hugging Face API token
118
- # Get one from: https://huggingface.co/settings/tokens
119
-
120
-
121
- # Prepare the payload
122
- payload = {
123
- "inputs": prompt,
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,image_generation_tool,search_tool,generate_image_from_prompt, ], ## add your tools here (don't remove final answer)
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,