Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from openai import OpenAI
|
3 |
import os
|
@@ -6,68 +12,64 @@ from dotenv import load_dotenv
|
|
6 |
load_dotenv()
|
7 |
|
8 |
# Initialize the client
|
9 |
-
client = OpenAI(
|
|
|
|
|
|
|
10 |
|
11 |
-
# Model link for Mistral
|
12 |
model_link = "mistralai/Mistral-7B-Instruct-v0.2"
|
13 |
|
14 |
def reset_conversation():
|
15 |
-
|
16 |
-
|
17 |
-
'''
|
18 |
st.session_state.messages = []
|
|
|
19 |
|
20 |
# Set the temperature value directly in the code
|
21 |
temperature = 0.5
|
22 |
|
23 |
-
# Add
|
24 |
-
st.button('Reset Chat'
|
|
|
25 |
|
26 |
# Initialize chat history
|
27 |
if "messages" not in st.session_state:
|
28 |
st.session_state.messages = []
|
29 |
|
|
|
|
|
|
|
30 |
# Display chat messages from history on app rerun
|
31 |
for message in st.session_state.messages:
|
32 |
with st.chat_message(message["role"]):
|
33 |
st.markdown(message["content"])
|
34 |
|
35 |
# Accept user input
|
36 |
-
|
37 |
|
|
|
38 |
# Display user message in chat message container
|
39 |
with st.chat_message("user"):
|
40 |
st.markdown(prompt)
|
41 |
-
|
42 |
# Add user message to chat history
|
43 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
44 |
|
45 |
-
#
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
st.markdown(assistant_response)
|
65 |
-
|
66 |
-
# Append the assistant's response to the chat history
|
67 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
68 |
-
|
69 |
-
except Exception as e:
|
70 |
-
# Display error message to user
|
71 |
-
with st.chat_message("assistant"):
|
72 |
-
st.markdown("Sorry, I couldn't process your request. Please try again later.")
|
73 |
-
st.error(f"An error occurred: {e}")
|
|
|
1 |
+
"""
|
2 |
+
Simple Chatbot
|
3 |
+
@author: Nigel Gebodh
|
4 |
+
@email: nigel.gebodh@gmail.com
|
5 |
+
"""
|
6 |
+
|
7 |
import streamlit as st
|
8 |
from openai import OpenAI
|
9 |
import os
|
|
|
12 |
load_dotenv()
|
13 |
|
14 |
# Initialize the client
|
15 |
+
client = OpenAI(
|
16 |
+
base_url="https://api-inference.huggingface.co/v1",
|
17 |
+
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Replace with your token
|
18 |
+
)
|
19 |
|
|
|
20 |
model_link = "mistralai/Mistral-7B-Instruct-v0.2"
|
21 |
|
22 |
def reset_conversation():
|
23 |
+
"""Resets Conversation"""
|
24 |
+
st.session_state.conversation = []
|
|
|
25 |
st.session_state.messages = []
|
26 |
+
return None
|
27 |
|
28 |
# Set the temperature value directly in the code
|
29 |
temperature = 0.5
|
30 |
|
31 |
+
# Add a button to clear conversation
|
32 |
+
if st.button('Reset Chat'):
|
33 |
+
reset_conversation()
|
34 |
|
35 |
# Initialize chat history
|
36 |
if "messages" not in st.session_state:
|
37 |
st.session_state.messages = []
|
38 |
|
39 |
+
st.title("Mistral-7B Chatbot")
|
40 |
+
st.subheader("Ask me anything!")
|
41 |
+
|
42 |
# Display chat messages from history on app rerun
|
43 |
for message in st.session_state.messages:
|
44 |
with st.chat_message(message["role"]):
|
45 |
st.markdown(message["content"])
|
46 |
|
47 |
# Accept user input
|
48 |
+
prompt = st.chat_input("Type your message here...")
|
49 |
|
50 |
+
if prompt:
|
51 |
# Display user message in chat message container
|
52 |
with st.chat_message("user"):
|
53 |
st.markdown(prompt)
|
|
|
54 |
# Add user message to chat history
|
55 |
st.session_state.messages.append({"role": "user", "content": prompt})
|
56 |
|
57 |
+
# Display assistant response in chat message container
|
58 |
+
with st.chat_message("assistant"):
|
59 |
+
try:
|
60 |
+
response = client.chat.completions.create(
|
61 |
+
model=model_link,
|
62 |
+
messages=[
|
63 |
+
{"role": m["role"], "content": m["content"]}
|
64 |
+
for m in st.session_state.messages
|
65 |
+
],
|
66 |
+
temperature=temperature,
|
67 |
+
max_tokens=3000
|
68 |
+
)['choices'][0]['message']['content']
|
69 |
+
|
70 |
+
st.markdown(response)
|
71 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
72 |
+
|
73 |
+
except Exception as e:
|
74 |
+
st.markdown("An error occurred. Please try again later.")
|
75 |
+
st.markdown(f"Error details: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|