Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,15 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
-
# ========== AI
|
| 6 |
def ask_ai(prompt):
|
| 7 |
try:
|
| 8 |
-
# Yahan
|
| 9 |
-
#
|
| 10 |
result = subprocess.run(
|
| 11 |
-
["openclaw", "ask", prompt],
|
| 12 |
capture_output=True,
|
| 13 |
text=True,
|
| 14 |
timeout=120,
|
|
@@ -19,9 +20,8 @@ def ask_ai(prompt):
|
|
| 19 |
except Exception as e:
|
| 20 |
return f"AI Error: {str(e)}"
|
| 21 |
|
| 22 |
-
# ========== Code
|
| 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],
|
|
@@ -41,10 +41,11 @@ def execute_code(code):
|
|
| 41 |
except Exception as e:
|
| 42 |
return f"🔥 Sandbox Error: {str(e)}"
|
| 43 |
|
| 44 |
-
# ==========
|
| 45 |
-
with gr.Blocks(title="AI + Sandbox"
|
| 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...")
|
|
@@ -52,28 +53,28 @@ with gr.Blocks(title="AI + Sandbox", theme="soft") as demo:
|
|
| 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
|
| 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 |
-
#
|
| 74 |
ai_response = ask_ai(prompt + "\nReturn only the Python code inside triple backticks.")
|
| 75 |
-
# Code
|
| 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()
|
|
@@ -83,4 +84,5 @@ with gr.Blocks(title="AI + Sandbox", theme="soft") as demo:
|
|
| 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 |
-
|
|
|
|
|
|
| 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,
|
|
|
|
| 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],
|
|
|
|
| 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...")
|
|
|
|
| 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()
|
|
|
|
| 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")
|