binqiangliu commited on
Commit
5e0cb4e
·
1 Parent(s): 0960d30

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -0
app.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from langchain import PromptTemplate, LLMChain
3
+ from langchain.memory import StreamlitChatMessageHistory
4
+ from streamlit_chat import message
5
+ import numpy as np
6
+ from langchain.chains import LLMChain
7
+ from langchain.prompts import PromptTemplate
8
+ from langchain.memory import ConversationBufferMemory
9
+ from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
10
+ from streamlit.components.v1 import html
11
+ from langchain import HuggingFaceHub
12
+
13
+ import os
14
+ from dotenv import load_dotenv
15
+ load_dotenv()
16
+
17
+ st.set_page_config(page_title="Open AI Chat Assistant", layout="wide")
18
+ st.subheader("Open AI Chat Assistant: Life Enhancing with AI!")
19
+
20
+ css_file = "main.css"
21
+ with open(css_file) as f:
22
+ st.markdown("<style>{}</style>".format(f.read()), unsafe_allow_html=True)
23
+
24
+ HUGGINGFACEHUB_API_TOKEN = os.getenv('HUGGINGFACEHUB_API_TOKEN')
25
+ repo_id = os.environ.get('repo_id')
26
+
27
+ llm = 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
+
34
+ prompt_template = """You are a very helpful AI assistant. Please response to the user's input question with as many details as possible.
35
+ Question: {user_question}
36
+ Helpufl AI AI Repsonse:
37
+ """
38
+ llm_chain = LLMChain(llm=llm, prompt=PromptTemplate.from_template(prompt_template))
39
+
40
+ user_query = st.text_input("Enter your query here:")
41
+ with 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)