CoolFace
Apppublic

inmind/DSlogic

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py68 linesDownload Raw Back to root
1from langchain.chains import ConversationChain2from langchain.chains.conversation.memory import ConversationBufferWindowMemory3from langchain.prompts import (4    SystemMessagePromptTemplate,5    HumanMessagePromptTemplate,6    ChatPromptTemplate,7    MessagesPlaceholder8)9import streamlit as st10from streamlit_chat import message11from utils import *12from langchain_community.llms import HuggingFaceHub13import configparser14import os15 16 17# Access HuggingFaceHub API information18huggingfacehub_api_token = os.environ['HF_API_TOKEN']19st.subheader("DSlogic")20 21if 'responses' not in st.session_state:22    st.session_state['responses'] = ["How can I assist you?"]23 24if 'requests' not in st.session_state:25    st.session_state['requests'] = []26 27 28repo_id = "mistralai/Mixtral-8x7B-Instruct-v0.1"29llm = HuggingFaceHub(huggingfacehub_api_token=huggingfacehub_api_token, 30                     repo_id=repo_id, model_kwargs={"temperature":0.5, "max_new_tokens":250})31 32if 'buffer_memory' not in st.session_state:33            st.session_state.buffer_memory=ConversationBufferWindowMemory(k=3,return_messages=True)34 35 36system_msg_template = SystemMessagePromptTemplate.from_template(template="""You are a Data Science expert and are helping data scientists. Answer the question as truthfully as possible using the provided context, 37and if the answer is not contained within the text below or in your knowledge, say 'I don't know'. Answer in steps if possible.""")38 39 40human_msg_template = HumanMessagePromptTemplate.from_template(template="{input}")41 42prompt_template = ChatPromptTemplate.from_messages([system_msg_template, MessagesPlaceholder(variable_name="history"), human_msg_template])43 44conversation = ConversationChain(memory=st.session_state.buffer_memory, prompt=prompt_template, llm=llm, verbose=True)45 46if "messages" not in st.session_state:47    st.session_state.messages = []48 49 50for message in st.session_state.messages:51    with st.chat_message(message["role"]):52        st.markdown(message["content"])53 54if prompt := st.chat_input("What is up?"):55    st.session_state.messages.append({"role": "user", "content": prompt})56    with st.chat_message("user"):57        st.markdown(prompt)58 59    with st.chat_message("assistant"):60        message_placeholder = st.empty()61        query=st.session_state.messages[-1]["content"]62        context = find_match(query)63        full_response = ""64        for response in conversation.predict(input=f"Context:'{context}' \n\n Query:'{query}'"):65            full_response += response66            message_placeholder.markdown(full_response + "▌")67        message_placeholder.markdown(full_response)68    st.session_state.messages.append({"role": "assistant", "content": full_response})