Spaces:
Sleeping
Sleeping
Update app.py
Browse files29 unmodified lines3031323334353637383940414282 unmodified lines1251261271281291301311 unmodified line13313413513613713829 unmodified lines"Cause a deliberate error like divide by zero, then fix it in next step and print 10 / 2."]# Generator function with GPU decorator and model loading inside@spaces.GPU(duration=180) # Increased to 180s for multi-turn safety; no limits!def run_agent(user_content):yield "Initializing GPU and model... (this may take a moment if queued)\n\n" # Immediate feedback for visibility# Load model here to ensure GPU is available (avoids startup errors)pipe = pipeline("text-generation", model="xingyaoww/CodeActAgent-Mistral-7b-v0.1", device_map='auto', torch_dtype=torch.bfloat16)# Initial messagesmessages = [82 unmodified linesgr.Markdown("# Code Agent Simulator on Hugging Face Spaces\nEnter a coding task prompt, and watch the agent simulate execution in real-time.")input_prompt = gr.Textbox(label="Enter your prompt", placeholder="e.g., Implement binary search...")output_log = gr.Textbox(value="", lines=30, autoscroll=True, show_label=True, label="Simulation Log") # Switched to Textbox for reliable real-time streamingrun_button = gr.Button("Run Simulation")examples = gr.Examples(examples=prompt_list, inputs=[input_prompt])1 unmodified line# On click, run the generator and stream to outputrun_button.click(fn=run_agent, inputs=input_prompt, outputs=output_log)# Launch (enable queue for streaming, explicit SSR for ZeroGPU detection fix)if __name__ == "__main__":demo.queue().launch(ssr_mode=True) # Explicitly enable SSR to resolve ZeroGPU glitches29 unmodified lines3031323334353637383940414243444546474882 unmodified lines1311321331341351361371 unmodified line13914014114214314429 unmodified lines"Cause a deliberate error like divide by zero, then fix it in next step and print 10 / 2."]# Load model globally (CPU-safe to avoid startup CUDA errors)pipe = pipeline("text-generation", model="xingyaoww/CodeActAgent-Mistral-7b-v0.1", device_map=None, torch_dtype=torch.float16) # float16 fallback for init; no 'auto'# Generator function with GPU decorator@spaces.GPU(duration=180) # 180s for safety with multi-turn/model movedef run_agent(user_content):yield "Allocating GPU... (may queue if busy)\n\n"yield "Moving model to GPU and initializing...\n\n"# Move to GPU here (unrestricted power move)device = torch.device('cuda')pipe.model.to(device)pipe.device = device# Initial messagesmessages = [82 unmodified linesgr.Markdown("# Code Agent Simulator on Hugging Face Spaces\nEnter a coding task prompt, and watch the agent simulate execution in real-time.")input_prompt = gr.Textbox(label="Enter your prompt", placeholder="e.g., Implement binary search...")output_log = gr.Textbox(value="", lines=30, autoscroll=True, show_label=True, label="Simulation Log")run_button = gr.Button("Run Simulation")examples = gr.Examples(examples=prompt_list, inputs=[input_prompt])1 unmodified line# On click, run the generator and stream to outputrun_button.click(fn=run_agent, inputs=input_prompt, outputs=output_log)# Launch (disable SSR for stability, enable debug for logs)if __name__ == "__main__":demo.queue().launch(ssr_mode=False, debug=True)
app.py+14-8