File size: 5,885 Bytes
72d5e51
 
 
 
 
 
 
12af378
72d5e51
 
 
 
 
 
 
 
 
 
 
 
 
 
12af378
 
72d5e51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import streamlit as st
from interpreter import interpreter
import os

# Page configuration
st.set_page_config(page_title="AutoInterpreter", layout="wide")


# Initialize session state for settings if not exists
if "settings" not in st.session_state:
    st.session_state.settings = {
        "api_key": os.getenv("HF_API_KEY", ""),
        "api_base": "/static-proxy?url=https%3A%2F%2Fapi-inference.huggingface.co%2Fmodels%2FQwen%2FQwen2.5-72B-Instruct%26quot%3B%3C%2Fspan%3E%2C
        "model": "huggingface/Qwen/Qwen2.5-72B-Instruct",
        "auto_run": True,
        "context_window": 8000,
        "max_tokens": 4000
    }

# Create header with title and settings button
col1, col2 = st.columns([0.9, 0.1])
with col1:
    st.markdown("# Autointerpreter")
    st.markdown("Run Any Code. The Final AI Coding Experience.")
with col2:
    settings_button = st.button("โš™๏ธ", help="Settings")

# Settings modal
if settings_button:
    settings_modal = st.container()
    with settings_modal:
        st.markdown("### Settings")
        cols = st.columns(2)
        
        with cols[0]:
            # API Settings
            st.text_input(
                "API Key", 
                value=st.session_state.settings["api_key"],
                type="password",
                key="api_key",
                on_change=lambda: st.session_state.settings.update({"api_key": st.session_state.api_key})
            )
            st.text_input(
                "Model", 
                value=st.session_state.settings["model"],
                key="model",
                on_change=lambda: st.session_state.settings.update({"model": st.session_state.model})
            )
        
        with cols[1]:
            # Model Settings
            st.toggle(
                "Auto Run", 
                value=st.session_state.settings["auto_run"],
                key="auto_run",
                on_change=lambda: st.session_state.settings.update({"auto_run": st.session_state.auto_run})
            )
            st.number_input(
                "Max Tokens", 
                value=st.session_state.settings["max_tokens"],
                min_value=100,
                max_value=8000,
                key="max_tokens",
                on_change=lambda: st.session_state.settings.update({"max_tokens": st.session_state.max_tokens})
            )

# Apply settings to interpreter
interpreter.llm.api_key = st.session_state.settings["api_key"]
interpreter.llm.api_base = st.session_state.settings["api_base"]
interpreter.llm.model = st.session_state.settings["model"]
interpreter.auto_run = st.session_state.settings["auto_run"]
interpreter.context_window = st.session_state.settings["context_window"]
interpreter.max_tokens = st.session_state.settings["max_tokens"]

# Initialize messages session state
if "messages" not in st.session_state:
    st.session_state.messages = []

# Clear button
if st.button("๐Ÿ—‘๏ธ Clear", help="Clear chat"):
    interpreter.messages = []
    st.session_state.messages = []
    st.rerun()

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

# User input
user_input = st.chat_input("Enter your message:")

if user_input:
    # Display user message
    st.chat_message("user").write(user_input)
    st.session_state.messages.append({"role": "user", "content": user_input})
    
    try:
        # Create a chat message container for the assistant
        with st.chat_message("assistant"):
            response_placeholder = st.empty()
            message_buffer = []
            code_buffer = []
            
            # Stream the response
            for chunk in interpreter.chat(user_input, stream=True):
                if isinstance(chunk, dict):
                    content = chunk.get('content')
                    if content is not None and not any(skip in str(content) for skip in ["context window", "max_tokens", "<|im_end|>"]):
                        if chunk.get('type') == 'console':
                            # Accumulate code separately
                            code_buffer.append(str(content))
                            # Show complete message + current code
                            full_response = []
                            if message_buffer:
                                full_response.extend(message_buffer)
                            if code_buffer:
                                full_response.append(f"\n```python\n{''.join(code_buffer)}\n```\n")
                            response_placeholder.markdown(''.join(full_response))
                        else:
                            # Accumulate message until we have a complete thought
                            current = str(content)
                            message_buffer.append(current)
                            if '.' in current or '\n' in current or len(''.join(message_buffer)) > 80:
                                # Show complete message + current code
                                full_response = []
                                if message_buffer:
                                    full_response.extend(message_buffer)
                                if code_buffer:
                                    full_response.append(f"\n```python\n{''.join(code_buffer)}\n```\n")
                                response_placeholder.markdown(''.join(full_response))
            
            # Store the complete response
            final_response = []
            if message_buffer:
                final_response.extend(message_buffer)
            if code_buffer:
                final_response.append(f"\n```python\n{''.join(code_buffer)}\n```\n")
            
            st.session_state.messages.append({
                "role": "assistant", 
                "content": ''.join(final_response)
            })
            
    except Exception as e:
        st.error(f"Error: {str(e)}")