Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,136 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
demo.launch(
|
| 7 |
server_name="0.0.0.0",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import subprocess
|
| 3 |
+
import re
|
| 4 |
|
| 5 |
+
|
| 6 |
+
# =========================
|
| 7 |
+
# AI CHAT
|
| 8 |
+
# =========================
|
| 9 |
+
def ask_ai(prompt):
|
| 10 |
+
return f"AI Reply:\n\n{prompt}"
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
# =========================
|
| 14 |
+
# CODE EXECUTION
|
| 15 |
+
# =========================
|
| 16 |
+
def execute_code(code):
|
| 17 |
+
try:
|
| 18 |
+
result = subprocess.run(
|
| 19 |
+
["python3", "-c", code],
|
| 20 |
+
capture_output=True,
|
| 21 |
+
text=True,
|
| 22 |
+
timeout=30
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
if result.returncode == 0:
|
| 26 |
+
return result.stdout or "Execution completed successfully."
|
| 27 |
+
|
| 28 |
+
return result.stderr
|
| 29 |
+
|
| 30 |
+
except subprocess.TimeoutExpired:
|
| 31 |
+
return "Execution timed out."
|
| 32 |
+
|
| 33 |
+
except Exception as e:
|
| 34 |
+
return str(e)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
# =========================
|
| 38 |
+
# GENERATE + RUN
|
| 39 |
+
# =========================
|
| 40 |
+
def generate_and_run(prompt):
|
| 41 |
+
|
| 42 |
+
ai_response = ask_ai(prompt)
|
| 43 |
+
|
| 44 |
+
code_match = re.search(
|
| 45 |
+
r"```(?:python)?\s*(.*?)\s*```",
|
| 46 |
+
ai_response,
|
| 47 |
+
re.DOTALL
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if code_match:
|
| 51 |
+
|
| 52 |
+
code = code_match.group(1)
|
| 53 |
+
|
| 54 |
+
result = execute_code(code)
|
| 55 |
+
|
| 56 |
+
return (
|
| 57 |
+
f"AI Response:\n\n{ai_response}\n\n"
|
| 58 |
+
f"Execution Result:\n\n{result}"
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return ai_response
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
# =========================
|
| 65 |
+
# UI
|
| 66 |
+
# =========================
|
| 67 |
+
with gr.Blocks(
|
| 68 |
+
title="AI + Sandbox",
|
| 69 |
+
theme=gr.themes.Soft()
|
| 70 |
+
) as demo:
|
| 71 |
+
|
| 72 |
+
gr.Markdown("# 🧠 AI Chat & Code Sandbox")
|
| 73 |
+
|
| 74 |
+
with gr.Tab("Chat with AI"):
|
| 75 |
+
|
| 76 |
+
chat_input = gr.Textbox(
|
| 77 |
+
label="Prompt",
|
| 78 |
+
placeholder="Type your message..."
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
chat_output = gr.Textbox(
|
| 82 |
+
label="Response",
|
| 83 |
+
lines=10
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
chat_btn = gr.Button("Send")
|
| 87 |
+
|
| 88 |
+
chat_btn.click(
|
| 89 |
+
fn=ask_ai,
|
| 90 |
+
inputs=chat_input,
|
| 91 |
+
outputs=chat_output
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
with gr.Tab("Code Sandbox"):
|
| 95 |
+
|
| 96 |
+
code_input = gr.Textbox(
|
| 97 |
+
label="Python Code",
|
| 98 |
+
lines=10,
|
| 99 |
+
placeholder="print('hello')"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
code_output = gr.Textbox(
|
| 103 |
+
label="Output",
|
| 104 |
+
lines=10
|
| 105 |
+
)
|
| 106 |
+
|
| 107 |
+
run_btn = gr.Button("Run Code")
|
| 108 |
+
|
| 109 |
+
run_btn.click(
|
| 110 |
+
fn=execute_code,
|
| 111 |
+
inputs=code_input,
|
| 112 |
+
outputs=code_output
|
| 113 |
+
)
|
| 114 |
+
|
| 115 |
+
with gr.Tab("Generate & Run"):
|
| 116 |
+
|
| 117 |
+
auto_prompt = gr.Textbox(
|
| 118 |
+
label="Instruction",
|
| 119 |
+
placeholder="Write python code..."
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
auto_output = gr.Textbox(
|
| 123 |
+
label="Result",
|
| 124 |
+
lines=15
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
auto_btn = gr.Button("Generate & Run")
|
| 128 |
+
|
| 129 |
+
auto_btn.click(
|
| 130 |
+
fn=generate_and_run,
|
| 131 |
+
inputs=auto_prompt,
|
| 132 |
+
outputs=auto_output
|
| 133 |
+
)
|
| 134 |
|
| 135 |
demo.launch(
|
| 136 |
server_name="0.0.0.0",
|