wuhp commited on
Commit
b16f810
·
verified ·
1 Parent(s): 3c6a3bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -193,7 +193,8 @@ def run_agent(client, model_name, system_prompt_template, user_input_state, conf
193
  model_to_use = model_name
194
 
195
  try:
196
- # *** FIX: Use a single Content object with role="user" and combined text in a Part ***
 
197
  messages = [
198
  Content(role="user", parts=[Part(text=system_prompt + "\n\n" + user_message_content)])
199
  ]
@@ -441,26 +442,23 @@ def orchestrate_development(client, project_state, config, oauth_token_token):
441
  elif current_task.startswith('CODING'):
442
  # Ensure minimum files exist before asking CodeGen to code
443
  # This happens once at the start of the first coding task or if syntax errors occurred
444
- if project_state['attempt_count'] == 0 and project_state['current_task'] == 'CODING - Initial Implementation':
445
- # Add initial stub files if they don't exist or need regeneration due to syntax issues
446
- if project_state['main_app_file'] not in project_state['files'] or project_state.get('needs_initial_stubs', False): # Check flag if set
447
- print("Adding initial stubs for main app file...")
448
- project_state['files'][project_state['main_app_file']] = f"# Initial {project_state['sdk_choice']} app file\n" # Start with a basic stub
449
- if project_state['sdk_choice'] == 'gradio':
450
- project_state['files'][project_state['main_app_file']] += "import gradio as gr\n\n# Define a simple interface\n# For example: gr.Interface(...).launch()\n"
451
- elif project_state['sdk_choice'] == 'streamlit':
452
- project_state['files'][project_state['main_app_file']] += "import streamlit as st\n\n# Your Streamlit app starts here\n# For example: st.write('Hello, world!')\n"
453
- project_state['needs_initial_stubs'] = False # Reset flag
454
-
455
-
456
- if 'requirements.txt' not in project_state['files']:
457
- print("Adding initial requirements.txt stub...")
458
- req_content = "pandas\n" + ("streamlit\n" if project_state['sdk_choice']=="streamlit" else "gradio\n") + "google-generativeai\nhuggingface-hub\n"
459
- project_state['files']['requirements.txt'] = req_content
460
-
461
- if 'README.md' not in project_state['files']:
462
- print("Adding initial README.md stub...")
463
- readme_content = f"""---
464
  title: {project_state['repo_id']}
465
  emoji: 🐢
466
  sdk: {project_state['sdk_choice']}
@@ -477,7 +475,7 @@ This is an auto-generated HF Space.
477
  **Plan:**
478
  {project_state['plan']}
479
  """
480
- project_state['files']['README.md'] = readme_content
481
 
482
 
483
  step_successful = run_codegen(client, project_state, config)
@@ -499,10 +497,21 @@ This is an auto-generated HF Space.
499
  create_repo(repo_id=project_state['repo_id'], token=oauth_token_token,
500
  exist_ok=True, repo_type="space", space_sdk=project_state['sdk_choice'])
501
 
502
- # Write and upload all files in project_state['files']
503
- for fn, content in project_state['files'].items():
 
 
 
 
 
 
 
 
 
504
  # Ensure directories exist for files like utils/data.py
505
- os.makedirs(os.path.dirname(fn), exist_ok=True)
 
 
506
  with open(fn, "w") as f:
507
  f.write(content)
508
  upload_file(
@@ -511,7 +520,7 @@ This is an auto-generated HF Space.
511
  repo_type="space"
512
  )
513
 
514
- print(f"Pushed {len(project_state['files'])} files to {project_state['repo_id']}")
515
  project_state['status_message'] = f"Pushed code to HF Space **{project_state['repo_id']}**. Waiting for build..."
516
  project_state['chat_history'].append({"role": "assistant", "content": project_state['status_message']})
517
  project_state['current_task'] = 'LOGGING' # Move to fetching logs
 
193
  model_to_use = model_name
194
 
195
  try:
196
+ # Use a single Content object with role="user" and combined text in a Part
197
+ # The GenAI API for models like Gemini Flash Preview expects input roles to be 'user'.
198
  messages = [
199
  Content(role="user", parts=[Part(text=system_prompt + "\n\n" + user_message_content)])
200
  ]
 
442
  elif current_task.startswith('CODING'):
443
  # Ensure minimum files exist before asking CodeGen to code
444
  # This happens once at the start of the first coding task or if syntax errors occurred
445
+ # Simplify the stubbing logic - just ensure these files exist in the state before CodeGen runs
446
+ if project_state['main_app_file'] not in project_state['files']:
447
+ print(f"Adding initial stub for {project_state['main_app_file']}...")
448
+ project_state['files'][project_state['main_app_file']] = f"# Initial {project_state['sdk_choice']} app file\n" # Start with a basic stub
449
+ if project_state['sdk_choice'] == 'gradio':
450
+ project_state['files'][project_state['main_app_file']] += "import gradio as gr\n\n# Define a simple interface\n# For example: gr.Interface(...).launch()\n"
451
+ elif project_state['sdk_choice'] == 'streamlit':
452
+ project_state['files'][project_state['main_app_file']] += "import streamlit as st\n\n# Your Streamlit app starts here\n# For example: st.write('Hello, world!')\n"
453
+
454
+ if 'requirements.txt' not in project_state['files']:
455
+ print("Adding initial requirements.txt stub...")
456
+ req_content = "pandas\n" + ("streamlit\n" if project_state['sdk_choice']=="streamlit" else "gradio\n") + "google-generativeai\nhuggingface-hub\n"
457
+ project_state['files']['requirements.txt'] = req_content
458
+
459
+ if 'README.md' not in project_state['files']:
460
+ print("Adding initial README.md stub...")
461
+ readme_content = f"""---
 
 
 
462
  title: {project_state['repo_id']}
463
  emoji: 🐢
464
  sdk: {project_state['sdk_choice']}
 
475
  **Plan:**
476
  {project_state['plan']}
477
  """
478
+ project_state['files']['README.md'] = readme_content
479
 
480
 
481
  step_successful = run_codegen(client, project_state, config)
 
497
  create_repo(repo_id=project_state['repo_id'], token=oauth_token_token,
498
  exist_ok=True, repo_type="space", space_sdk=project_state['sdk_choice'])
499
 
500
+ # *** FIX: Filter out any empty string keys before iterating ***
501
+ files_to_push = {
502
+ fn: content
503
+ for fn, content in project_state['files'].items()
504
+ if fn and fn.strip() # Keep only non-empty, non-whitespace filenames
505
+ }
506
+ print(f"Attempting to push {len(files_to_push)} valid files...")
507
+
508
+
509
+ # Write and upload all valid files
510
+ for fn, content in files_to_push.items():
511
  # Ensure directories exist for files like utils/data.py
512
+ dirpath = os.path.dirname(fn)
513
+ if dirpath: # Only create dir if filename has a path component
514
+ os.makedirs(dirpath, exist_ok=True)
515
  with open(fn, "w") as f:
516
  f.write(content)
517
  upload_file(
 
520
  repo_type="space"
521
  )
522
 
523
+ print(f"Pushed {len(files_to_push)} files to {project_state['repo_id']}")
524
  project_state['status_message'] = f"Pushed code to HF Space **{project_state['repo_id']}**. Waiting for build..."
525
  project_state['chat_history'].append({"role": "assistant", "content": project_state['status_message']})
526
  project_state['current_task'] = 'LOGGING' # Move to fetching logs