Spaces:
Sleeping
Sleeping
Vasanth M
commited on
Commit
·
c31a69d
1
Parent(s):
2d20764
Added app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from io import StringIO
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
def execute_code(code: str) -> str:
|
| 6 |
+
"""
|
| 7 |
+
Execute Python code and return the output or error message.
|
| 8 |
+
|
| 9 |
+
Args:
|
| 10 |
+
code (str): The Python code to execute.
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
str: The output of the executed code or the error message if an exception occurs.
|
| 14 |
+
"""
|
| 15 |
+
old_stdout = sys.stdout
|
| 16 |
+
redirected_output = sys.stdout = StringIO()
|
| 17 |
+
try:
|
| 18 |
+
exec(code)
|
| 19 |
+
return redirected_output.getvalue()
|
| 20 |
+
except Exception as e:
|
| 21 |
+
return str(e)
|
| 22 |
+
finally:
|
| 23 |
+
sys.stdout = old_stdout
|
| 24 |
+
|
| 25 |
+
# Create the Gradio interface
|
| 26 |
+
demo = gr.Interface(
|
| 27 |
+
fn=execute_code,
|
| 28 |
+
inputs=gr.Code(label="Python Code", language="python"),
|
| 29 |
+
outputs=gr.Textbox(label="Output"),
|
| 30 |
+
title="Python Code Executor",
|
| 31 |
+
description="Execute Python code and see the output. This app is also an MCP server for LLMs."
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
demo.launch(mcp_server=True)
|