Spaces:
Build error
Build error
File size: 2,569 Bytes
4619446 ea58536 e257067 4619446 39761ea 216d87b ec7fd0d 4619446 38eb158 4619446 ec7fd0d 4619446 9f360ba ea58536 fd4912d ea58536 ec7fd0d 93f7408 4c0ee47 ec7fd0d 93f7408 ec7fd0d 93f7408 70f8721 f3e8c29 93f7408 9246cc9 a00daaa 9246cc9 a00daaa 93f7408 4df9ed4 93f7408 | 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 | import subprocess
import sys
# Install required libraries
def install_packages():
required_packages = [
"langchain",
"langchain-community",
"streamlit"
]
for package in required_packages:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
# Run package installation
install_packages()
import os
import streamlit as st
from langchain.llms import HuggingFaceHub
from langchain.schema import SystemMessage, HumanMessage, AIMessage
import dotenv
# Load environment variables from .env file
dotenv.load_dotenv()
token = os.getenv("HF_TOKEN") # Automatically retrieved from environment variables
if not token:
raise ValueError("HF_TOKEN is not set. Please configure it in your environment variables or a .env file.")
llm = HuggingFaceHub(
repo_id = "mistralai/Mistral-7B-Instruct-v0.2",
model_kwargs={"max_length": 128, "temperature": 0.3},
huggingfacehub_api_token=token,
)
# Streamlit App Functions
def init_page() -> None:
"""Initializes the Streamlit page."""
st.set_page_config(page_title="AI Chatbot")
# Display the header
st.header("AI Chatbot 🤖")
# Display the subheader
st.write("Created by Pradeep Kumar")
st.sidebar.title("Options")
def init_messages() -> None:
"""Initializes the conversation messages."""
clear_button = st.sidebar.button("Clear Conversation", key="clear")
if clear_button or "messages" not in st.session_state:
st.session_state.messages = [
SystemMessage(content="You are a helpful AI assistant. Reply in markdown format.")
]
def get_answer(llm, user_input: str) -> str:
try:
return llm(user_input)
except Exception as e:
return f"An error occurred: {str(e)}"
def main() -> None:
"""Main function for the Streamlit app."""
init_page()
init_messages()
if user_input := st.chat_input("Input your question!"):
st.session_state.messages.append(HumanMessage(content=user_input))
with st.spinner("Bot is typing ..."):
answer = get_answer(llm, user_input)
st.session_state.messages.append(AIMessage(content=answer))
messages = st.session_state.get("messages", [])
for message in messages:
if isinstance(message, AIMessage):
with st.chat_message("assistant"):
st.markdown(message.content)
elif isinstance(message, HumanMessage):
with st.chat_message("user"):
st.markdown(message.content)
if __name__ == "__main__":
main()
|