masadonline commited on
Commit
91e81b4
Β·
verified Β·
1 Parent(s): bce7695

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -32
app.py CHANGED
@@ -11,12 +11,7 @@ import docx
11
  from groq import Groq
12
  import PyPDF2
13
  import requests
14
-
15
- # --- Auto Refresh ---
16
- st_autorefresh = st.experimental_rerun # fallback if newer version doesn't have st_autorefresh
17
- if hasattr(st, "autorefresh"):
18
- st_autorefresh = st.autorefresh
19
- st_autorefresh(interval=10000, key="refresh") # every 10 seconds
20
 
21
  # --- Document Loaders ---
22
  def extract_text_from_pdf(pdf_path):
@@ -103,16 +98,12 @@ def send_twilio_message(account_sid, auth_token, conversation_sid, body):
103
  except Exception as e:
104
  return str(e)
105
 
106
- def fetch_latest_conversation_sid(account_sid, auth_token):
107
- client = Client(account_sid, auth_token)
108
- conversations = client.conversations.v1.conversations.list(limit=1)
109
- if conversations:
110
- return conversations[0].sid
111
- return ""
112
-
113
  # --- Streamlit UI ---
114
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
115
 
 
 
 
116
  # Styling
117
  st.markdown("""
118
  <style>
@@ -151,8 +142,7 @@ if not all([account_sid, auth_token, GROQ_API_KEY]):
151
  auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
152
  GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
153
 
154
- conversation_sid_input = st.text_input("Enter Twilio Conversation SID (leave blank for latest)", value="")
155
- conversation_sid = conversation_sid_input or fetch_latest_conversation_sid(account_sid, auth_token)
156
 
157
  if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
158
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
@@ -177,25 +167,24 @@ if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
177
 
178
  index, embedding_model, text_chunks = setup_knowledge_base()
179
 
180
- st.success("βœ… Knowledge base ready. Checking WhatsApp messages every 10 seconds...")
181
 
182
- # Track last message index
183
  if "last_processed_index" not in st.session_state:
184
  st.session_state.last_processed_index = -1
185
 
186
- question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
187
-
188
- if question and msg_index != st.session_state.last_processed_index:
189
- st.session_state.last_processed_index = msg_index
190
- st.info(f"πŸ“₯ New question from **{sender}**:\n\n> {question}")
191
- relevant_chunks = retrieve_chunks(question, index, embedding_model, text_chunks)
192
- context = "\n\n".join(relevant_chunks)
193
- answer = generate_answer_with_groq(question, context)
194
- send_twilio_message(account_sid, auth_token, conversation_sid, answer)
195
- st.success("πŸ“€ Answer sent back to user on WhatsApp!")
196
- st.markdown(f"### ✨ Answer:\n\n{answer}")
197
- else:
198
- st.write("πŸ”„ No new message yet... waiting.")
199
-
200
  else:
201
- st.warning("❗ Please provide all required credentials.")
 
11
  from groq import Groq
12
  import PyPDF2
13
  import requests
14
+ from streamlit_extras.st_autorefresh import st_autorefresh
 
 
 
 
 
15
 
16
  # --- Document Loaders ---
17
  def extract_text_from_pdf(pdf_path):
 
98
  except Exception as e:
99
  return str(e)
100
 
 
 
 
 
 
 
 
101
  # --- Streamlit UI ---
102
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
103
 
104
+ # Auto-refresh every 10 seconds
105
+ st_autorefresh(interval=10 * 1000, key="auto_refresh")
106
+
107
  # Styling
108
  st.markdown("""
109
  <style>
 
142
  auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
143
  GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
144
 
145
+ conversation_sid = st.text_input("Enter Twilio Conversation SID", value="")
 
146
 
147
  if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
148
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
167
 
168
  index, embedding_model, text_chunks = setup_knowledge_base()
169
 
170
+ st.success("βœ… Knowledge base ready. Monitoring WhatsApp messages...")
171
 
 
172
  if "last_processed_index" not in st.session_state:
173
  st.session_state.last_processed_index = -1
174
 
175
+ with st.spinner("Checking for new WhatsApp messages..."):
176
+ question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
177
+
178
+ if question and msg_index != st.session_state.last_processed_index:
179
+ st.session_state.last_processed_index = msg_index
180
+ st.info(f"πŸ“₯ New question from **{sender}**:\n\n> {question}")
181
+ relevant_chunks = retrieve_chunks(question, index, embedding_model, text_chunks)
182
+ context = "\n\n".join(relevant_chunks)
183
+ answer = generate_answer_with_groq(question, context)
184
+ send_twilio_message(account_sid, auth_token, conversation_sid, answer)
185
+ st.success("πŸ“€ Answer sent back to user on WhatsApp!")
186
+ st.markdown(f"### ✨ Answer:\n\n{answer}")
187
+ else:
188
+ st.info("πŸ”„ No new messages yet...")
189
  else:
190
+ st.warning("❗ Please provide all required credentials and conversation SID.")