Spaces:
Sleeping
Sleeping
Ilya
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -30,9 +30,23 @@ def chat(user_input, history):
|
|
30 |
history.append((user_input, response))
|
31 |
return history, history
|
32 |
|
33 |
-
def execute_code(code):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
old_stdout = sys.stdout
|
35 |
redirected_output = sys.stdout = io.StringIO()
|
|
|
|
|
36 |
try:
|
37 |
exec(code, {})
|
38 |
output = redirected_output.getvalue()
|
@@ -40,6 +54,7 @@ def execute_code(code):
|
|
40 |
output = f"Error: {e}\n{traceback.format_exc()}"
|
41 |
finally:
|
42 |
sys.stdout = old_stdout
|
|
|
43 |
return output
|
44 |
|
45 |
with gr.Blocks() as demo:
|
@@ -50,10 +65,12 @@ with gr.Blocks() as demo:
|
|
50 |
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
51 |
with gr.Tab("Interpreter"):
|
52 |
gr.Markdown("### π₯οΈ Test Your Code")
|
53 |
-
code_input = gr.Code(language="python")
|
|
|
|
|
54 |
run_button = gr.Button("Run Code")
|
55 |
-
code_output = gr.Textbox(label="Output")
|
56 |
-
run_button.click(execute_code, inputs=code_input, outputs=code_output)
|
57 |
with gr.Tab("Logs"):
|
58 |
gr.Markdown("### π Logs")
|
59 |
log_output = gr.Textbox(label="Logs", lines=10, interactive=False)
|
|
|
30 |
history.append((user_input, response))
|
31 |
return history, history
|
32 |
|
33 |
+
def execute_code(code, user_inputs):
|
34 |
+
# Split user inputs by newline
|
35 |
+
inputs = user_inputs.strip().split('\n')
|
36 |
+
input_iter = iter(inputs)
|
37 |
+
|
38 |
+
# Custom input function to replace built-in input()
|
39 |
+
def custom_input(prompt=''):
|
40 |
+
try:
|
41 |
+
return next(input_iter)
|
42 |
+
except StopIteration:
|
43 |
+
raise Exception("Not enough inputs provided.")
|
44 |
+
|
45 |
+
# Redirect stdout
|
46 |
old_stdout = sys.stdout
|
47 |
redirected_output = sys.stdout = io.StringIO()
|
48 |
+
old_input = __builtins__.input
|
49 |
+
__builtins__.input = custom_input
|
50 |
try:
|
51 |
exec(code, {})
|
52 |
output = redirected_output.getvalue()
|
|
|
54 |
output = f"Error: {e}\n{traceback.format_exc()}"
|
55 |
finally:
|
56 |
sys.stdout = old_stdout
|
57 |
+
__builtins__.input = old_input
|
58 |
return output
|
59 |
|
60 |
with gr.Blocks() as demo:
|
|
|
65 |
msg.submit(chat, inputs=[msg, chatbot], outputs=[chatbot, chatbot])
|
66 |
with gr.Tab("Interpreter"):
|
67 |
gr.Markdown("### π₯οΈ Test Your Code")
|
68 |
+
code_input = gr.Code(language="python", lines=20)
|
69 |
+
gr.Markdown("#### π Provide Inputs (Each input on a new line):")
|
70 |
+
user_inputs = gr.Textbox(lines=5, placeholder="Enter inputs for your code here...")
|
71 |
run_button = gr.Button("Run Code")
|
72 |
+
code_output = gr.Textbox(label="Output", lines=15)
|
73 |
+
run_button.click(execute_code, inputs=[code_input, user_inputs], outputs=code_output)
|
74 |
with gr.Tab("Logs"):
|
75 |
gr.Markdown("### π Logs")
|
76 |
log_output = gr.Textbox(label="Logs", lines=10, interactive=False)
|