Spaces:
Sleeping
Sleeping
| 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) |