masadonline commited on
Commit
e172b58
·
verified ·
1 Parent(s): bb87ec7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -11
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import os
2
  import time
3
  import streamlit as st
4
- from streamlit_autorefresh import st_autorefresh
5
  from twilio.rest import Client
6
  from pdfminer.high_level import extract_text
7
  from sentence_transformers import SentenceTransformer
@@ -12,8 +11,15 @@ import docx
12
  import PyPDF2
13
  import requests
14
 
 
 
 
15
  # --- Auto-refresh every 10 seconds ---
16
- count = st_autorefresh(interval=10_000, limit=None, key="refresh")
 
 
 
 
17
 
18
  # --- Document Loaders ---
19
  def extract_text_from_pdf(pdf_path):
@@ -55,10 +61,7 @@ def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
55
  # --- GROQ Answer Generation ---
56
  def generate_answer_with_groq(question, context, retries=3, delay=2):
57
  url = "https://api.groq.com/openai/v1/chat/completions"
58
- api_key = os.environ.get("GROQ_API_KEY")
59
- if not api_key:
60
- return "⚠️ GROQ_API_KEY is not set."
61
-
62
  headers = {
63
  "Authorization": f"Bearer {api_key}",
64
  "Content-Type": "application/json",
@@ -138,8 +141,6 @@ def send_twilio_message(account_sid, auth_token, conversation_sid, body):
138
  return str(e)
139
 
140
  # --- Streamlit UI ---
141
- st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
142
-
143
  st.markdown("""
144
  <style>
145
  .big-font { font-size: 28px !important; font-weight: bold; }
@@ -156,9 +157,15 @@ st.markdown('<div class="big-font">📱 Quasa – A Smart WhatsApp Chatbot</div>
156
  st.markdown('<div class="small-font">Talk to your documents using WhatsApp. Powered by Groq, Twilio, and RAG.</div>', unsafe_allow_html=True)
157
 
158
  # Load secrets or fallback
159
- account_sid = st.secrets.get("TWILIO_SID") or st.text_input("Twilio SID", "")
160
- auth_token = st.secrets.get("TWILIO_TOKEN") or st.text_input("Twilio Auth Token", type="password")
161
- GROQ_API_KEY = st.secrets.get("GROQ_API_KEY") or st.text_input("GROQ API Key", type="password")
 
 
 
 
 
 
162
 
163
  if all([account_sid, auth_token, GROQ_API_KEY]):
164
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
@@ -166,6 +173,7 @@ if all([account_sid, auth_token, GROQ_API_KEY]):
166
  conversation_sid = fetch_latest_conversation_sid(account_sid, auth_token)
167
 
168
  if conversation_sid:
 
169
  @st.cache_resource
170
  def setup_knowledge_base():
171
  folder_path = "docs"
 
1
  import os
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
 
11
  import PyPDF2
12
  import requests
13
 
14
+ # --- Streamlit page config MUST be first ---
15
+ st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
16
+
17
  # --- Auto-refresh every 10 seconds ---
18
+ if "last_refresh" not in st.session_state:
19
+ st.session_state.last_refresh = time.time()
20
+ elif time.time() - st.session_state.last_refresh > 10:
21
+ st.session_state.last_refresh = time.time()
22
+ st.experimental_rerun()
23
 
24
  # --- Document Loaders ---
25
  def extract_text_from_pdf(pdf_path):
 
61
  # --- GROQ Answer Generation ---
62
  def generate_answer_with_groq(question, context, retries=3, delay=2):
63
  url = "https://api.groq.com/openai/v1/chat/completions"
64
+ api_key = os.environ["GROQ_API_KEY"]
 
 
 
65
  headers = {
66
  "Authorization": f"Bearer {api_key}",
67
  "Content-Type": "application/json",
 
141
  return str(e)
142
 
143
  # --- Streamlit UI ---
 
 
144
  st.markdown("""
145
  <style>
146
  .big-font { font-size: 28px !important; font-weight: bold; }
 
157
  st.markdown('<div class="small-font">Talk to your documents using WhatsApp. Powered by Groq, Twilio, and RAG.</div>', unsafe_allow_html=True)
158
 
159
  # Load secrets or fallback
160
+ account_sid = st.secrets.get("TWILIO_SID")
161
+ auth_token = st.secrets.get("TWILIO_TOKEN")
162
+ GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
163
+
164
+ if not all([account_sid, auth_token, GROQ_API_KEY]):
165
+ st.warning("⚠️ Some secrets are missing. Please provide them manually:")
166
+ account_sid = st.text_input("Twilio SID", value=account_sid or "")
167
+ auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
168
+ GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
169
 
170
  if all([account_sid, auth_token, GROQ_API_KEY]):
171
  os.environ["GROQ_API_KEY"] = GROQ_API_KEY
 
173
  conversation_sid = fetch_latest_conversation_sid(account_sid, auth_token)
174
 
175
  if conversation_sid:
176
+
177
  @st.cache_resource
178
  def setup_knowledge_base():
179
  folder_path = "docs"