LEEPoRu commited on
Commit
5290cf5
1 Parent(s): 2c384a1

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +115 -0
  2. requirements.txt +7 -0
app.py ADDED
@@ -0,0 +1,115 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from streamlit_extras.let_it_rain import rain
4
+ from transformers import AutoTokenizer
5
+ from langchain.chat_models import ChatOpenAI
6
+ from langchain.schema import AIMessage,HumanMessage,SystemMessage
7
+ from langchain import PromptTemplate, LLMChain
8
+ from langchain import HuggingFacePipeline
9
+ import transformers
10
+ import torch
11
+ from huggingface_hub import login
12
+
13
+
14
+ def get_response(question):
15
+ st.session_state.sessionMessages.append(HumanMessage(content=question))
16
+
17
+ assistant_answer = chat(st.session_state.sessionMessages )
18
+
19
+ st.session_state.sessionMessages.append(AIMessage(content=assistant_answer.content))
20
+
21
+ return assistant_answer.content
22
+
23
+
24
+ def get_sentiment(user_input, llm_chain):
25
+ result = llm_chain.run(user_input)
26
+
27
+ return result.lower()
28
+
29
+
30
+ def init_llama_model():
31
+ model = "meta-llama/Llama-2-7b-chat-hf"
32
+ tokenizer = AutoTokenizer.from_pretrained(model)
33
+ pipeline = transformers.pipeline(
34
+ "text-generation",
35
+ model=model,
36
+ tokenizer=tokenizer,
37
+ torch_dtype=torch.bfloat16,
38
+ device_map="auto",
39
+ max_length=200,
40
+ do_sample=True,
41
+ top_k=10,
42
+ num_return_sequences=1,
43
+ eos_token_id=tokenizer.eos_token_id
44
+ )
45
+ llm = HuggingFacePipeline(pipeline = pipeline, model_kwargs = {'temperature':0})
46
+
47
+ template = '''Classify the text into neutral, negative, or positive. Reply with only one word: Positive, Negative, or Neutral.
48
+
49
+ Examples:
50
+ Text: You will simply love the Big variety of snacks (sweet and savoury) and you can't get wrong if you choose the place for a quick meal or coffee.
51
+ Sentiment: Positive.
52
+
53
+ Text: I got food poisoning
54
+ Sentiment: Negative.
55
+
56
+ Text: {text}
57
+ Sentiment:
58
+ '''
59
+
60
+ prompt = PromptTemplate(template=template, input_variables=["text"])
61
+ llm_chain = LLMChain(prompt=prompt, llm=llm)
62
+
63
+ return llm_chain
64
+
65
+ chat = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
66
+ llm_chain = init_llama_model()
67
+
68
+ st.set_page_config(page_title="HomeX Assistant", page_icon=":robot:")
69
+ st.header("Hey, I'm your HomeX Assistant")
70
+
71
+ if "sessionMessages" not in st.session_state:
72
+ st.session_state.sessionMessages = [SystemMessage(content="You are a helpful assistant.")]
73
+
74
+ if "messages" not in st.session_state:
75
+ st.session_state.messages = []
76
+
77
+ if user_input := st.chat_input("Say something"):
78
+ assistant_input = get_response(user_input)
79
+
80
+ # add user input to history
81
+ st.session_state.messages.append({"role": "user", "content": user_input})
82
+
83
+ # add assistant input to history
84
+ st.session_state.messages.append({"role": "assistant", "content": assistant_input})
85
+
86
+ # sentiment analysis
87
+ sentiment = get_sentiment(user_input, llm_chain)
88
+ if sentiment == "negative":
89
+ rain(
90
+ emoji="😭",
91
+ font_size=30, # the size of emoji
92
+ falling_speed=3, # speed of raining
93
+ animation_length="infinite", # for how much time the animation will happen
94
+ )
95
+ elif sentiment == "neutral":
96
+ rain(
97
+ emoji="😐",
98
+ font_size=30, # the size of emoji
99
+ falling_speed=3, # speed of raining
100
+ animation_length="infinite", # for how much time the animation will happen
101
+ )
102
+ elif sentiment == "positive":
103
+ rain(
104
+ emoji="🤩",
105
+ font_size=30, # the size of emoji
106
+ falling_speed=3, # speed of raining
107
+ animation_length="infinite", # for how much time the animation will happen
108
+ )
109
+
110
+ # display chat history
111
+ for message in st.session_state.messages:
112
+ with st.chat_message(message["role"]):
113
+ st.markdown(message["content"])
114
+
115
+
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ langchain
2
+ Openai
3
+ streamlit
4
+ streamlit_extras
5
+ torch
6
+ accelerate
7
+ transformers