101TestSpace / app.py
ogegadavis254's picture
Update app.py
a9c7401 verified
raw
history blame
2.14 kB
"""
Simple Chatbot
@author: Nigel Gebodh
@email: [email protected]
"""
import streamlit as st
from openai import OpenAI
import os
from dotenv import load_dotenv
load_dotenv()
# Initialize the client
client = OpenAI(
base_url="https://api-inference.huggingface.co/v1",
api_key=os.environ.get('HUGGINGFACEHUB_API_TOKEN') # Replace with your token
)
model_link = "mistralai/Mistral-7B-Instruct-v0.2"
def reset_conversation():
"""Resets Conversation"""
st.session_state.conversation = []
st.session_state.messages = []
return None
# Set the temperature value directly in the code
temperature = 0.5
# Add a button to clear conversation
if st.button('Reset Chat'):
reset_conversation()
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
st.title("Mistral-7B Chatbot")
st.subheader("Ask me anything!")
# Display chat messages from history on app rerun
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Accept user input
prompt = st.chat_input("Type your message here...")
if prompt:
# Display user message in chat message container
with st.chat_message("user"):
st.markdown(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
# Display assistant response in chat message container
with st.chat_message("assistant"):
try:
response = client.chat.completions.create(
model=model_link,
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
temperature=temperature,
max_tokens=3000
)['choices'][0]['message']['content']
st.markdown(response)
st.session_state.messages.append({"role": "assistant", "content": response})
except Exception as e:
st.markdown("An error occurred. Please try again later.")
st.markdown(f"Error details: {e}")