MatheusHRV commited on
Commit
0b211f9
·
verified ·
1 Parent(s): b5ae24a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -8
app.py CHANGED
@@ -2,35 +2,60 @@ import streamlit as st
2
  from langchain.chat_models import HuggingFaceHub
3
  from langchain.schema import AIMessage, HumanMessage, SystemMessage
4
 
5
- # StreamLit UI
 
 
6
  st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
7
  st.header("MHRV Chatbot")
8
 
9
  if "sessionMessages" not in st.session_state:
10
  st.session_state.sessionMessages = [
11
- SystemMessage(content="You are customer support chatbot from a website.")
12
  ]
13
 
14
- # Load HuggingFace model
 
 
15
  chat = HuggingFaceHub(
16
  repo_id="OpenAssistant/oasst-sft-4-pythia-12b",
17
  model_kwargs={"temperature": 0, "max_new_tokens": 512}
18
  )
19
 
 
 
 
20
  def load_answer(question):
 
21
  st.session_state.sessionMessages.append(HumanMessage(content=question))
22
- assistant_answer = chat(st.session_state.sessionMessages)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
 
24
  return assistant_answer.content
25
 
26
  def get_text():
27
- input_text = st.text_input("You: ", key="input")
28
- return input_text
29
 
 
 
 
30
  user_input = get_text()
31
- submit = st.button('Generate')
32
 
33
  if submit and user_input:
34
  response = load_answer(user_input)
35
  st.subheader("Answer:")
36
- st.write(response, key=1)
 
2
  from langchain.chat_models import HuggingFaceHub
3
  from langchain.schema import AIMessage, HumanMessage, SystemMessage
4
 
5
+ # ------------------------
6
+ # Streamlit UI
7
+ # ------------------------
8
  st.set_page_config(page_title="LangChain Demo", page_icon=":robot:")
9
  st.header("MHRV Chatbot")
10
 
11
  if "sessionMessages" not in st.session_state:
12
  st.session_state.sessionMessages = [
13
+ SystemMessage(content="You are a customer support chatbot for a website.")
14
  ]
15
 
16
+ # ------------------------
17
+ # Load Hugging Face Model
18
+ # ------------------------
19
  chat = HuggingFaceHub(
20
  repo_id="OpenAssistant/oasst-sft-4-pythia-12b",
21
  model_kwargs={"temperature": 0, "max_new_tokens": 512}
22
  )
23
 
24
+ # ------------------------
25
+ # Helper Functions
26
+ # ------------------------
27
  def load_answer(question):
28
+ # Add human message
29
  st.session_state.sessionMessages.append(HumanMessage(content=question))
30
+
31
+ # Convert messages to a single prompt string
32
+ prompt = ""
33
+ for msg in st.session_state.sessionMessages:
34
+ if isinstance(msg, SystemMessage):
35
+ prompt += f"System: {msg.content}\n"
36
+ elif isinstance(msg, HumanMessage):
37
+ prompt += f"Human: {msg.content}\n"
38
+ elif isinstance(msg, AIMessage):
39
+ prompt += f"AI: {msg.content}\n"
40
+
41
+ # Get AI response
42
+ assistant_answer = chat(prompt)
43
+
44
+ # Add AI message to session
45
  st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
46
+
47
  return assistant_answer.content
48
 
49
  def get_text():
50
+ return st.text_input("You: ", key="input")
 
51
 
52
+ # ------------------------
53
+ # Main App
54
+ # ------------------------
55
  user_input = get_text()
56
+ submit = st.button("Generate")
57
 
58
  if submit and user_input:
59
  response = load_answer(user_input)
60
  st.subheader("Answer:")
61
+ st.write(response)