Spaces:
Sleeping
Sleeping
Update app.py
Browse files29 unmodified lines30313233343334353637383639404138394243444546474882 unmodified lines1311321331281341351361371 unmodified line13914014113614214313814429 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!# 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 "Initializing GPU and model... (this may take a moment if queued)\n\n" # Immediate feedback for visibility yield "Allocating GPU... (may queue if busy)\n\n" yield "Moving model to GPU and initializing...\n\n"
# 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) # Move to GPU here (unrestricted power move) device = torch.device('cuda') pipe.model.to(device) pipe.device = device
# Initial messages messages = [82 unmodified lines gr.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 streaming 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 output run_button.click(fn=run_agent, inputs=input_prompt, outputs=output_log)
# Launch (enable queue for streaming, explicit SSR for ZeroGPU detection fix)# Launch (disable SSR for stability, enable debug for logs)if __name__ == "__main__": demo.queue().launch(ssr_mode=True) # Explicitly enable SSR to resolve ZeroGPU glitches demo.queue().launch(ssr_mode=False, debug=True)app.py+14-8