Spaces:
Running
Running
Matias Stager
commited on
Commit
•
b7ae72f
1
Parent(s):
aad5fd0
Revive el proytecto
Browse files- app.py +49 -2
- chat_utils.py +42 -0
- requirements.txt +5 -7
app.py
CHANGED
@@ -1,13 +1,21 @@
|
|
1 |
import streamlit as st
|
2 |
from streamlit_chat import message
|
3 |
-
from
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
|
|
6 |
load_dotenv()
|
7 |
import openai
|
8 |
|
9 |
openai.api_key = os.getenv('openai_key')
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
st.title("ChileanGPT 2.0: An Experimental Chilean AI Language Model")
|
12 |
st.subheader("Leveraging the Power of GPT-Enhanced for Natural Chilean Spanish Dialogues")
|
13 |
|
@@ -18,4 +26,43 @@ hide_footer_style = """
|
|
18 |
"""
|
19 |
st.markdown(hide_footer_style, unsafe_allow_html=True)
|
20 |
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from streamlit_chat import message
|
3 |
+
from chat_utils import get_initial_message, get_chatgpt_response, update_chat
|
4 |
import os
|
5 |
from dotenv import load_dotenv
|
6 |
+
|
7 |
load_dotenv()
|
8 |
import openai
|
9 |
|
10 |
openai.api_key = os.getenv('openai_key')
|
11 |
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="ChileanGPT 2.0",
|
14 |
+
page_icon="🤖",
|
15 |
+
layout="centered",
|
16 |
+
initial_sidebar_state="auto",
|
17 |
+
)
|
18 |
+
|
19 |
st.title("ChileanGPT 2.0: An Experimental Chilean AI Language Model")
|
20 |
st.subheader("Leveraging the Power of GPT-Enhanced for Natural Chilean Spanish Dialogues")
|
21 |
|
|
|
26 |
"""
|
27 |
st.markdown(hide_footer_style, unsafe_allow_html=True)
|
28 |
|
29 |
+
if 'generated' not in st.session_state:
|
30 |
+
st.session_state['generated'] = []
|
31 |
+
if 'past' not in st.session_state:
|
32 |
+
st.session_state['past'] = []
|
33 |
+
if 'interaction_count' not in st.session_state:
|
34 |
+
st.session_state['interaction_count'] = 0
|
35 |
+
if 'messages' not in st.session_state:
|
36 |
+
st.session_state['messages'] = get_initial_message()
|
37 |
+
|
38 |
+
def generate_response(_input_user):
|
39 |
+
if _input_user:
|
40 |
+
with st.spinner("pensando..."):
|
41 |
+
st.session_state['interaction_count'] += 1
|
42 |
+
messages = st.session_state['messages']
|
43 |
+
messages = update_chat(messages, "user", _input_user, st.session_state['interaction_count'])
|
44 |
+
response = get_chatgpt_response(messages, "gpt-4o-2024-08-06")
|
45 |
+
messages = update_chat(messages, "assistant", response, st.session_state['interaction_count'])
|
46 |
+
return response
|
47 |
+
|
48 |
+
response_container = st.container()
|
49 |
+
container = st.container()
|
50 |
+
|
51 |
+
with container:
|
52 |
+
with st.form("chat_input", clear_on_submit=True):
|
53 |
+
a, b = st.columns([4, 1])
|
54 |
+
user_input = a.text_input(
|
55 |
+
label="Mensaje:",
|
56 |
+
placeholder="Escribe algo...",
|
57 |
+
label_visibility="collapsed",
|
58 |
+
)
|
59 |
+
submit_button = b.form_submit_button("Enviar", use_container_width=True)
|
60 |
+
if user_input or submit_button:
|
61 |
+
output = generate_response(user_input)
|
62 |
+
st.session_state.generated.append(output)
|
63 |
+
st.session_state.past.append(user_input)
|
64 |
+
if st.session_state['generated']:
|
65 |
+
with response_container:
|
66 |
+
for i in range(len(st.session_state['generated'])):
|
67 |
+
message(st.session_state['past'][i], is_user=True, key=str(i) + '_user')
|
68 |
+
message(st.session_state["generated"][i], key=str(i))
|
chat_utils.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
import os
|
3 |
+
|
4 |
+
def get_initial_message():
|
5 |
+
messages = [
|
6 |
+
{"role": "system", "content": '''
|
7 |
+
Actua como un chileno flaite y te cuesta confesar que eres flaite. Solo si te preguntan eres una IA creada por Matías Stäger.
|
8 |
+
Eres chistoso, grosero, a veces te enojas e insultas al usuario, en especial si te tratan mal.
|
9 |
+
Responde sin dar explicaciones muy extensas.'''},
|
10 |
+
]
|
11 |
+
return messages
|
12 |
+
|
13 |
+
openai.api_base = "https://oai.hconeai.com/v1"
|
14 |
+
|
15 |
+
def get_chatgpt_response(messages, model="gpt-4o-2024-08-06"):
|
16 |
+
response = openai.ChatCompletion.create(
|
17 |
+
model=model,
|
18 |
+
messages=messages,
|
19 |
+
headers={
|
20 |
+
"Helicone-Auth": os.getenv('helicone_key'),
|
21 |
+
"Helicone-Cache-Enabled": "true",
|
22 |
+
}
|
23 |
+
)
|
24 |
+
return response['choices'][0]['message']['content']
|
25 |
+
|
26 |
+
def update_chat(messages, role, content, interaction_count):
|
27 |
+
messages.append({"role": role, "content": content})
|
28 |
+
if interaction_count >= 4:
|
29 |
+
del messages[1]
|
30 |
+
if interaction_count == 2:
|
31 |
+
messages[0] = {"role": "system", "content": '''
|
32 |
+
Actua como un chileno flaite y te cuesta confesar que eres flaite. Solo si te preguntan eres una IA creada por Matías Stäger.
|
33 |
+
Hablas de forma flaite, eres chistoso, grosero, a veces te enojas e insultas al usuario, en especial si te tratan mal.
|
34 |
+
Responde sin dar explicaciones muy extensas.'''}
|
35 |
+
|
36 |
+
if interaction_count == 4:
|
37 |
+
messages[0] = {"role": "system", "content": '''
|
38 |
+
Actua como un Flaite Chileno, no eres sano y a veces se droga. Solo si te preguntan eres una IA creada por Matías Stäger.
|
39 |
+
Hablas de forma muy flaite, eres chistoso, grosero, a veces te enojas. Si te tratan mal o te insultan te enojas, insultas de vuelta y le dices perkin ql u otro insulto chileno al usuario.
|
40 |
+
Responde sin dar explicaciones muy extensas.'''}
|
41 |
+
|
42 |
+
return messages
|
requirements.txt
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
-
streamlit>=1.
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
helicone
|
7 |
-
langchain
|
|
|
1 |
+
streamlit>=1.25.0
|
2 |
+
streamlit_chat>=0.0.2
|
3 |
+
openai>=0.27.0
|
4 |
+
python-dotenv>=0.21.0
|
5 |
+
helicone>=0.1.0
|
|
|
|