informsapta commited on
Commit
7144088
·
verified ·
1 Parent(s): faf2f77

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -48
app.py CHANGED
@@ -1,70 +1,35 @@
1
- from smolagents import CodeAgent,DuckDuckGoSearchTool,HfApiModel,load_tool,tool,Tool
2
- import datetime
3
- import requests
4
- import pytz
5
- import yaml
6
  from tools.final_answer import FinalAnswerTool
7
-
8
  from Gradio_UI import GradioUI
9
-
10
- # Below is an example of a tool that does nothing. Amaze us with your creativity !
11
- @tool
12
- def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
13
- #Keep this format for the description / args / args description but feel free to modify the tool
14
- """A tool that does nothing yet
15
- Args:
16
- arg1: the first argument
17
- arg2: the second argument
18
- """
19
- return "What magic will you build ?"
20
 
21
  @tool
22
- def get_current_time_in_timezone(timezone: str) -> str:
23
- """A tool that fetches the current local time in a specified timezone.
24
- Args:
25
- timezone: A string representing a valid timezone (e.g., 'America/New_York').
26
- """
27
- try:
28
- # Create timezone object
29
- tz = pytz.timezone(timezone)
30
- # Get current time in that timezone
31
- local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
32
- return f"The current local time in {timezone} is: {local_time}"
33
- except Exception as e:
34
- return f"Error fetching time for timezone '{timezone}': {str(e)}"
35
-
36
 
37
  final_answer = FinalAnswerTool()
38
 
39
- # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
40
- # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
41
-
42
  model = HfApiModel(
43
- max_tokens=2096,
44
- temperature=0.5,
45
- #model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
46
- model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
47
- custom_role_conversions=None,
48
  )
49
 
50
-
51
- # Import tool from Hub
52
- #image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
53
  comics_generation_tool = Tool.from_space(
54
- #"jbilcke-hf/ai-comic-factory",
55
  "black-forest-labs/FLUX.1-schnell",
56
- #name="comics_generator",
57
- #description="Create your own AI comic with a single prompt"
58
  name="image_generator",
59
  description="Generate an image from a prompt"
60
  )
61
 
62
  with open("prompts.yaml", 'r') as stream:
63
  prompt_templates = yaml.safe_load(stream)
64
-
65
  agent = CodeAgent(
66
  model=model,
67
- tools=[final_answer, comics_generation_tool], ## add your tools here (don't remove final answer)
68
  max_steps=6,
69
  verbosity_level=1,
70
  grammar=None,
@@ -74,5 +39,19 @@ agent = CodeAgent(
74
  prompt_templates=prompt_templates
75
  )
76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- GradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, Tool
 
 
 
 
2
  from tools.final_answer import FinalAnswerTool
 
3
  from Gradio_UI import GradioUI
4
+ from PIL import Image
5
+ import yaml
6
+ import gradio as gr
 
 
 
 
 
 
 
 
7
 
8
  @tool
9
+ def my_custom_tool(arg1: str, arg2: int) -> str:
10
+ """A tool that does nothing yet."""
11
+ return "What magic will you build?"
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  final_answer = FinalAnswerTool()
14
 
 
 
 
15
  model = HfApiModel(
16
+ max_tokens=2096,
17
+ temperature=0.5,
18
+ model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud',
 
 
19
  )
20
 
 
 
 
21
  comics_generation_tool = Tool.from_space(
 
22
  "black-forest-labs/FLUX.1-schnell",
 
 
23
  name="image_generator",
24
  description="Generate an image from a prompt"
25
  )
26
 
27
  with open("prompts.yaml", 'r') as stream:
28
  prompt_templates = yaml.safe_load(stream)
29
+
30
  agent = CodeAgent(
31
  model=model,
32
+ tools=[final_answer, comics_generation_tool],
33
  max_steps=6,
34
  verbosity_level=1,
35
  grammar=None,
 
39
  prompt_templates=prompt_templates
40
  )
41
 
42
+ def final_answer(image_path):
43
+ return Image.open(image_path)
44
+
45
+ def generate_image(prompt):
46
+ result = agent.run(prompt)
47
+ if not result or not os.path.exists(result):
48
+ return "Failed to generate image."
49
+ return final_answer(result)
50
+
51
+ with gr.Blocks() as demo:
52
+ prompt_input = gr.Textbox(label="Enter Prompt")
53
+ image_output = gr.Image(label="Generated Image", type="pil")
54
+ generate_button = gr.Button("Generate Image")
55
+ generate_button.click(generate_image, inputs=prompt_input, outputs=image_output)
56
 
57
+ demo.launch()