File size: 945 Bytes
c31a69d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import gradio as gr
from io import StringIO
import sys

def execute_code(code: str) -> str:
    """
    Execute Python code and return the output or error message.

    Args:
        code (str): The Python code to execute.

    Returns:
        str: The output of the executed code or the error message if an exception occurs.
    """
    old_stdout = sys.stdout
    redirected_output = sys.stdout = StringIO()
    try:
        exec(code)
        return redirected_output.getvalue()
    except Exception as e:
        return str(e)
    finally:
        sys.stdout = old_stdout

# Create the Gradio interface
demo = gr.Interface(
    fn=execute_code,
    inputs=gr.Code(label="Python Code", language="python"),
    outputs=gr.Textbox(label="Output"),
    title="Python Code Executor",
    description="Execute Python code and see the output. This app is also an MCP server for LLMs."
)

if __name__ == "__main__":
    demo.launch(mcp_server=True)