Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
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
|
@@ -8,16 +9,11 @@ from transformers import AutoTokenizer
|
|
8 |
import faiss
|
9 |
import numpy as np
|
10 |
import docx
|
11 |
-
from groq import Groq
|
12 |
import PyPDF2
|
13 |
import requests
|
14 |
|
15 |
# --- Auto-refresh every 10 seconds ---
|
16 |
-
|
17 |
-
st.session_state.last_refresh = time.time()
|
18 |
-
elif time.time() - st.session_state.last_refresh > 10:
|
19 |
-
st.session_state.last_refresh = time.time()
|
20 |
-
st.experimental_rerun()
|
21 |
|
22 |
# --- Document Loaders ---
|
23 |
def extract_text_from_pdf(pdf_path):
|
@@ -59,7 +55,10 @@ def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
|
|
59 |
# --- GROQ Answer Generation ---
|
60 |
def generate_answer_with_groq(question, context, retries=3, delay=2):
|
61 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
62 |
-
api_key = os.environ
|
|
|
|
|
|
|
63 |
headers = {
|
64 |
"Authorization": f"Bearer {api_key}",
|
65 |
"Content-Type": "application/json",
|
@@ -121,9 +120,7 @@ def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
|
|
121 |
def send_twilio_message(account_sid, auth_token, conversation_sid, body):
|
122 |
try:
|
123 |
client = Client(account_sid, auth_token)
|
124 |
-
# Get participants to find the bot's WhatsApp identity
|
125 |
participants = client.conversations.v1.conversations(conversation_sid).participants.list()
|
126 |
-
# Pick the first participant whose identity starts with 'whatsapp:'
|
127 |
bot_identity = None
|
128 |
for p in participants:
|
129 |
if p.identity.startswith("whatsapp:"):
|
@@ -140,7 +137,6 @@ def send_twilio_message(account_sid, auth_token, conversation_sid, body):
|
|
140 |
except Exception as e:
|
141 |
return str(e)
|
142 |
|
143 |
-
|
144 |
# --- Streamlit UI ---
|
145 |
st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
|
146 |
|
@@ -160,15 +156,9 @@ st.markdown('<div class="big-font">📱 Quasa – A Smart WhatsApp Chatbot</div>
|
|
160 |
st.markdown('<div class="small-font">Talk to your documents using WhatsApp. Powered by Groq, Twilio, and RAG.</div>', unsafe_allow_html=True)
|
161 |
|
162 |
# Load secrets or fallback
|
163 |
-
account_sid = st.secrets.get("TWILIO_SID")
|
164 |
-
auth_token = st.secrets.get("TWILIO_TOKEN")
|
165 |
-
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
|
166 |
-
|
167 |
-
if not all([account_sid, auth_token, GROQ_API_KEY]):
|
168 |
-
st.warning("⚠️ Some secrets are missing. Please provide them manually:")
|
169 |
-
account_sid = st.text_input("Twilio SID", value=account_sid or "")
|
170 |
-
auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
|
171 |
-
GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
|
172 |
|
173 |
if all([account_sid, auth_token, GROQ_API_KEY]):
|
174 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
|
|
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
|
|
|
9 |
import faiss
|
10 |
import numpy as np
|
11 |
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 |
# --- 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",
|
|
|
120 |
def send_twilio_message(account_sid, auth_token, conversation_sid, body):
|
121 |
try:
|
122 |
client = Client(account_sid, auth_token)
|
|
|
123 |
participants = client.conversations.v1.conversations(conversation_sid).participants.list()
|
|
|
124 |
bot_identity = None
|
125 |
for p in participants:
|
126 |
if p.identity.startswith("whatsapp:"):
|
|
|
137 |
except Exception as e:
|
138 |
return str(e)
|
139 |
|
|
|
140 |
# --- Streamlit UI ---
|
141 |
st.set_page_config(page_title="Quasa – A Smart WhatsApp Chatbot", layout="wide")
|
142 |
|
|
|
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
|