RozzaCreat commited on
Commit
734f8a1
·
verified ·
1 Parent(s): f9517fc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -85
app.py CHANGED
@@ -1,13 +1,8 @@
1
- from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
2
- import datetime
3
- import requests
4
- import pytz
5
- import yaml
6
- import re
7
  import tempfile
8
  import os
9
  from tools.final_answer import FinalAnswerTool
10
- from Gradio_UI import GradioUI
11
 
12
  @tool
13
  def generate_passover_coloring_pages(age: int) -> str:
@@ -93,62 +88,53 @@ def generate_passover_coloring_pages(age: int) -> str:
93
  response += "\n".join(descriptions)
94
  return f"{response}\n\nError generating images: {str(e)}"
95
 
96
- # Custom UI wrapper to handle image display
97
- class CustomGradioUI(GradioUI):
98
- def __init__(self, agent):
99
- super().__init__(agent)
100
-
101
- def launch(self, **kwargs):
102
- import gradio as gr
103
-
104
- # Create a custom interface that can display images
105
- with gr.Blocks() as demo:
106
- with gr.Row():
107
- with gr.Column(scale=1):
108
- gr.Markdown("# Passover Coloring Pages Generator")
 
 
 
 
109
 
110
- with gr.Row():
111
- with gr.Column(scale=1):
112
- input_box = gr.Textbox(label="Enter child's age (1-12)")
113
- submit_btn = gr.Button("Generate Coloring Pages")
 
 
 
 
114
 
115
- with gr.Row():
116
- with gr.Column(scale=1):
117
- output_text = gr.Markdown(label="Generated Pages")
118
-
119
- with gr.Row():
120
- gallery = gr.Gallery(label="Coloring Pages", show_label=True)
121
 
122
- def process_input(input_text):
123
- try:
124
- # Try to convert the input to an integer
125
- age = int(input_text.strip())
126
- if not (1 <= age <= 12):
127
- return "Please enter a valid age between 1 and 12.", []
128
-
129
- # Generate the coloring pages
130
- result = generate_passover_coloring_pages(age)
131
-
132
- # Get the image paths from the temporary files
133
- image_paths = []
134
- for file in os.listdir(tempfile.gettempdir()):
135
- if file.endswith('.png'):
136
- image_paths.append(os.path.join(tempfile.gettempdir(), file))
137
-
138
- # Display the most recent images (last 4 if there are more)
139
- image_paths = sorted(image_paths, key=os.path.getmtime, reverse=True)[:4]
140
-
141
- # Return the result and image paths
142
- return result, image_paths
143
- except ValueError:
144
- return "Please enter a valid number for the child's age.", []
145
- except Exception as e:
146
- return f"Error: {str(e)}", []
147
 
148
- submit_btn.click(process_input, inputs=input_box, outputs=[output_text, gallery])
149
-
150
- # Launch the demo
151
- demo.launch(**kwargs)
 
 
 
 
 
 
 
152
 
153
  # Modified prompt templates to handle simple age input
154
  custom_prompt_templates = {
@@ -171,32 +157,37 @@ Always use Python syntax for calling tools. After using a tool, analyze the resu
171
  "react-assistant": "{agent_response}",
172
  }
173
 
174
- # Setup agent
175
- final_answer = FinalAnswerTool()
 
176
 
177
- model = HfApiModel(
178
- max_tokens=2096,
179
- temperature=0.5,
180
- model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
181
- )
182
 
183
- # Check if custom prompts should be used or merged with existing ones
184
- try:
185
- with open("prompts.yaml", 'r') as stream:
186
- prompt_templates = yaml.safe_load(stream)
187
- # Merge the templates, giving priority to our custom ones
188
- prompt_templates.update(custom_prompt_templates)
189
- except:
190
- # If file doesn't exist or has issues, use our custom templates
191
- prompt_templates = custom_prompt_templates
192
-
193
- agent = CodeAgent(
194
- model=model,
195
- tools=[final_answer, generate_passover_coloring_pages],
196
- max_steps=6,
197
- verbosity_level=1,
198
- prompt_templates=prompt_templates
199
- )
 
 
 
 
200
 
201
- # Launch custom Gradio UI
202
- CustomGradioUI(agent).launch()
 
1
+ from smolagents import CodeAgent, HfApiModel, load_tool, tool
 
 
 
 
 
2
  import tempfile
3
  import os
4
  from tools.final_answer import FinalAnswerTool
5
+ import yaml
6
 
7
  @tool
8
  def generate_passover_coloring_pages(age: int) -> str:
 
88
  response += "\n".join(descriptions)
89
  return f"{response}\n\nError generating images: {str(e)}"
90
 
91
+ def create_gradio_interface():
92
+ import gradio as gr
93
+
94
+ # Create a custom interface that can display images
95
+ with gr.Blocks() as demo:
96
+ with gr.Row():
97
+ with gr.Column(scale=1):
98
+ gr.Markdown("# Passover Coloring Pages Generator")
99
+
100
+ with gr.Row():
101
+ with gr.Column(scale=1):
102
+ input_box = gr.Number(label="Enter child's age (1-12)", minimum=1, maximum=12, step=1)
103
+ submit_btn = gr.Button("Generate Coloring Pages")
104
+
105
+ with gr.Row():
106
+ with gr.Column(scale=1):
107
+ output_text = gr.Markdown(label="Generated Pages")
108
 
109
+ with gr.Row():
110
+ gallery = gr.Gallery(label="Coloring Pages")
111
+
112
+ def process_input(age):
113
+ try:
114
+ # Validate age
115
+ if age is None or not (1 <= age <= 12):
116
+ return "Please enter a valid age between 1 and 12.", []
117
 
118
+ # Generate the coloring pages
119
+ result = generate_passover_coloring_pages(int(age))
 
 
 
 
120
 
121
+ # Get the image paths from the temporary files
122
+ image_paths = []
123
+ for file in os.listdir(tempfile.gettempdir()):
124
+ if file.endswith('.png'):
125
+ image_paths.append(os.path.join(tempfile.gettempdir(), file))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
+ # Display the most recent images (last 4 if there are more)
128
+ image_paths = sorted(image_paths, key=os.path.getmtime, reverse=True)[:4]
129
+
130
+ # Return the result and image paths
131
+ return result, image_paths
132
+ except Exception as e:
133
+ return f"Error: {str(e)}", []
134
+
135
+ submit_btn.click(process_input, inputs=input_box, outputs=[output_text, gallery])
136
+
137
+ return demo
138
 
139
  # Modified prompt templates to handle simple age input
140
  custom_prompt_templates = {
 
157
  "react-assistant": "{agent_response}",
158
  }
159
 
160
+ def main():
161
+ # Setup agent
162
+ final_answer = FinalAnswerTool()
163
 
164
+ model = HfApiModel(
165
+ max_tokens=2096,
166
+ temperature=0.5,
167
+ model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
168
+ )
169
 
170
+ # Check if custom prompts should be used or merged with existing ones
171
+ try:
172
+ with open("prompts.yaml", 'r') as stream:
173
+ prompt_templates = yaml.safe_load(stream)
174
+ # Merge the templates, giving priority to our custom ones
175
+ prompt_templates.update(custom_prompt_templates)
176
+ except:
177
+ # If file doesn't exist or has issues, use our custom templates
178
+ prompt_templates = custom_prompt_templates
179
+
180
+ agent = CodeAgent(
181
+ model=model,
182
+ tools=[final_answer, generate_passover_coloring_pages],
183
+ max_steps=6,
184
+ verbosity_level=1,
185
+ prompt_templates=prompt_templates
186
+ )
187
+
188
+ # Create and launch Gradio interface
189
+ demo = create_gradio_interface()
190
+ demo.launch(share=False)
191
 
192
+ if __name__ == "__main__":
193
+ main()