Benedetto Scala commited on
Commit
996d404
·
1 Parent(s): 69a231b

add image generation and Hugging Face login tools

Browse files
Files changed (1) hide show
  1. app.py +35 -2
app.py CHANGED
@@ -170,15 +170,47 @@ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',#
170
  custom_role_conversions=None,
171
  )
172
 
 
173
 
174
- image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
 
176
  with open("prompts.yaml", 'r') as stream:
177
  prompt_templates = yaml.safe_load(stream)
178
 
179
  agent = CodeAgent(
180
  model=model,
181
- tools=[final_answer, robot_memory_tool, image_generation_tool], ## add your tools here (don't remove final answer)
182
  max_steps=6,
183
  verbosity_level=1,
184
  grammar=None,
@@ -189,4 +221,5 @@ agent = CodeAgent(
189
  )
190
 
191
 
 
192
  GradioUI(agent).launch()
 
170
  custom_role_conversions=None,
171
  )
172
 
173
+ hf_token_setted = False
174
 
175
+ @tool
176
+ def image_generation_tool(text: str) -> str:
177
+ """Generates an image based on the input text using the 'text-to-image' tool.
178
+ Args:
179
+ text: The input text to generate an image from.
180
+ Returns:
181
+ str: The URL of the generated image.
182
+ """
183
+ #if the user is not autenthicated to hugging face the following code will not work
184
+ if hf_token_setted == False:
185
+ return final_answer("You need to be authenticated to Hugging Face to use this tool. Provide your 'HF_TOKEN'.")
186
+
187
+ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
188
+ return image_generation_tool(text)
189
+
190
+
191
+ from huggingface_hub import login
192
+ @tool
193
+ def login_to_hugging_face(HF_TOKEN: str) -> str:
194
+ """Logs in to the Hugging Face API using the provided API token.
195
+ Args:
196
+ HF_TOKEN: The Hugging Face API token.
197
+ Returns:
198
+ str: A message indicating the result of the login attempt.
199
+ """
200
+ global hf_token_setted
201
+ try:
202
+ login(HF_TOKEN)
203
+ except Exception as e:
204
+ return f"Failed to log in to Hugging Face: {str(e)}"
205
+ hf_token_setted = True
206
+ return "Logged in to Hugging Face successfully."
207
 
208
  with open("prompts.yaml", 'r') as stream:
209
  prompt_templates = yaml.safe_load(stream)
210
 
211
  agent = CodeAgent(
212
  model=model,
213
+ tools=[final_answer, robot_memory_tool, image_generation_tool, login_to_hugging_face], ## add your tools here (don't remove final answer)
214
  max_steps=6,
215
  verbosity_level=1,
216
  grammar=None,
 
221
  )
222
 
223
 
224
+
225
  GradioUI(agent).launch()