THED1 commited on
Commit
1216c4b
·
verified ·
1 Parent(s): 68715fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +74 -14
app.py CHANGED
@@ -1,26 +1,86 @@
1
  import gradio as gr
2
  import subprocess
 
3
 
 
4
  def ask_ai(prompt):
5
- # OpenClaw ko call karein (command adjust karein agar jaroori ho)
6
  try:
7
- # Example: "openclaw run <prompt>"
 
8
  result = subprocess.run(
9
- ["openclaw", "run", prompt],
10
  capture_output=True,
11
  text=True,
12
- timeout=120
 
13
  )
14
- return result.stdout or result.stderr
 
15
  except Exception as e:
16
- return f"Error: {str(e)}"
17
 
18
- iface = gr.Interface(
19
- fn=ask_ai,
20
- inputs=gr.Textbox(label="Prompt", placeholder="Kuchh bhi puchhiye..."),
21
- outputs=gr.Textbox(label="OpenClaw Response"),
22
- title="OpenClaw AI",
23
- description="Ye Space OpenClaw + sabhi tools ke saath ready hai."
24
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
 
26
- iface.launch(server_name="0.0.0.0", server_port=7860)
 
1
  import gradio as gr
2
  import subprocess
3
+ import os
4
 
5
+ # ========== AI ko call karne ka function (pehle jaise) ==========
6
  def ask_ai(prompt):
 
7
  try:
8
+ # Yahan aapne jo sahi command identify kari thi wo daalen (e.g., "openclaw ask")
9
+ # agar abhi pata nahi to "openclaw --help" se dekh len. Example:
10
  result = subprocess.run(
11
+ ["openclaw", "ask", prompt], # ya "chat", "generate" etc.
12
  capture_output=True,
13
  text=True,
14
+ timeout=120,
15
+ env={**os.environ, "OPENCLAW_DEBUG": "0"}
16
  )
17
+ output = result.stdout or result.stderr
18
+ return output
19
  except Exception as e:
20
+ return f"AI Error: {str(e)}"
21
 
22
+ # ========== Code Sandbox function ==========
23
+ def execute_code(code):
24
+ # Warning: ye container ke andar raw code run karta hai - sirf trusted code ke liye
25
+ try:
26
+ proc = subprocess.run(
27
+ ["python3", "-c", code],
28
+ capture_output=True,
29
+ text=True,
30
+ timeout=30,
31
+ env={**os.environ, "PYTHONUNBUFFERED": "1"}
32
+ )
33
+ stdout = proc.stdout
34
+ stderr = proc.stderr
35
+ if proc.returncode == 0:
36
+ return f"✅ Execution Success\n\nOutput:\n{stdout}"
37
+ else:
38
+ return f"❌ Error (exit code {proc.returncode})\n\n{stderr or stdout}"
39
+ except subprocess.TimeoutExpired:
40
+ return "⏰ Timeout: Code took too long (>30s)"
41
+ except Exception as e:
42
+ return f"🔥 Sandbox Error: {str(e)}"
43
+
44
+ # ========== Gradio interface (Tabs) ==========
45
+ with gr.Blocks(title="AI + Sandbox", theme="soft") as demo:
46
+ gr.Markdown("# 🧠 AI Chat & Code Sandbox")
47
+
48
+ with gr.Tab("Chat with AI"):
49
+ gr.Markdown("OpenClaw se baat karein. Sahi command `openclaw ask` ya `openclaw chat` set karein.")
50
+ chat_input = gr.Textbox(label="Aapka prompt", placeholder="Type message...")
51
+ chat_output = gr.Textbox(label="AI Response", lines=10)
52
+ chat_button = gr.Button("Send")
53
+ chat_button.click(fn=ask_ai, inputs=chat_input, outputs=chat_output)
54
+
55
+ with gr.Tab("Code Sandbox"):
56
+ gr.Markdown("Python code run karein. **⚠️ Safety:** Only run trusted code.")
57
+ code_input = gr.Textbox(
58
+ label="Python Code",
59
+ placeholder="print('Hello World')",
60
+ lines=8,
61
+ language="python"
62
+ )
63
+ code_output = gr.Textbox(label="Output", lines=10)
64
+ run_button = gr.Button("Run Code")
65
+ run_button.click(fn=execute_code, inputs=code_input, outputs=code_output)
66
+
67
+ gr.Markdown("---\n**Aur advanced: AI ke response se code extract karke run karna**")
68
+ auto_prompt = gr.Textbox(label="AI ko instruction dein (jo code generate kare)", placeholder="Write a Python script to list files")
69
+ auto_generate = gr.Button("Generate & Run Code")
70
+ auto_output = gr.Textbox(label="AI Response + Code Execution", lines=15)
71
+
72
+ def generate_and_run(prompt):
73
+ # Pehle AI se code generato karao
74
+ ai_response = ask_ai(prompt + "\nReturn only the Python code inside triple backticks.")
75
+ # Code extract karein (simple regex)
76
+ import re
77
+ code_match = re.search(r"```(?:python)?\s*(.*?)\s*```", ai_response, re.DOTALL)
78
+ if code_match:
79
+ code = code_match.group(1).strip()
80
+ exec_result = execute_code(code)
81
+ return f"**AI Response:**\n{ai_response}\n\n**Executed Code:**\n```python\n{code}\n```\n\n**Execution Result:**\n{exec_result}"
82
+ else:
83
+ return f"**AI Response:**\n{ai_response}\n\n*(No code block found to execute)*"
84
+ auto_generate.click(fn=generate_and_run, inputs=auto_prompt, outputs=auto_output)
85
 
86
+ demo.launch(server_name="0.0.0.0", server_port=7860)