KvrParaskevi commited on
Commit
189a7a7
1 Parent(s): e2df5b9

Upload 2 files

Browse files
Files changed (2) hide show
  1. chatbot-model-huggingface.py +52 -0
  2. chatbot_bedrock.py +44 -0
chatbot-model-huggingface.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import chatbot_bedrock as demo_chat
3
+ from transformers import AutoModelForCausalLM, AutoTokenizer
4
+
5
+ st.title("Hi, I am Chatbot Philio :mermaid:")
6
+ st.write("I am your hotel booking assistant for today.")
7
+
8
+ hugging_face_key = st.sidebar.text_input('Enter Hugging Face Key to start the chat: ', type='password')
9
+
10
+ # tokenizer = AutoTokenizer.from_pretrained("KvrParaskevi/Hotel-Assistant-Attempt4-Llama-2-7b")
11
+
12
+ # [theme]
13
+ # base="light"
14
+ # primaryColor="#6b4bff"
15
+ if(hugging_face_key != ""):
16
+ model_Loading = True
17
+ if model_Loading == True:
18
+ model = demo_chat.get_Model(hugging_face_key=hugging_face_key)
19
+ model_Loading = False
20
+
21
+ #Application
22
+ with st.container():
23
+ st.markdown('<div class="scrollable-div">', unsafe_allow_html=True)
24
+ #Langchain memory in session cache
25
+ if 'memory' not in st.session_state:
26
+ st.write("Memory is initilizing ...")
27
+ st.session_state.memory = demo_chat.demo_miny_memory(model)
28
+
29
+ #Check if chat history exists in this session
30
+ if 'chat_history' not in st.session_state:
31
+ st.session_state.chat_history = [ ] #Initialize chat history
32
+
33
+ #renders chat history
34
+ for message in st.session_state.chat_history:
35
+ with st.chat_message(message["role"]):
36
+ st.write(message["content"])
37
+
38
+ #Set up input text field
39
+ input_text = st.chat_input(placeholder="Here you can chat with Llamma 2 model.")
40
+
41
+ if input_text:
42
+ with st.chat_message("user"):
43
+ st.write(input_text)
44
+ st.session_state.chat_history.append({"role" : "user", "content" : input_text}) #append message to chat history
45
+
46
+ chat_response = demo_chat.demo_chain(input_text=input_text, memory=st.session_state.memory, model= model)
47
+ first_answer = chat_response.split("Human")[0] #Because of Predict it prints the whole conversation.Here we seperate the first answer only.
48
+
49
+ with st.chat_message("assistant"):
50
+ st.write(first_answer)
51
+ st.session_state.chat_history.append({"role": "assistant", "content": first_answer})
52
+ st.markdown('</div>', unsafe_allow_html=True)
chatbot_bedrock.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from langchain import PromptTemplate, HuggingFaceHub, LLMChain
3
+ from langchain.memory import ConversationBufferMemory
4
+ from langchain.chains import ConversationChain
5
+ import langchain.globals
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer
7
+
8
+ def get_Model(hugging_face_key):
9
+ tokenizer = AutoTokenizer.from_pretrained("KvrParaskevi/Hotel-Assistant-Attempt4-Llama-2-7b",use_auth_token=hugging_face_key)
10
+ model = AutoModelForCausalLM.from_pretrained("KvrParaskevi/Hotel-Assistant-Attempt4-Llama-2-7b",use_auth_token=hugging_face_key).eval()
11
+ return model
12
+
13
+
14
+ #Write function to connect to Bedrock
15
+ # def demo_chatbot():
16
+ # # client = boto3.client('bedrock-runtime')
17
+
18
+ # template = """Question: {question}
19
+
20
+ # Answer: Let's think step by step."""
21
+ # prompt = PromptTemplate(template=template, input_variables=["question"])
22
+ # llm=HuggingFaceHub(repo_id="google/flan-t5-xl", model_kwargs={"temperature":1e-10})
23
+
24
+ # question = "When was Google founded?"
25
+
26
+ # print(llm_chain.run(question))
27
+ # return demo_llm
28
+
29
+ #test out the code with the Predicgt method
30
+ #return demo_llm.predict(input)
31
+ # = demo_chatbot('What is the temperature in Nuremberg today?')
32
+ #print(response)
33
+
34
+ def demo_miny_memory(model):
35
+ # llm_data = get_Model(hugging_face_key)
36
+ memory = ConversationBufferMemory(llm = model,max_token_limit = 512)
37
+ return memory
38
+
39
+ def demo_chain(input_text, memory,model):
40
+ # llm_data = get_Model(hugging_face_key)
41
+ llm_conversation = ConversationChain(llm=model,memory=memory,verbose=langchain.globals.get_verbose())
42
+
43
+ chat_reply = llm_conversation.predict(input=input_text)
44
+ return chat_reply