101TestSpace / app.py
ogegadavis254's picture
Update app.py
0634989 verified
"""
Simple Chatbot
"""
import streamlit as st
import openai # Ensure you have the correct import
import os
from dotenv import load_dotenv
load_dotenv()
# Initialize the client
openai.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 = openai.Completion.create(
engine=model_link,
prompt=prompt,
max_tokens=3000,
temperature=temperature
)
response_content = response.choices[0].text.strip()
st.markdown(response_content)
st.session_state.messages.append({"role": "assistant", "content": response_content})
except Exception as e:
st.markdown("An error occurred. Please try again later.")
st.markdown(f"Error details: {e}")