CoolFace
Apppublic

AIShare/OpenAIChat

sourceHugging Faceupdated 3y agoView on Hugging Face
0likes
app.py48 linesDownload Raw Back to root
1import streamlit as st2from langchain import PromptTemplate, LLMChain3from langchain.memory import StreamlitChatMessageHistory4from streamlit_chat import message5import numpy as np6from langchain.chains import LLMChain7from langchain.prompts import PromptTemplate8from langchain.memory import ConversationBufferMemory9from langchain.memory.chat_message_histories import StreamlitChatMessageHistory10from streamlit.components.v1 import html11from langchain import HuggingFaceHub12 13import os14from dotenv import load_dotenv15load_dotenv()16 17st.set_page_config(page_title="Open AI Chat Assistant", layout="wide")18st.subheader("Open AI Chat Assistant: Life Enhancing with AI!")19 20css_file = "main.css"21with open(css_file) as f:22    st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)23 24HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')25repo_id = os.environ.get('repo_id')26 27llm = HuggingFaceHub(repo_id=repo_id,28                     model_kwargs={"min_length":100,29                                   "max_new_tokens":1024, "do_sample":True,30                                   "temperature":0.1,31                                   "top_k":50,32                                   "top_p":0.95, "eos_token_id":49155})33 34prompt_template = """You are a very helpful AI assistant. Please response to the user's input question with as many details as possible.35Question: {user_question}36Helpufl AI AI Repsonse:37"""  38llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template))39 40user_query = st.text_input("Enter your query here:")41with st.spinner("AI Thinking...Please wait a while to Cheers!"):42    if user_query != "":43        initial_response=llm_chain.run(user_query)44        temp_ai_response_1=initial_response.partition('<|end|>\n<|user|>\n')[0]45        temp_ai_response_2=temp_ai_response_1.replace('<|end|>\n<|assistant|>\n', '') 46        final_ai_response=temp_ai_response_2.replace('<|end|>\n<|system|>\n', '')         47        st.write("AI Response:")48        st.write(final_ai_response)