bnsapa commited on
Commit
ec8234a
β€’
1 Parent(s): df3e1cd
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain.agents import ConversationalChatAgent, AgentExecutor
2
+ from langchain.callbacks import StreamlitCallbackHandler
3
+ from langchain.chat_models import ChatOpenAI
4
+ from langchain import HuggingFaceHub
5
+ from langchain.memory import ConversationBufferMemory
6
+ from langchain.memory.chat_message_histories import StreamlitChatMessageHistory
7
+ from langchain.chains import LLMChain, RetrievalQA
8
+ from langchain import PromptTemplate
9
+ import streamlit as st
10
+ import os
11
+ import dotenv
12
+
13
+ dotenv.load_dotenv()
14
+
15
+ HUGGINGFACE_API = os.getenv("HUGGINGFACE_API")
16
+
17
+ st.set_page_config(page_title="ChatBot", page_icon="😊")
18
+ st.title("ChatBot")
19
+
20
+ msgs = StreamlitChatMessageHistory()
21
+ memory = ConversationBufferMemory(
22
+ chat_memory=msgs, return_messages=True, memory_key="chat_history", output_key="output"
23
+ )
24
+ if len(msgs.messages) == 0:
25
+ msgs.clear()
26
+ msgs.add_ai_message("How can I help you?")
27
+ st.session_state.steps = {}
28
+
29
+ avatars = {"human": "user", "ai": "assistant"}
30
+ for idx, msg in enumerate(msgs.messages):
31
+ with st.chat_message(avatars[msg.type]):
32
+ # Render intermediate steps if any were saved
33
+ for step in st.session_state.steps.get(str(idx), []):
34
+ if step[0].tool == "_Exception":
35
+ continue
36
+ with st.expander(f"βœ… **{step[0].tool}**: {step[0].tool_input}"):
37
+ st.write(step[0].log)
38
+ st.write(f"**{step[1]}**")
39
+ st.write(msg.content)
40
+
41
+ if prompt := st.chat_input(placeholder="Who won the Women's U.S. Open in 2018?"):
42
+ st.chat_message("user").write(prompt)
43
+
44
+ msgs.add_user_message(prompt)
45
+ llm = HuggingFaceHub(
46
+ repo_id="tiiuae/falcon-7b-instruct",
47
+ model_kwargs={"temperature": 0.5, "max_new_tokens": 500},
48
+ huggingfacehub_api_token=HUGGINGFACE_API,
49
+ )
50
+ prompt_template = PromptTemplate.from_template(
51
+ "Answer the question: {prompt}"
52
+ )
53
+ qa_chain = LLMChain(llm = llm, prompt = prompt_template)
54
+ with st.chat_message("assistant"):
55
+ st_cb = StreamlitCallbackHandler(st.container(), expand_new_thoughts=False)
56
+ response = qa_chain({"prompt": prompt})
57
+ msgs.add_ai_message(response["text"])
58
+ st.write(response["text"])