nakas commited on
Commit
cdb6cfd
·
verified ·
1 Parent(s): f8ca66e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -97
app.py CHANGED
@@ -72,7 +72,7 @@ Create a complete, standalone Gradio application based on the user's prompt.
72
  Provide your response in the following JSON format:
73
  {
74
  "app_code": "# Python code here...",
75
- "requirements": ["gradio==3.32.0", "other_packages"],
76
  "description": "Brief description of what the app does"
77
  }
78
 
@@ -80,11 +80,11 @@ Important guidelines:
80
  1. The app_code should be a complete Gradio application
81
  2. Use ONLY gr.Interface (NOT gr.Blocks)
82
  3. Always include server_name="0.0.0.0" and server_port=7861 in the launch parameters
83
- 4. First requirement must be exactly "gradio==3.32.0"
84
- 5. Keep the app simple and focused on the user's request
85
  6. DO NOT use any flagging callbacks or flagging_dir parameters in launch
86
  7. DO NOT create or use any directories or file paths
87
- 8. Do not use features from newer Gradio versions
88
 
89
  Example:
90
  ```python
@@ -147,18 +147,10 @@ demo.launch(server_name="0.0.0.0", server_port=7861)
147
 
148
  code = code_match.group(1)
149
 
150
- # Extract requirements from imports
151
- req_pattern = r'import\s+([a-zA-Z0-9_]+)'
152
- req_matches = re.findall(req_pattern, code)
153
-
154
- requirements = ["gradio==3.32.0"]
155
- for module in req_matches:
156
- if module != "gradio" and module not in requirements and module not in ["os", "sys"]:
157
- requirements.append(module)
158
-
159
  app_info = {
160
  "app_code": code,
161
- "requirements": requirements,
162
  "description": "Generated Gradio application"
163
  }
164
 
@@ -175,44 +167,6 @@ demo.launch(server_name="0.0.0.0", server_port=7861)
175
  except Exception as e:
176
  return None, f"API call failed: {str(e)}"
177
 
178
- def install_package(package, output_queue):
179
- """Install a package and send output to the queue"""
180
- cmd = [sys.executable, "-m", "pip", "install", "--upgrade", package]
181
- print(f"Installing: {package}")
182
- output_queue.put(f"Installing: {package}\n")
183
-
184
- process = subprocess.Popen(
185
- cmd,
186
- stdout=subprocess.PIPE,
187
- stderr=subprocess.PIPE,
188
- text=True,
189
- bufsize=1
190
- )
191
-
192
- # Start threads to read output
193
- stdout_thread = threading.Thread(target=read_output, args=(process, output_queue))
194
- stderr_thread = threading.Thread(target=read_error, args=(process, output_queue))
195
-
196
- stdout_thread.daemon = True
197
- stderr_thread.daemon = True
198
-
199
- stdout_thread.start()
200
- stderr_thread.start()
201
-
202
- # Wait for the process to complete
203
- process.wait()
204
-
205
- # Wait for threads to finish
206
- stdout_thread.join()
207
- stderr_thread.join()
208
-
209
- if process.returncode == 0:
210
- output_queue.put(f"Successfully installed {package}\n")
211
- return True
212
- else:
213
- output_queue.put(f"Failed to install {package} (error code: {process.returncode})\n")
214
- return False
215
-
216
  def run_app(app_code, output_queue):
217
  """Run the app in a separate process"""
218
  global running_process, temp_files
@@ -289,8 +243,8 @@ def run_app(app_code, output_queue):
289
  with gr.Blocks(title="Gradio App Generator") as demo:
290
  gr.Markdown("# 🤖 Gradio App Generator")
291
  gr.Markdown("""
292
- This app generates a Gradio application based on your description, installs required packages,
293
- and runs it directly within this container. The generated app will be displayed below.
294
  """)
295
 
296
  with gr.Row():
@@ -365,7 +319,6 @@ with gr.Blocks(title="Gradio App Generator") as demo:
365
  )
366
 
367
  code = app_info["app_code"]
368
- requirements = app_info["requirements"]
369
 
370
  # Make sure server_name and server_port are specified
371
  if "server_name" not in code or "server_port" not in code:
@@ -374,57 +327,18 @@ with gr.Blocks(title="Gradio App Generator") as demo:
374
  else:
375
  code += "\n\ndemo.launch(server_name=\"0.0.0.0\", server_port=7861)"
376
 
377
- status_message = "⏳ Installing packages..."
378
- yield (
379
- code, status_message,
380
- "<div style='text-align:center; padding:50px;'><h3>Installing packages...</h3></div>",
381
- "Starting package installation...",
382
- gr.update(visible=False), None
383
- )
384
-
385
- # Install packages one by one with real-time updates
386
- install_results = []
387
- for i, package in enumerate(requirements):
388
- success = install_package(package, output_queue)
389
- install_results.append(success)
390
-
391
- # Update the progress output
392
- progress_text = ""
393
- while not output_queue.empty():
394
- progress_text += output_queue.get_nowait()
395
-
396
- yield (
397
- code, f"⏳ Installing packages ({i+1}/{len(requirements)})...",
398
- "<div style='text-align:center; padding:50px;'><h3>Installing packages...</h3></div>",
399
- progress_text,
400
- gr.update(visible=False), None
401
- )
402
-
403
- # Check if all packages were installed successfully
404
- if not all(install_results):
405
- progress_text = ""
406
- while not output_queue.empty():
407
- progress_text += output_queue.get_nowait()
408
-
409
- return (
410
- code, "⚠️ Some packages failed to install",
411
- "<div style='text-align:center; padding:50px;'><h3>Error installing packages</h3></div>",
412
- progress_text,
413
- gr.update(visible=False), None
414
- )
415
-
416
  status_message = "⏳ Starting the app..."
417
  yield (
418
  code, status_message,
419
  "<div style='text-align:center; padding:50px;'><h3>Starting app...</h3></div>",
420
- progress_text,
421
  gr.update(visible=False), None
422
  )
423
 
424
  # Run the app
425
  success, app_file = run_app(code, output_queue)
426
 
427
- # Update the progress output again
428
  progress_text = ""
429
  while not output_queue.empty():
430
  progress_text += output_queue.get_nowait()
@@ -507,5 +421,5 @@ with gr.Blocks(title="Gradio App Generator") as demo:
507
  )
508
 
509
  if __name__ == "__main__":
510
- # Make sure we're not trying to use flagging - for Gradio 3.50.2 compatibility
511
  demo.queue().launch(server_name="0.0.0.0", server_port=7860)
 
72
  Provide your response in the following JSON format:
73
  {
74
  "app_code": "# Python code here...",
75
+ "requirements": [],
76
  "description": "Brief description of what the app does"
77
  }
78
 
 
80
  1. The app_code should be a complete Gradio application
81
  2. Use ONLY gr.Interface (NOT gr.Blocks)
82
  3. Always include server_name="0.0.0.0" and server_port=7861 in the launch parameters
83
+ 4. DO NOT include any requirements - use ONLY packages that are already included with Gradio
84
+ 5. Keep the app simple and focused on the user's request - NO extra package dependencies
85
  6. DO NOT use any flagging callbacks or flagging_dir parameters in launch
86
  7. DO NOT create or use any directories or file paths
87
+ 8. Only use basic Python libraries and features from Gradio and its default dependencies
88
 
89
  Example:
90
  ```python
 
147
 
148
  code = code_match.group(1)
149
 
150
+ # For simplicity, we're going to avoid additional requirements
 
 
 
 
 
 
 
 
151
  app_info = {
152
  "app_code": code,
153
+ "requirements": [],
154
  "description": "Generated Gradio application"
155
  }
156
 
 
167
  except Exception as e:
168
  return None, f"API call failed: {str(e)}"
169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  def run_app(app_code, output_queue):
171
  """Run the app in a separate process"""
172
  global running_process, temp_files
 
243
  with gr.Blocks(title="Gradio App Generator") as demo:
244
  gr.Markdown("# 🤖 Gradio App Generator")
245
  gr.Markdown("""
246
+ This app generates a Gradio application based on your description,
247
+ and runs it directly with no additional dependencies. The generated app will be displayed below.
248
  """)
249
 
250
  with gr.Row():
 
319
  )
320
 
321
  code = app_info["app_code"]
 
322
 
323
  # Make sure server_name and server_port are specified
324
  if "server_name" not in code or "server_port" not in code:
 
327
  else:
328
  code += "\n\ndemo.launch(server_name=\"0.0.0.0\", server_port=7861)"
329
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  status_message = "⏳ Starting the app..."
331
  yield (
332
  code, status_message,
333
  "<div style='text-align:center; padding:50px;'><h3>Starting app...</h3></div>",
334
+ "Starting the app...",
335
  gr.update(visible=False), None
336
  )
337
 
338
  # Run the app
339
  success, app_file = run_app(code, output_queue)
340
 
341
+ # Update the progress output
342
  progress_text = ""
343
  while not output_queue.empty():
344
  progress_text += output_queue.get_nowait()
 
421
  )
422
 
423
  if __name__ == "__main__":
424
+ # Launch without any optional parameters that might cause issues
425
  demo.queue().launch(server_name="0.0.0.0", server_port=7860)