asmaab commited on
Commit
9aace8a
·
verified ·
1 Parent(s): 54560cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -2
app.py CHANGED
@@ -96,7 +96,63 @@ temperature=0.5,
96
  model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
97
  custom_role_conversions=None,
98
  )
99
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  #@tool
101
  #def image_generation_tool(promt: str)->any:
102
  # """A tool that generate image from user`s promt.
@@ -117,7 +173,7 @@ with open("prompts.yaml", 'r') as stream:
117
 
118
  agent = CodeAgent(
119
  model=model,
120
- tools=[final_answer,enhanced_translate,image_generation_tool,search_tool], ## add your tools here (don't remove final answer)
121
  max_steps=6,
122
  verbosity_level=1,
123
  grammar=None,
 
96
  model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',# it is possible that this model may be overloaded
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
+ headers = {
120
+ "Authorization": f"Bearer {os.environ.get('HF_API_TOKEN', 'YOUR_HF_TOKEN')}"
121
+ }
122
+
123
+ # Prepare the payload
124
+ payload = {
125
+ "inputs": prompt,
126
+ "parameters": {
127
+ "negative_prompt": negative_prompt,
128
+ "height": height,
129
+ "width": width,
130
+ "num_inference_steps": 50
131
+ }
132
+ }
133
+
134
+ # Make the API call
135
+ response = requests.post(API_URL, headers=headers, json=payload)
136
+
137
+ if response.status_code != 200:
138
+ return f"Error generating image: {response.text}"
139
+
140
+ # Create output directory if it doesn't exist
141
+ output_dir = "generated_images"
142
+ os.makedirs(output_dir, exist_ok=True)
143
+
144
+ # Save the image
145
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
146
+ image_name = f"{output_dir}/image_{timestamp}.png"
147
+
148
+ # Convert response to image
149
+ image = Image.open(io.BytesIO(response.content))
150
+ image.save(image_name)
151
+
152
+ return f"Image generated successfully and saved as {image_name}"
153
+
154
+ except Exception as e:
155
+ return f"Image generation failed: {str(e)}"
156
  #@tool
157
  #def image_generation_tool(promt: str)->any:
158
  # """A tool that generate image from user`s promt.
 
173
 
174
  agent = CodeAgent(
175
  model=model,
176
+ tools=[final_answer,enhanced_translate,image_generation_tool,search_tool,generate_image_from_prompt, ], ## add your tools here (don't remove final answer)
177
  max_steps=6,
178
  verbosity_level=1,
179
  grammar=None,