Draco15628 commited on
Commit
ac1e100
Β·
verified Β·
1 Parent(s): e09c0ea
Files changed (1) hide show
  1. app.py +82 -108
app.py CHANGED
@@ -48,116 +48,53 @@ question_embeddings = generate_question_embeddings()
48
  # Initialize translator
49
  translator = GoogleTranslator(source="auto", target="en")
50
 
51
- # Suggested Questions
52
- suggested_questions = ["What is AI?", "Tell me a joke!", "How does machine learning work?"]
53
-
54
- # Function to extract text from uploaded files
55
- def extract_text_from_file(uploaded_file):
56
- """Extracts text from TXT, PDF, or DOCX files."""
57
- file_extension = uploaded_file.name.split(".")[-1].lower()
58
-
59
- if file_extension == "txt":
60
- return uploaded_file.getvalue().decode("utf-8")
61
- elif file_extension == "pdf":
62
- reader = PdfReader(uploaded_file)
63
- return "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
64
- elif file_extension == "docx":
65
- doc = docx.Document(uploaded_file)
66
- return "\n".join([para.text for para in doc.paragraphs])
67
- else:
68
- return None
69
-
70
- def get_best_response(user_input):
71
- """Finds the closest matching dataset question using similarity or generates a response."""
72
- input_embedding = embedding_model.encode(user_input, convert_to_tensor=True)
73
- similarities = util.pytorch_cos_sim(input_embedding, question_embeddings)[0].cpu()
74
-
75
- best_match_idx = torch.argmax(similarities).item()
76
- best_match_score = similarities[best_match_idx].item()
77
-
78
- if best_match_score > 0.7:
79
- return answers[best_match_idx]
80
-
81
- # Generate response using BlenderBot
82
- inputs = chatbot_tokenizer(user_input, return_tensors="pt")
83
- outputs = chatbot_model.generate(**inputs)
84
- return chatbot_tokenizer.decode(outputs[0], skip_special_tokens=True)
85
-
86
- def analyze_sentiment(text):
87
- """Analyzes sentiment and returns an emoji representation."""
88
- sentiment = TextBlob(text).sentiment.polarity
89
- if sentiment > 0:
90
- return "😊 Positive"
91
- elif sentiment < 0:
92
- return "😞 Negative"
93
- else:
94
- return "😐 Neutral"
95
-
96
- def log_chat(user_input, bot_response):
97
- """Logs chat to a JSON file."""
98
- log_entry = {
99
- "timestamp": str(datetime.datetime.now()),
100
- "user_input": user_input,
101
- "bot_response": bot_response
102
- }
103
- with open("chat_log.json", "a") as log_file:
104
- json.dump(log_entry, log_file)
105
- log_file.write("\n")
106
-
107
- def text_to_speech(text):
108
- """Converts chatbot response to speech and provides download link."""
109
- tts = gTTS(text=text, lang="en")
110
- audio_file = BytesIO()
111
- tts.write_to_fp(audio_file)
112
- audio_file.seek(0)
113
- return audio_file
114
 
115
- def generate_chat_pdf():
116
- """Creates a PDF of the chat history and returns it as a downloadable file."""
117
- buffer = BytesIO()
118
- c = canvas.Canvas(buffer, pagesize=letter)
119
- width, height = letter
120
-
121
- y_position = height - 40 # Start at top
122
-
123
- c.setFont("Helvetica-Bold", 14)
124
- c.drawString(30, y_position, "Chat History")
125
- y_position -= 20
126
- c.setFont("Helvetica", 10)
127
-
128
- for message in st.session_state.messages:
129
- role = "User: " if message["role"] == "user" else "Bot: "
130
- text = role + message["content"]
131
-
132
- for line in text.split("\n"):
133
- if y_position < 40: # New page if reaching bottom
134
- c.showPage()
135
- c.setFont("Helvetica", 10)
136
- y_position = height - 40
137
-
138
- c.drawString(30, y_position, line)
139
- y_position -= 15
140
-
141
- c.save()
142
- buffer.seek(0)
143
- return buffer
144
 
145
  # Streamlit UI
146
  st.title("πŸ€– AI Chatbot with File Upload & Video Calling πŸš€")
147
 
148
- # File Upload Feature
 
 
 
 
 
 
 
 
 
 
 
 
149
  uploaded_file = st.file_uploader("πŸ“„ Upload a document for Q&A", type=["txt", "pdf", "docx"])
150
 
151
  if uploaded_file:
152
- extracted_text = extract_text_from_file(uploaded_file)
 
 
 
 
 
 
 
 
 
 
 
153
  if extracted_text:
154
  st.subheader("πŸ“œ Extracted File Content:")
155
  st.text_area("File Content", extracted_text, height=200)
156
  else:
157
  st.warning("Unsupported file format.")
158
 
159
- # Suggested Questions
160
  st.subheader("πŸ’‘ Suggested Questions:")
 
161
  cols = st.columns(len(suggested_questions))
162
 
163
  user_input = None
@@ -165,7 +102,7 @@ for i, q in enumerate(suggested_questions):
165
  if cols[i].button(q):
166
  user_input = q
167
 
168
- # Voice Input
169
  st.subheader("🎀 Speak instead of typing!")
170
  if st.button("πŸŽ™οΈ Use Voice Input"):
171
  recognizer = sr.Recognizer()
@@ -179,15 +116,16 @@ if st.button("πŸŽ™οΈ Use Voice Input"):
179
  except sr.RequestError:
180
  user_input = "Speech recognition service error."
181
 
182
- # Video Call Feature
183
- st.subheader("πŸ“Ή Video Call")
184
- if st.button("πŸ“ž Start Video Call"):
185
- webrtc_streamer(key="video-call", mode=WebRtcMode.SENDRECV)
186
-
187
  if user_input is None:
188
  user_input = st.chat_input("Type your message here...")
189
 
190
- # Initialize chat history
 
 
 
 
 
191
  if "messages" not in st.session_state:
192
  st.session_state.messages = []
193
 
@@ -196,19 +134,55 @@ if user_input:
196
  if translated_text != user_input:
197
  user_input = translated_text
198
 
199
- response = get_best_response(user_input)
200
- log_chat(user_input, response)
 
 
201
 
202
- sentiment_result = analyze_sentiment(user_input)
 
 
 
 
 
 
 
 
203
 
204
  st.session_state.messages.append({"role": "user", "content": user_input})
205
  st.session_state.messages.append({"role": "assistant", "content": response})
206
 
207
- audio_file = text_to_speech(response)
 
 
 
208
 
209
  with st.chat_message("assistant"):
210
  st.write(f"{response}\n\n**Sentiment Analysis:** {sentiment_result}")
211
  st.audio(audio_file, format="audio/mp3")
212
 
213
- pdf_file = generate_chat_pdf()
214
- st.download_button("πŸ“₯ Download Chat as PDF", pdf_file, "chat_history.pdf", "application/pdf")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  # Initialize translator
49
  translator = GoogleTranslator(source="auto", target="en")
50
 
51
+ # Video Call Configuration
52
+ RTC_CONFIG = RTCConfiguration({"iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}]})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
+ # Initialize video call session state
55
+ if "video_call_active" not in st.session_state:
56
+ st.session_state.video_call_active = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
  # Streamlit UI
59
  st.title("πŸ€– AI Chatbot with File Upload & Video Calling πŸš€")
60
 
61
+ # πŸ“Ή **Video Call Feature**
62
+ st.subheader("πŸ“Ή Video Call")
63
+
64
+ if st.button("πŸ“ž Start Video Call"):
65
+ st.session_state.video_call_active = True
66
+
67
+ if st.button("❌ End Video Call"):
68
+ st.session_state.video_call_active = False
69
+
70
+ if st.session_state.video_call_active:
71
+ webrtc_streamer(key="video-chat", mode=WebRtcMode.SENDRECV, rtc_configuration=RTC_CONFIG)
72
+
73
+ # πŸ“ **File Upload Feature**
74
  uploaded_file = st.file_uploader("πŸ“„ Upload a document for Q&A", type=["txt", "pdf", "docx"])
75
 
76
  if uploaded_file:
77
+ extracted_text = None
78
+ file_extension = uploaded_file.name.split(".")[-1].lower()
79
+
80
+ if file_extension == "txt":
81
+ extracted_text = uploaded_file.getvalue().decode("utf-8")
82
+ elif file_extension == "pdf":
83
+ reader = PdfReader(uploaded_file)
84
+ extracted_text = "\n".join([page.extract_text() for page in reader.pages if page.extract_text()])
85
+ elif file_extension == "docx":
86
+ doc = docx.Document(uploaded_file)
87
+ extracted_text = "\n".join([para.text for para in doc.paragraphs])
88
+
89
  if extracted_text:
90
  st.subheader("πŸ“œ Extracted File Content:")
91
  st.text_area("File Content", extracted_text, height=200)
92
  else:
93
  st.warning("Unsupported file format.")
94
 
95
+ # πŸ’‘ **Suggested Questions**
96
  st.subheader("πŸ’‘ Suggested Questions:")
97
+ suggested_questions = ["What is AI?", "Tell me a joke!", "How does machine learning work?"]
98
  cols = st.columns(len(suggested_questions))
99
 
100
  user_input = None
 
102
  if cols[i].button(q):
103
  user_input = q
104
 
105
+ # 🎀 **Voice Input**
106
  st.subheader("🎀 Speak instead of typing!")
107
  if st.button("πŸŽ™οΈ Use Voice Input"):
108
  recognizer = sr.Recognizer()
 
116
  except sr.RequestError:
117
  user_input = "Speech recognition service error."
118
 
119
+ # ✍️ **Text Input**
 
 
 
 
120
  if user_input is None:
121
  user_input = st.chat_input("Type your message here...")
122
 
123
+ # πŸ—‘οΈ **Clear Chat Button**
124
+ if st.button("πŸ—‘οΈ Clear Chat"):
125
+ st.session_state.messages = []
126
+ st.rerun()
127
+
128
+ # πŸ“Œ **Chat Processing**
129
  if "messages" not in st.session_state:
130
  st.session_state.messages = []
131
 
 
134
  if translated_text != user_input:
135
  user_input = translated_text
136
 
137
+ input_embedding = embedding_model.encode(user_input, convert_to_tensor=True)
138
+ similarities = util.pytorch_cos_sim(input_embedding, question_embeddings)[0].cpu()
139
+ best_match_idx = torch.argmax(similarities).item()
140
+ best_match_score = similarities[best_match_idx].item()
141
 
142
+ if best_match_score > 0.7:
143
+ response = answers[best_match_idx]
144
+ else:
145
+ inputs = chatbot_tokenizer(user_input, return_tensors="pt")
146
+ outputs = chatbot_model.generate(**inputs)
147
+ response = chatbot_tokenizer.decode(outputs[0], skip_special_tokens=True)
148
+
149
+ sentiment = TextBlob(user_input).sentiment.polarity
150
+ sentiment_result = "😊 Positive" if sentiment > 0 else "😞 Negative" if sentiment < 0 else "😐 Neutral"
151
 
152
  st.session_state.messages.append({"role": "user", "content": user_input})
153
  st.session_state.messages.append({"role": "assistant", "content": response})
154
 
155
+ tts = gTTS(text=response, lang="en")
156
+ audio_file = BytesIO()
157
+ tts.write_to_fp(audio_file)
158
+ audio_file.seek(0)
159
 
160
  with st.chat_message("assistant"):
161
  st.write(f"{response}\n\n**Sentiment Analysis:** {sentiment_result}")
162
  st.audio(audio_file, format="audio/mp3")
163
 
164
+ # πŸ“₯ **Download Chat as PDF**
165
+ buffer = BytesIO()
166
+ c = canvas.Canvas(buffer, pagesize=letter)
167
+ width, height = letter
168
+ y_position = height - 40
169
+
170
+ c.setFont("Helvetica-Bold", 14)
171
+ c.drawString(30, y_position, "Chat History")
172
+ y_position -= 20
173
+ c.setFont("Helvetica", 10)
174
+
175
+ for message in st.session_state.messages:
176
+ role = "User: " if message["role"] == "user" else "Bot: "
177
+ text = role + message["content"]
178
+ for line in text.split("\n"):
179
+ if y_position < 40:
180
+ c.showPage()
181
+ c.setFont("Helvetica", 10)
182
+ y_position = height - 40
183
+ c.drawString(30, y_position, line)
184
+ y_position -= 15
185
+
186
+ c.save()
187
+ buffer.seek(0)
188
+ st.download_button("πŸ“₯ Download Chat as PDF", buffer, "chat_history.pdf", "application/pdf")