masadonline commited on
Commit
97626e0
Β·
verified Β·
1 Parent(s): d9461a1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -13
app.py CHANGED
@@ -2,7 +2,6 @@ import os
2
  import time
3
  import streamlit as st
4
  from twilio.rest import Client
5
- from twilio.base.exceptions import TwilioRestException
6
  from pdfminer.high_level import extract_text
7
  from sentence_transformers import SentenceTransformer
8
  from transformers import AutoTokenizer
@@ -100,22 +99,47 @@ def send_twilio_message(account_sid, auth_token, conversation_sid, body):
100
 
101
  # --- Streamlit UI ---
102
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
103
- st.title("πŸ“± Quasa – A Smart WhatsApp Chatbot")
104
 
105
- # Load from Hugging Face secrets
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  account_sid = st.secrets.get("TWILIO_SID")
107
  auth_token = st.secrets.get("TWILIO_TOKEN")
108
  GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
109
 
110
- # Fallback for testing
111
  if not all([account_sid, auth_token, GROQ_API_KEY]):
112
- st.warning("⚠️ Some secrets not found. Please enter missing credentials below:")
113
  account_sid = st.text_input("Twilio SID", value=account_sid or "")
114
  auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
115
  GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
116
 
117
- # Always show conversation SID input
118
- conversation_sid = st.text_input("Enter Conversation SID", value="")
119
 
120
  if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
121
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
@@ -140,20 +164,26 @@ if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
140
 
141
  index, embedding_model, text_chunks = setup_knowledge_base()
142
 
143
- st.success("βœ… Knowledge base ready. Monitoring WhatsApp...")
 
 
 
 
144
 
145
  if st.button("πŸ” Check for New WhatsApp Query"):
146
- with st.spinner("Checking messages..."):
147
  question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
148
- if question:
149
- st.info(f"πŸ“₯ New Question from {sender}:\n\n> {question}")
 
 
150
  relevant_chunks = retrieve_chunks(question, index, embedding_model, text_chunks)
151
  context = "\n\n".join(relevant_chunks)
152
  answer = generate_answer_with_groq(question, context)
153
  send_twilio_message(account_sid, auth_token, conversation_sid, answer)
154
- st.success("πŸ“€ Answer sent via WhatsApp!")
155
  st.markdown(f"### ✨ Answer:\n\n{answer}")
156
  else:
157
- st.warning("No new messages from users found.")
158
  else:
159
  st.warning("❗ Please provide all required credentials and conversation SID.")
 
2
  import time
3
  import streamlit as st
4
  from twilio.rest import Client
 
5
  from pdfminer.high_level import extract_text
6
  from sentence_transformers import SentenceTransformer
7
  from transformers import AutoTokenizer
 
99
 
100
  # --- Streamlit UI ---
101
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
 
102
 
103
+ # Styling
104
+ st.markdown("""
105
+ <style>
106
+ .big-font {
107
+ font-size: 28px !important;
108
+ font-weight: bold;
109
+ }
110
+ .small-font {
111
+ font-size: 16px !important;
112
+ color: #555;
113
+ }
114
+ .stButton > button {
115
+ background-color: #0066CC;
116
+ color: white;
117
+ padding: 0.5em 1em;
118
+ border-radius: 8px;
119
+ font-size: 18px;
120
+ }
121
+ .stTextInput > div > input {
122
+ font-size: 16px;
123
+ }
124
+ </style>
125
+ """, unsafe_allow_html=True)
126
+
127
+ st.markdown('<div class="big-font">πŸ“± Quasa – A Smart WhatsApp Chatbot</div>', unsafe_allow_html=True)
128
+ st.markdown('<div class="small-font">Talk to your documents using WhatsApp. Powered by Groq, Twilio, and RAG.</div>', unsafe_allow_html=True)
129
+
130
+ # Load secrets
131
  account_sid = st.secrets.get("TWILIO_SID")
132
  auth_token = st.secrets.get("TWILIO_TOKEN")
133
  GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
134
 
135
+ # Allow user input fallback
136
  if not all([account_sid, auth_token, GROQ_API_KEY]):
137
+ st.warning("⚠️ Some secrets are missing. Please provide them manually:")
138
  account_sid = st.text_input("Twilio SID", value=account_sid or "")
139
  auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
140
  GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
141
 
142
+ conversation_sid = st.text_input("Enter Twilio Conversation SID", value="")
 
143
 
144
  if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
145
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
164
 
165
  index, embedding_model, text_chunks = setup_knowledge_base()
166
 
167
+ st.success("βœ… Knowledge base ready. Monitoring WhatsApp messages...")
168
+
169
+ # Message index tracking
170
+ if "last_processed_index" not in st.session_state:
171
+ st.session_state.last_processed_index = -1
172
 
173
  if st.button("πŸ” Check for New WhatsApp Query"):
174
+ with st.spinner("Checking for new WhatsApp messages..."):
175
  question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
176
+
177
+ if question and msg_index != st.session_state.last_processed_index:
178
+ st.session_state.last_processed_index = msg_index
179
+ st.info(f"πŸ“₯ New question from **{sender}**:\n\n> {question}")
180
  relevant_chunks = retrieve_chunks(question, index, embedding_model, text_chunks)
181
  context = "\n\n".join(relevant_chunks)
182
  answer = generate_answer_with_groq(question, context)
183
  send_twilio_message(account_sid, auth_token, conversation_sid, answer)
184
+ st.success("πŸ“€ Answer sent back to user on WhatsApp!")
185
  st.markdown(f"### ✨ Answer:\n\n{answer}")
186
  else:
187
+ st.warning("No new messages found.")
188
  else:
189
  st.warning("❗ Please provide all required credentials and conversation SID.")