nakas commited on
Commit
ac7d2e9
·
verified ·
1 Parent(s): 6e19f63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -100
app.py CHANGED
@@ -33,13 +33,15 @@ The code must:
33
  4. The interface should be assigned to a variable named 'demo'
34
  5. Handle errors gracefully
35
  6. Be completely self-contained in a single script
 
 
36
 
37
  Example for Gradio 3.x:
38
  ```python
39
  import gradio as gr
40
 
41
- def greet(name):
42
- return f"Hello, {name}!"
43
 
44
  with gr.Blocks() as demo:
45
  name_input = gr.Textbox(label="Your Name")
@@ -168,98 +170,44 @@ def generate_app(api_key, prompt):
168
 
169
  return code, app
170
 
171
- # Example Gradio apps for common requests
172
- EXAMPLE_APPS = {
173
- "calculator": """
174
- import gradio as gr
175
- import numpy as np
176
-
177
- def calculate(num1, num2, operation):
178
- if operation == "Add":
179
- return num1 + num2
180
- elif operation == "Subtract":
181
- return num1 - num2
182
- elif operation == "Multiply":
183
- return num1 * num2
184
- elif operation == "Divide":
185
- if num2 == 0:
186
- return "Error: Division by zero"
187
- return num1 / num2
188
- else:
189
- return "Please select an operation"
190
 
191
- with gr.Blocks() as demo:
192
- gr.Markdown("# Simple Calculator")
193
-
194
- with gr.Row():
195
- num1_input = gr.Number(label="First Number")
196
- num2_input = gr.Number(label="Second Number")
197
 
198
- op_dropdown = gr.Dropdown(
199
- ["Add", "Subtract", "Multiply", "Divide"],
200
- label="Operation"
201
- )
202
-
203
- calculate_btn = gr.Button("Calculate")
204
- result = gr.Number(label="Result")
205
-
206
- calculate_btn.click(
207
- fn=calculate,
208
- inputs=[num1_input, num2_input, op_dropdown],
209
- outputs=result
210
- )
211
- """,
212
- "image_filter": """
213
- import gradio as gr
214
- import numpy as np
215
- from PIL import Image, ImageEnhance, ImageFilter
216
-
217
- def apply_filter(image, filter_type, intensity):
218
- if image is None:
219
- return None
220
-
221
- img = Image.fromarray(image)
222
-
223
- if filter_type == "Blur":
224
- filtered = img.filter(ImageFilter.GaussianBlur(radius=intensity))
225
- elif filter_type == "Sharpen":
226
- enhancer = ImageEnhance.Sharpness(img)
227
- filtered = enhancer.enhance(1 + intensity)
228
- elif filter_type == "Brightness":
229
- enhancer = ImageEnhance.Brightness(img)
230
- filtered = enhancer.enhance(1 + intensity)
231
- elif filter_type == "Contrast":
232
- enhancer = ImageEnhance.Contrast(img)
233
- filtered = enhancer.enhance(1 + intensity)
234
- else:
235
- filtered = img
236
-
237
- return np.array(filtered)
238
-
239
- with gr.Blocks() as demo:
240
- gr.Markdown("# Image Filter App")
241
-
242
- with gr.Row():
243
- with gr.Column():
244
- input_image = gr.Image(label="Original Image")
245
- filter_type = gr.Dropdown(
246
- ["Blur", "Sharpen", "Brightness", "Contrast"],
247
- label="Filter Type",
248
- value="Blur"
249
- )
250
- intensity = gr.Slider(0, 2, 0.5, label="Intensity")
251
- apply_btn = gr.Button("Apply Filter")
252
 
253
- with gr.Column():
254
- output_image = gr.Image(label="Filtered Image")
 
 
 
 
 
 
255
 
256
- apply_btn.click(
257
- fn=apply_filter,
258
- inputs=[input_image, filter_type, intensity],
259
- outputs=output_image
260
- )
261
- """
262
- }
263
 
264
  # Create the main UI
265
  with gr.Blocks(title="AI Gradio App Generator") as demo:
@@ -292,17 +240,15 @@ with gr.Blocks(title="AI Gradio App Generator") as demo:
292
 
293
  def on_submit(api_key_input, prompt_text):
294
  try:
295
- # If it's a simple request for a common app, use our built-in example
296
  lower_prompt = prompt_text.lower()
297
- for key, example_code in EXAMPLE_APPS.items():
298
- if key in lower_prompt:
299
- try:
300
- app, app_error = load_generated_app(example_code)
301
- if app_error:
302
- return example_code, f"⚠️ {app_error}", gr.update(visible=False)
303
- return example_code, "✅ App generated successfully (from template)!", gr.update(visible=True, value=app)
304
- except Exception as e:
305
- pass # Fall back to API generation if template fails
306
 
307
  # Generate custom app via API
308
  code, result = generate_app(api_key_input, prompt_text)
 
33
  4. The interface should be assigned to a variable named 'demo'
34
  5. Handle errors gracefully
35
  6. Be completely self-contained in a single script
36
+ 7. Make sure all variables are properly defined before use
37
+ 8. Use simple input/output types that are well-supported (text, numbers, images)
38
 
39
  Example for Gradio 3.x:
40
  ```python
41
  import gradio as gr
42
 
43
+ def greet(name_input):
44
+ return f"Hello, {{name_input}}!"
45
 
46
  with gr.Blocks() as demo:
47
  name_input = gr.Textbox(label="Your Name")
 
170
 
171
  return code, app
172
 
173
+ # Simple calculator app for Gradio 3.x
174
+ def calculator_app():
175
+ def calculate(num1, num2, operation):
176
+ if operation == "Add":
177
+ return num1 + num2
178
+ elif operation == "Subtract":
179
+ return num1 - num2
180
+ elif operation == "Multiply":
181
+ return num1 * num2
182
+ elif operation == "Divide":
183
+ if num2 == 0:
184
+ return "Error: Division by zero"
185
+ return num1 / num2
186
+ else:
187
+ return "Please select an operation"
 
 
 
 
188
 
189
+ with gr.Blocks() as demo:
190
+ gr.Markdown("# Simple Calculator")
 
 
 
 
191
 
192
+ with gr.Row():
193
+ num1_input = gr.Number(label="First Number")
194
+ num2_input = gr.Number(label="Second Number")
195
+
196
+ op_dropdown = gr.Dropdown(
197
+ ["Add", "Subtract", "Multiply", "Divide"],
198
+ label="Operation"
199
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
200
 
201
+ calculate_btn = gr.Button("Calculate")
202
+ result = gr.Number(label="Result")
203
+
204
+ calculate_btn.click(
205
+ fn=calculate,
206
+ inputs=[num1_input, num2_input, op_dropdown],
207
+ outputs=result
208
+ )
209
 
210
+ return demo
 
 
 
 
 
 
211
 
212
  # Create the main UI
213
  with gr.Blocks(title="AI Gradio App Generator") as demo:
 
240
 
241
  def on_submit(api_key_input, prompt_text):
242
  try:
243
+ # If it's a simple request for a calculator app, use our built-in example
244
  lower_prompt = prompt_text.lower()
245
+ if "calculator" in lower_prompt:
246
+ try:
247
+ app = calculator_app()
248
+ code = "# Using built-in calculator template"
249
+ return code, "✅ App generated from template!", gr.update(visible=True, value=app)
250
+ except Exception as e:
251
+ pass # Fall back to API generation if template fails
 
 
252
 
253
  # Generate custom app via API
254
  code, result = generate_app(api_key_input, prompt_text)