Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
|
|
|
|
|
|
|
|
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 |
-
#
|
121 |
-
|
122 |
-
|
|
|
|
|
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 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
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.")
|