masadonline commited on
Commit
f7c1ada
Β·
verified Β·
1 Parent(s): d0b83ee

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -20
app.py CHANGED
@@ -12,6 +12,7 @@ import docx
12
  from groq import Groq
13
  import PyPDF2
14
  import requests
 
15
 
16
  # --- Document Loaders ---
17
  def extract_text_from_pdf(pdf_path):
@@ -102,24 +103,28 @@ def send_twilio_message(account_sid, auth_token, conversation_sid, body):
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
- # Initialize session state to track last message
121
- if "last_index" not in st.session_state:
122
- st.session_state.last_index = -1
 
 
123
 
124
  if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
125
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
@@ -146,19 +151,18 @@ if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
146
 
147
  st.success("βœ… Knowledge base ready. Monitoring WhatsApp...")
148
 
149
- if st.button("πŸ” Check for New WhatsApp Query"):
150
- with st.spinner("Checking messages..."):
151
- question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
152
- if question and msg_index > st.session_state.last_index:
153
- st.session_state.last_index = msg_index
154
- st.info(f"πŸ“₯ New Question from {sender}:\n\n> {question}")
155
- relevant_chunks = retrieve_chunks(question, index, embedding_model, text_chunks)
156
- context = "\n\n".join(relevant_chunks)
157
- answer = generate_answer_with_groq(question, context)
158
- send_twilio_message(account_sid, auth_token, conversation_sid, answer)
159
- st.success("πŸ“€ Answer sent via WhatsApp!")
160
- st.markdown(f"### ✨ Answer:\n\n{answer}")
161
- else:
162
- st.warning("No new messages from users found.")
163
  else:
164
  st.warning("❗ Please provide all required credentials and conversation SID.")
 
12
  from groq import Groq
13
  import PyPDF2
14
  import requests
15
+ from streamlit_extras.st_autorefresh import st_autorefresh
16
 
17
  # --- Document Loaders ---
18
  def extract_text_from_pdf(pdf_path):
 
103
  st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
104
  st.title("πŸ“± Quasa – A Smart WhatsApp Chatbot")
105
 
106
+ # Initialize session state for last index
107
+ if "last_index" not in st.session_state:
108
+ st.session_state.last_index = -1
109
+
110
+ # Load secrets or allow manual input
111
  account_sid = st.secrets.get("TWILIO_SID")
112
  auth_token = st.secrets.get("TWILIO_TOKEN")
113
  GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
114
 
 
115
  if not all([account_sid, auth_token, GROQ_API_KEY]):
116
  st.warning("⚠️ Some secrets not found. Please enter missing credentials below:")
117
  account_sid = st.text_input("Twilio SID", value=account_sid or "")
118
  auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
119
  GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
120
 
 
121
  conversation_sid = st.text_input("Enter Conversation SID", value="")
122
 
123
+ # Auto-refresh toggle and interval selector
124
+ enable_autorefresh = st.toggle("πŸ”„ Enable Auto-Refresh", value=True)
125
+ interval_seconds = st.selectbox("Refresh Interval (seconds)", options=[5, 10, 15, 30, 60], index=1)
126
+ if enable_autorefresh:
127
+ st_autorefresh(interval=interval_seconds * 1000, key="auto-refresh")
128
 
129
  if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
130
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
151
 
152
  st.success("βœ… Knowledge base ready. Monitoring WhatsApp...")
153
 
154
+ with st.spinner("⏳ Checking for new WhatsApp messages..."):
155
+ question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
156
+ if question and msg_index > st.session_state.last_index:
157
+ st.session_state.last_index = msg_index
158
+ st.info(f"πŸ“₯ New Question from {sender}:\n\n> {question}")
159
+ relevant_chunks = retrieve_chunks(question, index, embedding_model, text_chunks)
160
+ context = "\n\n".join(relevant_chunks)
161
+ answer = generate_answer_with_groq(question, context)
162
+ send_twilio_message(account_sid, auth_token, conversation_sid, answer)
163
+ st.success("πŸ“€ Answer sent via WhatsApp!")
164
+ st.markdown(f"### ✨ Answer:\n\n{answer}")
165
+ else:
166
+ st.caption("βœ… No new message yet. Waiting for refresh...")
 
167
  else:
168
  st.warning("❗ Please provide all required credentials and conversation SID.")