Spaces:
Sleeping
Sleeping
| # π§ Tittoo AI Agent β Final Fixed Version (OpenAI v1.0+ compatible) | |
| # β Fully working on Hugging Face Spaces (CPU / ZeroGPU) | |
| import gradio as gr | |
| import tempfile, subprocess, sys, os | |
| from openai import OpenAI | |
| # Initialize OpenAI client | |
| client = OpenAI(api_key=os.getenv("OPENAI_API_KEY")) | |
| # π§© Function: Run generated code safely in a sandbox | |
| def run_code(code): | |
| fname = tempfile.mktemp(suffix=".py") | |
| with open(fname, "w") as f: | |
| f.write(code) | |
| try: | |
| out = subprocess.check_output( | |
| [sys.executable, fname], | |
| stderr=subprocess.STDOUT, | |
| timeout=10, | |
| text=True | |
| ) | |
| except subprocess.CalledProcessError as e: | |
| out = e.output | |
| except subprocess.TimeoutExpired: | |
| out = "β³ Execution timed out." | |
| finally: | |
| os.remove(fname) | |
| return out | |
| # π§ Tittoo Agent Logic | |
| def tittoo_agent(prompt): | |
| try: | |
| response = client.chat.completions.create( | |
| model="gpt-4o-mini", | |
| messages=[ | |
| {"role": "system", "content": "You are Tittoo β an Indian multilingual coding assistant who writes, fixes, and runs Python code."}, | |
| {"role": "user", "content": prompt} | |
| ] | |
| ) | |
| code = response.choices[0].message.content | |
| result = run_code(code) | |
| return f"π§ **Generated Code:**\n```python\n{code}\n```\n\nβοΈ **Output:**\n```\n{result}\n```" | |
| except Exception as e: | |
| return f"β οΈ Error: {str(e)}" | |
| # Optional: Dummy GPU decorator (avoid runtime warnings) | |
| try: | |
| import spaces | |
| def fake_gpu(): | |
| return "GPU not required" | |
| except: | |
| pass | |
| # π¨ Gradio Interface | |
| iface = gr.Interface( | |
| fn=tittoo_agent, | |
| inputs=gr.Textbox( | |
| lines=4, | |
| placeholder="π¬ Type your prompt here (e.g. 'Write a Python program to print Fibonacci numbers')" | |
| ), | |
| outputs="markdown", | |
| title="π§ Tittoo AI Agent (Final Public Build)", | |
| description="A free Hugging Face IDE Agent that writes, fixes and runs Python code instantly. Compatible with OpenAI v1.0+ SDK.", | |
| theme="gradio/soft" | |
| ) | |
| # π Launch app | |
| iface.launch( | |
| server_name="0.0.0.0", | |
| server_port=7860, | |
| ssr_mode=False, | |
| inbrowser=False, | |
| share=True | |
| ) | |