acecalisto3 commited on
Commit
61749a1
·
verified ·
1 Parent(s): 4f50677

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -11
app.py CHANGED
@@ -1,13 +1,20 @@
1
- import gradio as gr
2
  import os
3
  import subprocess
4
  import time
5
 
6
- def chat_with_code(history, user_input):
 
 
 
 
 
 
 
7
  """
8
  Handles user input and processes it through code interpreter and terminal.
9
  """
10
- history.append((user_input, None)) # Add user input to history
11
 
12
  try:
13
  # Attempt to execute code
@@ -18,11 +25,11 @@ def chat_with_code(history, user_input):
18
  # Attempt to execute terminal command
19
  output = execute_terminal(user_input)
20
 
21
- history[-1] = (user_input, output) # Update history with output
22
  except Exception as e:
23
- history[-1] = (user_input, f"Error: {e}")
24
 
25
- return history
26
 
27
  def execute_code(code):
28
  """
@@ -46,9 +53,24 @@ def execute_terminal(command):
46
  output += f"\nError: {stderr.decode('utf-8').strip()}"
47
  return output
48
 
49
- # Create Gradio interface
50
- iface = gr.ChatInterface(chat_with_code,
51
- title="Code Interpreter & Terminal Chat",
52
- description="Ask questions, write code, and run terminal commands!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- iface.launch(server_port=7861)
 
 
1
+ import streamlit as st
2
  import os
3
  import subprocess
4
  import time
5
 
6
+ st.set_page_config(page_title="Code Interpreter & Terminal Chat", page_icon="🤖")
7
+
8
+ st.title("Code Interpreter & Terminal Chat")
9
+ st.markdown("Ask questions, write code, and run terminal commands!")
10
+
11
+ chat_history = []
12
+
13
+ def chat_with_code(user_input):
14
  """
15
  Handles user input and processes it through code interpreter and terminal.
16
  """
17
+ chat_history.append((user_input, None)) # Add user input to history
18
 
19
  try:
20
  # Attempt to execute code
 
25
  # Attempt to execute terminal command
26
  output = execute_terminal(user_input)
27
 
28
+ chat_history[-1] = (user_input, output) # Update history with output
29
  except Exception as e:
30
+ chat_history[-1] = (user_input, f"Error: {e}")
31
 
32
+ return chat_history
33
 
34
  def execute_code(code):
35
  """
 
53
  output += f"\nError: {stderr.decode('utf-8').strip()}"
54
  return output
55
 
56
+ # Display chat history
57
+ for message in chat_history:
58
+ with st.chat_message(message[0]):
59
+ if message[1]:
60
+ st.write(message[1])
61
+
62
+ # User input area
63
+ user_input = st.text_input("Enter your message or code:", key="input")
64
+
65
+ # Process user input when Enter is pressed
66
+ if user_input:
67
+ chat_history = chat_with_code(user_input)
68
+
69
+ # Display updated chat history
70
+ for message in chat_history:
71
+ with st.chat_message(message[0]):
72
+ if message[1]:
73
+ st.write(message[1])
74
 
75
+ # Clear the input field
76
+ st.session_state.input = ""