Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,15 +31,24 @@ prompt_list = [
|
|
| 31 |
]
|
| 32 |
|
| 33 |
# Load model globally (CPU-safe to avoid startup CUDA errors)
|
| 34 |
-
pipe = pipeline("text-generation", model="xingyaoww/CodeActAgent-Mistral-7b-v0.1", device_map=None, torch_dtype=torch.float16) # float16
|
| 35 |
|
| 36 |
# Generator function with GPU decorator
|
| 37 |
-
@spaces.GPU(duration=180) # 180s for safety
|
| 38 |
def run_agent(user_content):
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
device = torch.device('cuda')
|
| 44 |
pipe.model.to(device)
|
| 45 |
pipe.device = device
|
|
@@ -60,35 +69,42 @@ def run_agent(user_content):
|
|
| 60 |
max_turns = 10
|
| 61 |
turn = 0
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
while turn < max_turns:
|
| 66 |
# Generate response
|
| 67 |
result = pipe(messages, max_new_tokens=512)
|
| 68 |
assistant_content = result[0]['generated_text'][-1]['content']
|
| 69 |
|
| 70 |
-
|
|
|
|
| 71 |
|
| 72 |
# Stop checks
|
| 73 |
if re.search(r'(task complete|done|final answer)', assistant_content.lower()):
|
| 74 |
-
|
|
|
|
| 75 |
break
|
| 76 |
|
| 77 |
# Extract <execute>
|
| 78 |
execute_match = re.search(r'<execute>(.*?)</execute>', assistant_content, re.DOTALL)
|
| 79 |
if not execute_match:
|
| 80 |
-
|
|
|
|
| 81 |
break
|
| 82 |
|
| 83 |
code = execute_match.group(1).strip()
|
|
|
|
| 84 |
|
| 85 |
# Loop detection
|
| 86 |
if code in prev_codes:
|
| 87 |
-
|
|
|
|
| 88 |
break
|
| 89 |
prev_codes.add(code)
|
| 90 |
|
| 91 |
-
|
|
|
|
| 92 |
|
| 93 |
# Exec with capture
|
| 94 |
old_stdout = sys.stdout
|
|
@@ -101,11 +117,14 @@ def run_agent(user_content):
|
|
| 101 |
finally:
|
| 102 |
sys.stdout = old_stdout
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
| 105 |
|
| 106 |
# Success stop: If output is pure digit (index), assume done
|
| 107 |
if re.match(r'^\d+$', exec_output.strip()):
|
| 108 |
-
|
|
|
|
| 109 |
break
|
| 110 |
|
| 111 |
# Append feedback
|
|
@@ -118,26 +137,36 @@ def run_agent(user_content):
|
|
| 118 |
if 'exec_output' in locals():
|
| 119 |
final_index = re.search(r'(\d+)$', exec_output)
|
| 120 |
if final_index:
|
| 121 |
-
|
|
|
|
| 122 |
else:
|
| 123 |
-
|
|
|
|
| 124 |
else:
|
| 125 |
-
|
|
|
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
|
| 129 |
# Gradio interface
|
| 130 |
with gr.Blocks(title="Code Agent Simulator") as demo:
|
| 131 |
gr.Markdown("# Code Agent Simulator on Hugging Face Spaces\nEnter a coding task prompt, and watch the agent simulate execution in real-time.")
|
| 132 |
|
| 133 |
input_prompt = gr.Textbox(label="Enter your prompt", placeholder="e.g., Implement binary search...")
|
| 134 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 135 |
run_button = gr.Button("Run Simulation")
|
| 136 |
|
| 137 |
examples = gr.Examples(examples=prompt_list, inputs=[input_prompt])
|
| 138 |
|
| 139 |
-
# On click, run the generator and stream to
|
| 140 |
-
run_button.click(fn=run_agent, inputs=input_prompt, outputs=
|
| 141 |
|
| 142 |
# Launch (disable SSR for stability, enable debug for logs)
|
| 143 |
if __name__ == "__main__":
|
|
|
|
| 31 |
]
|
| 32 |
|
| 33 |
# Load model globally (CPU-safe to avoid startup CUDA errors)
|
| 34 |
+
pipe = pipeline("text-generation", model="xingyaoww/CodeActAgent-Mistral-7b-v0.1", device_map=None, torch_dtype=torch.float16) # float16 for init; no 'auto'
|
| 35 |
|
| 36 |
# Generator function with GPU decorator
|
| 37 |
+
@spaces.GPU(duration=180) # 180s for safety
|
| 38 |
def run_agent(user_content):
|
| 39 |
+
full_log = ""
|
| 40 |
+
current_code = ""
|
| 41 |
+
current_exec_output = ""
|
| 42 |
|
| 43 |
+
yield [current_code, current_exec_output, full_log] # Initial empty
|
| 44 |
+
|
| 45 |
+
full_log += "Allocating GPU... (may queue if busy)\n\n"
|
| 46 |
+
yield [current_code, current_exec_output, full_log]
|
| 47 |
+
|
| 48 |
+
full_log += "Moving model to GPU and initializing...\n\n"
|
| 49 |
+
yield [current_code, current_exec_output, full_log]
|
| 50 |
+
|
| 51 |
+
# Move to GPU here
|
| 52 |
device = torch.device('cuda')
|
| 53 |
pipe.model.to(device)
|
| 54 |
pipe.device = device
|
|
|
|
| 69 |
max_turns = 10
|
| 70 |
turn = 0
|
| 71 |
|
| 72 |
+
full_log += f"### Starting simulation for prompt: '{user_content}'\n\n"
|
| 73 |
+
yield [current_code, current_exec_output, full_log]
|
| 74 |
|
| 75 |
while turn < max_turns:
|
| 76 |
# Generate response
|
| 77 |
result = pipe(messages, max_new_tokens=512)
|
| 78 |
assistant_content = result[0]['generated_text'][-1]['content']
|
| 79 |
|
| 80 |
+
full_log += f"**Assistant (Turn {turn+1}):** {assistant_content}\n\n"
|
| 81 |
+
yield [current_code, current_exec_output, full_log]
|
| 82 |
|
| 83 |
# Stop checks
|
| 84 |
if re.search(r'(task complete|done|final answer)', assistant_content.lower()):
|
| 85 |
+
full_log += "Detected completion keyword. Stopping.\n"
|
| 86 |
+
yield [current_code, current_exec_output, full_log]
|
| 87 |
break
|
| 88 |
|
| 89 |
# Extract <execute>
|
| 90 |
execute_match = re.search(r'<execute>(.*?)</execute>', assistant_content, re.DOTALL)
|
| 91 |
if not execute_match:
|
| 92 |
+
full_log += "No code to execute. Task likely complete.\n"
|
| 93 |
+
yield [current_code, current_exec_output, full_log]
|
| 94 |
break
|
| 95 |
|
| 96 |
code = execute_match.group(1).strip()
|
| 97 |
+
current_code = code
|
| 98 |
|
| 99 |
# Loop detection
|
| 100 |
if code in prev_codes:
|
| 101 |
+
full_log += "Repeated code detected. Possible infinite loop—stopping.\n"
|
| 102 |
+
yield [current_code, current_exec_output, full_log]
|
| 103 |
break
|
| 104 |
prev_codes.add(code)
|
| 105 |
|
| 106 |
+
full_log += f"**Executing code:**\n```\n{code}\n```\n\n"
|
| 107 |
+
yield [current_code, current_exec_output, full_log]
|
| 108 |
|
| 109 |
# Exec with capture
|
| 110 |
old_stdout = sys.stdout
|
|
|
|
| 117 |
finally:
|
| 118 |
sys.stdout = old_stdout
|
| 119 |
|
| 120 |
+
current_exec_output = exec_output
|
| 121 |
+
full_log += f"**Execution Output:** {exec_output}\n\n"
|
| 122 |
+
yield [current_code, current_exec_output, full_log]
|
| 123 |
|
| 124 |
# Success stop: If output is pure digit (index), assume done
|
| 125 |
if re.match(r'^\d+$', exec_output.strip()):
|
| 126 |
+
full_log += "Pure index output detected. Task successful—stopping.\n"
|
| 127 |
+
yield [current_code, current_exec_output, full_log]
|
| 128 |
break
|
| 129 |
|
| 130 |
# Append feedback
|
|
|
|
| 137 |
if 'exec_output' in locals():
|
| 138 |
final_index = re.search(r'(\d+)$', exec_output)
|
| 139 |
if final_index:
|
| 140 |
+
full_log += f"**Extracted Result:** Index {final_index.group(1)}\n"
|
| 141 |
+
yield [current_code, current_exec_output, full_log]
|
| 142 |
else:
|
| 143 |
+
full_log += "No clear index found—check errors.\n"
|
| 144 |
+
yield [current_code, current_exec_output, full_log]
|
| 145 |
else:
|
| 146 |
+
full_log += "No execution output.\n"
|
| 147 |
+
yield [current_code, current_exec_output, full_log]
|
| 148 |
|
| 149 |
+
full_log += f"### End of simulation for prompt: '{user_content}'\n"
|
| 150 |
+
yield [current_code, current_exec_output, full_log]
|
| 151 |
|
| 152 |
# Gradio interface
|
| 153 |
with gr.Blocks(title="Code Agent Simulator") as demo:
|
| 154 |
gr.Markdown("# Code Agent Simulator on Hugging Face Spaces\nEnter a coding task prompt, and watch the agent simulate execution in real-time.")
|
| 155 |
|
| 156 |
input_prompt = gr.Textbox(label="Enter your prompt", placeholder="e.g., Implement binary search...")
|
| 157 |
+
|
| 158 |
+
with gr.Row():
|
| 159 |
+
generated_code = gr.Code(label="Generated Code", language="python", lines=15, show_label=True)
|
| 160 |
+
exec_output = gr.Textbox(label="Execution Output", lines=15, show_label=True)
|
| 161 |
+
|
| 162 |
+
full_log = gr.Textbox(label="Full Simulation Log", lines=20, autoscroll=True, show_label=True)
|
| 163 |
+
|
| 164 |
run_button = gr.Button("Run Simulation")
|
| 165 |
|
| 166 |
examples = gr.Examples(examples=prompt_list, inputs=[input_prompt])
|
| 167 |
|
| 168 |
+
# On click, run the generator and stream to multiple outputs
|
| 169 |
+
run_button.click(fn=run_agent, inputs=input_prompt, outputs=[generated_code, exec_output, full_log])
|
| 170 |
|
| 171 |
# Launch (disable SSR for stability, enable debug for logs)
|
| 172 |
if __name__ == "__main__":
|