-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
130 lines (112 loc) · 5.67 KB
/
app.py
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
# First
from streamlit.components.v1 import html
from streamlit_extras.add_vertical_space import add_vertical_space
import openai
# import json
import streamlit as st
import openai
import interpreter
if 'OpenAI_api_key' not in st.session_state:
st.session_state.OpenAI_api_key = ''
if "messages" not in st.session_state:
st.session_state["messages"] = []
# [{"role": "assistant", "content": "How can I help you?"}]
st.set_page_config(
page_title="Open-Interpreter Gpt App",
page_icon="🤖",
layout="wide",
initial_sidebar_state="expanded",
)
st.title("💬 Open Interpreter")
with st.sidebar:
def submit():
try:
st.session_state.OpenAI_api_key = st.session_state.widget
openai.api_key = st.session_state.OpenAI_api_key
respuesta = openai.ChatCompletion.create(
model="gpt-3.5-turbo-0613",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
]
)
interpreter.api_key = st.session_state.OpenAI_api_key
interpreter.auto_run = True
except Exception as e:
st.write(e)
st.session_state.widget = ''
st.session_state.OpenAI_api_key = ''
st.info("Please add your OpenAI API key Correctly to continue.")
st.text_input('OpenAI API Key', key='widget', on_change=submit, type="password")
add_vertical_space(29)
html_chat = '<center><h5>🤗 Soporta el proyecto con una donación para el desarrollo de nuevas Características 🤗</h5>'
st.markdown(html_chat, unsafe_allow_html=True)
button = '<script type="text/javascript" src="https://cdnjs.buymeacoffee.com/1.0.0/button.prod.min.js" data-name="bmc-button" data-slug="blazzmocompany" data-color="#FFDD00" data-emoji="" data-font="Cookie" data-text="Buy me a coffee" data-outline-color="#000000" data-font-color="#000000" data-coffee-color="#ffffff" ></script>'
html(button, height=70, width=220)
iframe = '<style>iframe[width="220"]{position: absolute; top: 50%;left: 50%;transform: translate(-50%, -50%);margin:26px 0}</style>'
st.markdown(iframe, unsafe_allow_html=True)
add_vertical_space(2)
st.write('<center><h6>Hecho con ❤️ por <a href="mailto:blazzmo.company@gmail.com">AI-Programming</a></h6>',unsafe_allow_html=True)
# st.write(st.session_state.messages)
for msg in st.session_state.messages:
if msg["role"]=="user":
st.chat_message(msg["role"]).text(msg["content"])
elif msg["role"]=="assistant":
st.chat_message(msg["role"]).markdown(msg["content"])
if prompt := st.chat_input(placeholder="Write here your message", disabled=not st.session_state.OpenAI_api_key):
# st.write(interpreter.messages)
interpreter.model = "gpt-3.5-turbo-0613"
with st.chat_message("user"):
st.text(prompt)
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("assistant"):
# Initialize variables
codeb = True
outputb = False
full_response = ""
message_placeholder = st.empty()
for chunk in interpreter.chat(prompt, display=False, stream=True):
# Message
if "message" in chunk:
full_response += chunk["message"]
if chunk['message'] == ":":
full_response += "\n"
# Code
if "code" in chunk:
# Handle code lines
if full_response.endswith("```"):
if chunk['code'].find("\n")!=-1 and codeb:
partido = full_response[:len(full_response)-3].split("```")[-1]
full_response = full_response.replace("```"+partido,"\n```\n" + partido + chunk['code'])
codeb = False
else:
full_response = full_response[:len(full_response)-3] + chunk['code'] + "```"
else:
full_response += f"```{chunk['code']}```"
# Output
if "executing" in chunk:
# Handle code execution messages
if full_response.endswith("```") and full_response[:len(full_response)-3].split("```")[-1].find("\n") != -1:
full_response = full_response[:len(full_response)-3] + "\n```"
full_response += f"\n\n```{chunk['executing']['language']}\n{chunk['executing']['code']}\n```"
if "output" in chunk:
# Handle output messages
if chunk["output"] != "KeyboardInterrupt" and outputb:
full_response = full_response[:len(full_response)-4] + chunk['output'] + "\n```\n"
elif chunk["output"] != "KeyboardInterrupt":
full_response += f"\n\n```text\n{chunk['output']}```\n"
outputb = True
codeb = True
if "end_of_execution" in chunk:
# Add a newline to separate executions
full_response = full_response.strip()
full_response += "\n"
# Join the formatted messages
# full_response += json.dumps(chunk)
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
elif not st.session_state.OpenAI_api_key:
st.info("👋 Hey , estamos muy felices por verte aqui 🤗")
st.info("👉 Coloca tu OpenAI api key, para ser capaz to correr codigo mientras lo generas 🚀")
st.error("👉 El objetivo de este proyecto es tu mostrar una facil implementacion del uso de Open Interpreter 🤗")