THED1 commited on
Commit
9b2f448
·
verified ·
1 Parent(s): da81e10

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -82
app.py CHANGED
@@ -1,88 +1,10 @@
1
- import gradio as gr
2
- import subprocess
3
- import os
4
- import re # regex ke liye
5
-
6
- # ========== AI call function (sahi command daalen) ==========
7
  def ask_ai(prompt):
8
  try:
9
- # Yahan openclaw ka sahi command daalein - jo aapne help se dekha tha
10
- # Example: "openclaw ask", "openclaw chat", etc.
11
  result = subprocess.run(
12
- ["openclaw", "ask", prompt],
13
  capture_output=True,
14
- text=True,
15
- timeout=120,
16
- env={**os.environ, "OPENCLAW_DEBUG": "0"}
17
  )
18
- output = result.stdout or result.stderr
19
- return output
20
  except Exception as e:
21
- return f"AI Error: {str(e)}"
22
-
23
- # ========== Code execution function ==========
24
- def execute_code(code):
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
- # ========== Blocks interface (without theme here) ==========
45
- with gr.Blocks(title="AI + Sandbox") as demo:
46
- gr.Markdown("# 🧠 AI Chat & Code Sandbox")
47
-
48
- # ---------- Tab 1: Chat ----------
49
- with gr.Tab("Chat with AI"):
50
- gr.Markdown("OpenClaw se baat karein. Sahi command `openclaw ask` ya `openclaw chat` set karein.")
51
- chat_input = gr.Textbox(label="Aapka prompt", placeholder="Type message...")
52
- chat_output = gr.Textbox(label="AI Response", lines=10)
53
- chat_button = gr.Button("Send")
54
- chat_button.click(fn=ask_ai, inputs=chat_input, outputs=chat_output)
55
-
56
- # ---------- Tab 2: Sandbox ----------
57
- with gr.Tab("Code Sandbox"):
58
- gr.Markdown("Python code run karein. **⚠️ Safety:** Only run trusted code.")
59
- # language parameter hata diya
60
- code_input = gr.Textbox(
61
- label="Python Code",
62
- placeholder="print('Hello World')",
63
- lines=8
64
- )
65
- code_output = gr.Textbox(label="Output", lines=10)
66
- run_button = gr.Button("Run Code")
67
- run_button.click(fn=execute_code, inputs=code_input, outputs=code_output)
68
-
69
- gr.Markdown("---\n**Aur advanced: AI se code generate karke run karna**")
70
- auto_prompt = gr.Textbox(label="AI ko instruction dein (jo code generate kare)", placeholder="Write a Python script to list files")
71
- auto_generate = gr.Button("Generate & Run Code")
72
- auto_output = gr.Textbox(label="AI Response + Code Execution", lines=15)
73
-
74
- def generate_and_run(prompt):
75
- # AI se code mangaao
76
- ai_response = ask_ai(prompt + "\nReturn only the Python code inside triple backticks.")
77
- # Code block dhundo
78
- code_match = re.search(r"```(?:python)?\s*(.*?)\s*```", ai_response, re.DOTALL)
79
- if code_match:
80
- code = code_match.group(1).strip()
81
- exec_result = execute_code(code)
82
- return f"**AI Response:**\n{ai_response}\n\n**Executed Code:**\n```python\n{code}\n```\n\n**Execution Result:**\n{exec_result}"
83
- else:
84
- return f"**AI Response:**\n{ai_response}\n\n*(No code block found to execute)*"
85
- auto_generate.click(fn=generate_and_run, inputs=auto_prompt, outputs=auto_output)
86
-
87
- # Launch mein theme daalen
88
- demo.launch(server_name="0.0.0.0", server_port=7860, theme="soft")
 
 
 
 
 
 
 
1
  def ask_ai(prompt):
2
  try:
 
 
3
  result = subprocess.run(
4
+ ["openclaw", "--help"],
5
  capture_output=True,
6
+ text=True
 
 
7
  )
8
+ return result.stdout + result.stderr
 
9
  except Exception as e:
10
+ return str(e)