Update app.py
Browse files
app.py
CHANGED
@@ -1,206 +1,206 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline,AutoModelForSeq2SeqLM
|
3 |
-
from sqlalchemy import create_engine, Column, Integer, String, DateTime,Text
|
4 |
-
from sqlalchemy.orm import sessionmaker
|
5 |
-
from sqlalchemy.ext.declarative import declarative_base
|
6 |
-
from datetime import datetime
|
7 |
-
from sklearn.feature_extraction.text import TfidfVectorizer
|
8 |
-
from sklearn.linear_model import LogisticRegression
|
9 |
-
import torch
|
10 |
-
|
11 |
-
|
12 |
-
st.title("Simple Chatbot with persistent memory (mysql)(Flan -T5)")
|
13 |
-
|
14 |
-
|
15 |
-
# database setup mysql
|
16 |
-
|
17 |
-
Base = declarative_base()
|
18 |
-
|
19 |
-
class Conversation(Base):
|
20 |
-
__tablename__ = "conversations"
|
21 |
-
id = Column(Integer , primary_key = True ,autoincrement= True)
|
22 |
-
user_input = Column(Text, nullable = False)
|
23 |
-
chatbot_response = Column(Text, nullable = False)
|
24 |
-
timestamp = Column(DateTime, nullable = False)
|
25 |
-
# creating engine to connect to mysql
|
26 |
-
|
27 |
-
DATABASE_URL = "sqlite:///chatbot.db" # SQLite database file
|
28 |
-
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
29 |
-
Base.metadata.create_all(engine)
|
30 |
-
|
31 |
-
Session = sessionmaker(bind=engine)
|
32 |
-
|
33 |
-
|
34 |
-
# LOADING THE MODEL AND TOKENIZER
|
35 |
-
@st.cache_resource
|
36 |
-
def load_model():
|
37 |
-
model_name = "google/flan-t5-base"
|
38 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
39 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
40 |
-
return tokenizer, model
|
41 |
-
|
42 |
-
|
43 |
-
tokenizer, model = load_model()
|
44 |
-
|
45 |
-
# intent detection setup
|
46 |
-
intents = {
|
47 |
-
"greeting": ["hello", "hi", "hey", "good morning", "good afternoon"],
|
48 |
-
"farewell": ["bye", "goodbye", "see you later", "farewell"],
|
49 |
-
"general": ["how are you?", "tell me a joke", "what is the weather?","who am i?","do you recognize me?"],
|
50 |
-
"about_me": ["what is your name?","tell me about yourself", "who are you","you?","Murphy?"],
|
51 |
-
"search_web": ["search for", "find", "what is", "look up", "search the web for", "google", "find information about", "show me", "give me info on", "tell me about"]
|
52 |
-
}
|
53 |
-
all_texts = []
|
54 |
-
all_labels = []
|
55 |
-
|
56 |
-
for label, texts in intents.items():
|
57 |
-
all_texts.extend(texts)
|
58 |
-
all_labels.extend([label]*len(texts))
|
59 |
-
|
60 |
-
# vectorizer
|
61 |
-
|
62 |
-
vectorizer = TfidfVectorizer()
|
63 |
-
X = vectorizer.fit_transform(all_texts)
|
64 |
-
|
65 |
-
|
66 |
-
# classsifier
|
67 |
-
|
68 |
-
classifier = LogisticRegression()
|
69 |
-
classifier.fit(X, all_labels)
|
70 |
-
|
71 |
-
# intention detection function
|
72 |
-
def detect_intent(user_input):
|
73 |
-
user_input_vectorized = vectorizer.transform([user_input])
|
74 |
-
intent = classifier.predict(user_input_vectorized)[0]
|
75 |
-
return intent
|
76 |
-
# Few-shot examples
|
77 |
-
few_shot_examples = [
|
78 |
-
"User: What is the weather like today?",
|
79 |
-
"Chatbot: I'm sorry, I cannot provide real time information.",
|
80 |
-
"User: Tell me a joke.",
|
81 |
-
"Chatbot: Why don't scientists trust atoms? Because they make up everything!",
|
82 |
-
]
|
83 |
-
|
84 |
-
if 'conversation_history' not in st.session_state:
|
85 |
-
st.session_state.conversation_history = []
|
86 |
-
|
87 |
-
# sentiment analysis setup
|
88 |
-
sentiment_pipeline = pipeline("sentiment-analysis")
|
89 |
-
def get_sentiment(text):
|
90 |
-
result = sentiment_pipeline(text)[0]
|
91 |
-
return result["label"], result["score"]
|
92 |
-
|
93 |
-
sentiment_threshold = 0.8
|
94 |
-
|
95 |
-
|
96 |
-
# sumarization setup
|
97 |
-
@st.cache_resource
|
98 |
-
def load_summarizer():
|
99 |
-
return pipeline("summarization", model="facebook/bart-large-cnn")
|
100 |
-
|
101 |
-
summarizer = load_summarizer()
|
102 |
-
|
103 |
-
|
104 |
-
# summarizer = pipeline("summarization")
|
105 |
-
|
106 |
-
def summarize_history(history):
|
107 |
-
text = "\n".join(history)
|
108 |
-
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
|
109 |
-
return summary
|
110 |
-
|
111 |
-
user_input = st.text_input("You:", key="user_input_1")
|
112 |
-
|
113 |
-
# transformer-based intent detection setup
|
114 |
-
intent_model_name = "distilbert-base-uncased"
|
115 |
-
intent_tokenizer = AutoTokenizer.from_pretrained(intent_model_name)
|
116 |
-
intent_model = AutoModelForSequenceClassification.from_pretrained(intent_model_name, num_labels=len(intents))
|
117 |
-
|
118 |
-
# Load the current model into the GPU if available.
|
119 |
-
# Load the current model into the GPU if available.
|
120 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
121 |
-
intent_model.to(device) # Move the intent detection model to the correct device
|
122 |
-
model.to(device) # move the flan to same device
|
123 |
-
|
124 |
-
def detect_intent_transformer(user_input):
|
125 |
-
inputs = intent_tokenizer(user_input, return_tensors="pt").to(device)
|
126 |
-
with torch.no_grad():
|
127 |
-
outputs = intent_model(**inputs)
|
128 |
-
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
129 |
-
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
130 |
-
return list(intents.keys())[predicted_class]
|
131 |
-
|
132 |
-
|
133 |
-
if user_input:
|
134 |
-
session = Session()
|
135 |
-
try:
|
136 |
-
intent = detect_intent_transformer(user_input) #Use the transformer.
|
137 |
-
# FOR SENTIMENT
|
138 |
-
sentiment_label, sentiment_score = get_sentiment(user_input)
|
139 |
-
if intent == "greeting":
|
140 |
-
response = "Hello there! I'm Murphy, developed by Mr.Abhishek"
|
141 |
-
elif intent == "farewell":
|
142 |
-
response = "Goodbye!"
|
143 |
-
elif intent == "about_me":
|
144 |
-
response = "I'm Murphy, developed by Abhishek, i can learn by myself and i'm feeling good right now, seems like i am alive hehe!"
|
145 |
-
# elif intent == "search_web":
|
146 |
-
# response = "Web search is disabled."
|
147 |
-
else:
|
148 |
-
|
149 |
-
# Check if history needs summarization
|
150 |
-
if len(st.session_state.conversation_history) > 10:
|
151 |
-
try:
|
152 |
-
summary = summarize_history(st.session_state.conversation_history)
|
153 |
-
st.session_state.conversation_history = [summary]
|
154 |
-
except Exception as e:
|
155 |
-
st.write(f"Summarization failed: {e}")
|
156 |
-
# construct few-shot prompt
|
157 |
-
few_shot_context = "\n".join(few_shot_examples)
|
158 |
-
# construct contextual prompt
|
159 |
-
context = "\n".join(st.session_state.conversation_history)
|
160 |
-
prompt = f"{few_shot_context}\n{context}\nUser: {user_input}\nChatbot:"
|
161 |
-
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
162 |
-
outputs = model.generate(** inputs, max_length = 50 , pad_token_id = tokenizer.eos_token_id)
|
163 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens = True)
|
164 |
-
|
165 |
-
# modifyinh responseds on the sentiment
|
166 |
-
if sentiment_score > sentiment_threshold:
|
167 |
-
if sentiment_label == "NEGATIVE":
|
168 |
-
response = f"I sense you're feeling a bit down. {response}"
|
169 |
-
elif sentiment_label == "POSITIVE":
|
170 |
-
response = f"I'm glad you're in a good mood! {response}"
|
171 |
-
|
172 |
-
# now i am storing conversations in the database
|
173 |
-
conversation = Conversation( user_input = user_input, chatbot_response = response, timestamp = datetime.now())
|
174 |
-
session.add(conversation)
|
175 |
-
session.commit()
|
176 |
-
|
177 |
-
|
178 |
-
# displaying the response
|
179 |
-
st.write(f"You: {user_input}")
|
180 |
-
st.write(f"Murphy:{response}")
|
181 |
-
|
182 |
-
# update the conversation history
|
183 |
-
|
184 |
-
st.session_state.conversation_history.append(f"User: {user_input}")
|
185 |
-
st.session_state.conversation_history.append(f"Chatbot: {response}")
|
186 |
-
|
187 |
-
except Exception as e:
|
188 |
-
st.write(f"An error occurred: {e}")
|
189 |
-
session.rollback()
|
190 |
-
finally:
|
191 |
-
session.close()
|
192 |
-
|
193 |
-
|
194 |
-
# dispaly the conversation from history
|
195 |
-
session = Session()
|
196 |
-
try:
|
197 |
-
conversations = session.query(Conversation).all()
|
198 |
-
if conversations:
|
199 |
-
st.subheader("Conversations History")
|
200 |
-
for conv in conversations:
|
201 |
-
st.write(f"You: {conv.user_input}")
|
202 |
-
st.write(f"Murphy: {conv.chatbot_response}")
|
203 |
-
except Exception as e:
|
204 |
-
st.write(f"Error retrieving conversations: {e}")
|
205 |
-
finally:
|
206 |
session.close()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline,AutoModelForSeq2SeqLM
|
3 |
+
from sqlalchemy import create_engine, Column, Integer, String, DateTime,Text
|
4 |
+
from sqlalchemy.orm import sessionmaker
|
5 |
+
from sqlalchemy.ext.declarative import declarative_base
|
6 |
+
from datetime import datetime
|
7 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
8 |
+
from sklearn.linear_model import LogisticRegression
|
9 |
+
import torch
|
10 |
+
|
11 |
+
|
12 |
+
st.title("Simple Chatbot with persistent memory (mysql)(Flan -T5)")
|
13 |
+
|
14 |
+
|
15 |
+
# database setup mysql
|
16 |
+
|
17 |
+
Base = declarative_base()
|
18 |
+
|
19 |
+
class Conversation(Base):
|
20 |
+
__tablename__ = "conversations"
|
21 |
+
id = Column(Integer , primary_key = True ,autoincrement= True)
|
22 |
+
user_input = Column(Text, nullable = False)
|
23 |
+
chatbot_response = Column(Text, nullable = False)
|
24 |
+
timestamp = Column(DateTime, nullable = False)
|
25 |
+
# creating engine to connect to mysql
|
26 |
+
|
27 |
+
DATABASE_URL = "sqlite:///chatbot.db" # SQLite database file
|
28 |
+
engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
|
29 |
+
Base.metadata.create_all(engine)
|
30 |
+
|
31 |
+
Session = sessionmaker(bind=engine)
|
32 |
+
|
33 |
+
|
34 |
+
# LOADING THE MODEL AND TOKENIZER
|
35 |
+
@st.cache_resource
|
36 |
+
def load_model():
|
37 |
+
model_name = "google/flan-t5-base"
|
38 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
39 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
40 |
+
return tokenizer, model
|
41 |
+
|
42 |
+
|
43 |
+
tokenizer, model = load_model()
|
44 |
+
|
45 |
+
# intent detection setup
|
46 |
+
intents = {
|
47 |
+
"greeting": ["hello", "hi", "hey", "good morning", "good afternoon"],
|
48 |
+
"farewell": ["bye", "goodbye", "see you later", "farewell"],
|
49 |
+
"general": ["how are you?", "tell me a joke", "what is the weather?","who am i?","do you recognize me?"],
|
50 |
+
"about_me": ["what is your name?","tell me about yourself", "who are you","you?","Murphy?"],
|
51 |
+
"search_web": ["search for", "find", "what is", "look up", "search the web for", "google", "find information about", "show me", "give me info on", "tell me about"]
|
52 |
+
}
|
53 |
+
all_texts = []
|
54 |
+
all_labels = []
|
55 |
+
|
56 |
+
for label, texts in intents.items():
|
57 |
+
all_texts.extend(texts)
|
58 |
+
all_labels.extend([label]*len(texts))
|
59 |
+
|
60 |
+
# vectorizer
|
61 |
+
|
62 |
+
vectorizer = TfidfVectorizer()
|
63 |
+
X = vectorizer.fit_transform(all_texts)
|
64 |
+
|
65 |
+
|
66 |
+
# classsifier
|
67 |
+
|
68 |
+
classifier = LogisticRegression()
|
69 |
+
classifier.fit(X, all_labels)
|
70 |
+
|
71 |
+
# intention detection function
|
72 |
+
def detect_intent(user_input):
|
73 |
+
user_input_vectorized = vectorizer.transform([user_input])
|
74 |
+
intent = classifier.predict(user_input_vectorized)[0]
|
75 |
+
return intent
|
76 |
+
# Few-shot examples
|
77 |
+
few_shot_examples = [
|
78 |
+
"User: What is the weather like today?",
|
79 |
+
"Chatbot: I'm sorry, I cannot provide real time information.",
|
80 |
+
"User: Tell me a joke.",
|
81 |
+
"Chatbot: Why don't scientists trust atoms? Because they make up everything!",
|
82 |
+
]
|
83 |
+
|
84 |
+
if 'conversation_history' not in st.session_state:
|
85 |
+
st.session_state.conversation_history = []
|
86 |
+
|
87 |
+
# sentiment analysis setup
|
88 |
+
sentiment_pipeline = pipeline("sentiment-analysis")
|
89 |
+
def get_sentiment(text):
|
90 |
+
result = sentiment_pipeline(text)[0]
|
91 |
+
return result["label"], result["score"]
|
92 |
+
|
93 |
+
sentiment_threshold = 0.8
|
94 |
+
|
95 |
+
|
96 |
+
# sumarization setup
|
97 |
+
@st.cache_resource
|
98 |
+
def load_summarizer():
|
99 |
+
return pipeline("summarization", model="facebook/bart-large-cnn")
|
100 |
+
|
101 |
+
summarizer = load_summarizer()
|
102 |
+
|
103 |
+
|
104 |
+
# summarizer = pipeline("summarization")
|
105 |
+
|
106 |
+
def summarize_history(history):
|
107 |
+
text = "\n".join(history)
|
108 |
+
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']
|
109 |
+
return summary
|
110 |
+
|
111 |
+
user_input = st.text_input("You:", key="user_input_1")
|
112 |
+
|
113 |
+
# transformer-based intent detection setup
|
114 |
+
intent_model_name = "distilbert-base-uncased"
|
115 |
+
intent_tokenizer = AutoTokenizer.from_pretrained(intent_model_name)
|
116 |
+
intent_model = AutoModelForSequenceClassification.from_pretrained(intent_model_name, num_labels=len(intents))
|
117 |
+
|
118 |
+
# Load the current model into the GPU if available.
|
119 |
+
# Load the current model into the GPU if available.
|
120 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
121 |
+
intent_model.to(device) # Move the intent detection model to the correct device
|
122 |
+
model.to(device) # move the flan to same device
|
123 |
+
|
124 |
+
def detect_intent_transformer(user_input):
|
125 |
+
inputs = intent_tokenizer(user_input, return_tensors="pt").to(device)
|
126 |
+
with torch.no_grad():
|
127 |
+
outputs = intent_model(**inputs)
|
128 |
+
probabilities = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
129 |
+
predicted_class = torch.argmax(probabilities, dim=-1).item()
|
130 |
+
return list(intents.keys())[predicted_class]
|
131 |
+
|
132 |
+
|
133 |
+
if user_input:
|
134 |
+
session = Session()
|
135 |
+
try:
|
136 |
+
intent = detect_intent_transformer(user_input) #Use the transformer.
|
137 |
+
# FOR SENTIMENT
|
138 |
+
sentiment_label, sentiment_score = get_sentiment(user_input)
|
139 |
+
if intent == "greeting":
|
140 |
+
response = "Hello there! I'm Murphy, developed by Mr.Abhishek"
|
141 |
+
elif intent == "farewell":
|
142 |
+
response = "Goodbye!"
|
143 |
+
elif intent == "about_me":
|
144 |
+
response = "I'm Murphy, developed by Abhishek, i can learn by myself and i'm feeling good right now, seems like i am alive hehe!"
|
145 |
+
# elif intent == "search_web":
|
146 |
+
# response = "Web search is disabled."
|
147 |
+
else:
|
148 |
+
|
149 |
+
# Check if history needs summarization
|
150 |
+
if len(st.session_state.conversation_history) > 10:
|
151 |
+
try:
|
152 |
+
summary = summarize_history(st.session_state.conversation_history)
|
153 |
+
st.session_state.conversation_history = [summary]
|
154 |
+
except Exception as e:
|
155 |
+
st.write(f"Summarization failed: {e}")
|
156 |
+
# construct few-shot prompt
|
157 |
+
few_shot_context = "\n".join(few_shot_examples)
|
158 |
+
# construct contextual prompt
|
159 |
+
context = "\n".join(st.session_state.conversation_history)
|
160 |
+
prompt = f"{few_shot_context}\n{context}\nUser: {user_input}\nChatbot:"
|
161 |
+
inputs = tokenizer(user_input, return_tensors="pt").to(device)
|
162 |
+
outputs = model.generate(** inputs, max_length = 50 , pad_token_id = tokenizer.eos_token_id)
|
163 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens = True)
|
164 |
+
|
165 |
+
# modifyinh responseds on the sentiment
|
166 |
+
if sentiment_score > sentiment_threshold:
|
167 |
+
if sentiment_label == "NEGATIVE":
|
168 |
+
response = f"I sense you're feeling a bit down. {response}"
|
169 |
+
elif sentiment_label == "POSITIVE":
|
170 |
+
response = f"I'm glad you're in a good mood! {response}"
|
171 |
+
|
172 |
+
# now i am storing conversations in the database
|
173 |
+
conversation = Conversation( user_input = user_input, chatbot_response = response, timestamp = datetime.now())
|
174 |
+
session.add(conversation)
|
175 |
+
session.commit()
|
176 |
+
|
177 |
+
|
178 |
+
# displaying the response
|
179 |
+
st.write(f"You: {user_input}")
|
180 |
+
st.write(f"Murphy:{response}")
|
181 |
+
|
182 |
+
# update the conversation history
|
183 |
+
|
184 |
+
st.session_state.conversation_history.append(f"User: {user_input}")
|
185 |
+
st.session_state.conversation_history.append(f"Chatbot: {response}")
|
186 |
+
|
187 |
+
except Exception as e:
|
188 |
+
st.write(f"An error occurred: {e}")
|
189 |
+
session.rollback()
|
190 |
+
finally:
|
191 |
+
session.close()
|
192 |
+
|
193 |
+
|
194 |
+
# dispaly the conversation from history
|
195 |
+
session = Session()
|
196 |
+
try:
|
197 |
+
conversations = session.query(Conversation).all()
|
198 |
+
if conversations:
|
199 |
+
st.subheader("Conversations History")
|
200 |
+
for conv in conversations:
|
201 |
+
st.write(f"You: {conv.user_input}")
|
202 |
+
st.write(f"Murphy: {conv.chatbot_response}")
|
203 |
+
except Exception as e:
|
204 |
+
st.write(f"Error retrieving conversations: {e}")
|
205 |
+
finally:
|
206 |
session.close()
|