Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,6 @@ import PyPDF2
|
|
13 |
import requests
|
14 |
from streamlit_autorefresh import st_autorefresh
|
15 |
|
16 |
-
# Extract text from PDF with fallback
|
17 |
# --- Document Loaders ---
|
18 |
def extract_text_from_pdf(pdf_path):
|
19 |
try:
|
@@ -51,7 +50,6 @@ def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
|
|
51 |
D, I = index.search(np.array([question_embedding]), k)
|
52 |
return [text_chunks[i] for i in I[0]]
|
53 |
|
54 |
-
# Generate answer using Groq API with retries and timeout
|
55 |
def generate_answer_with_groq(question, context, retries=3, delay=2):
|
56 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
57 |
api_key = os.environ.get("GROQ_API_KEY")
|
@@ -100,7 +98,6 @@ def generate_answer_with_groq(question, context, retries=3, delay=2):
|
|
100 |
except Exception as e:
|
101 |
return f"β οΈ Groq API Error: {e}"
|
102 |
|
103 |
-
# Twilio message fetch and send
|
104 |
def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
|
105 |
client = Client(account_sid, auth_token)
|
106 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
|
@@ -117,7 +114,7 @@ def send_twilio_message(account_sid, auth_token, conversation_sid, body):
|
|
117 |
except Exception as e:
|
118 |
return str(e)
|
119 |
|
120 |
-
# Streamlit UI
|
121 |
st.set_page_config(page_title="Quasa β A Smart WhatsApp Chatbot", layout="wide")
|
122 |
st.title("π± Quasa β A Smart WhatsApp Chatbot")
|
123 |
|
@@ -127,18 +124,14 @@ if "last_index" not in st.session_state:
|
|
127 |
account_sid = st.secrets.get("TWILIO_SID")
|
128 |
auth_token = st.secrets.get("TWILIO_TOKEN")
|
129 |
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
|
|
|
130 |
|
131 |
-
if not all([account_sid, auth_token, GROQ_API_KEY]):
|
132 |
st.warning("β οΈ Some secrets not found. Please enter missing credentials below:")
|
133 |
account_sid = st.text_input("Twilio SID", value=account_sid or "")
|
134 |
auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
|
135 |
GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
|
136 |
-
|
137 |
-
enable_autorefresh = st.checkbox("π Enable Auto-Refresh", value=True)
|
138 |
-
interval_seconds = st.selectbox("Refresh Interval (seconds)", options=[5, 10, 15, 30, 60], index=4)
|
139 |
-
|
140 |
-
if enable_autorefresh:
|
141 |
-
st_autorefresh(interval=interval_seconds * 1000, key="auto-refresh")
|
142 |
|
143 |
if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
|
144 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
@@ -165,12 +158,19 @@ if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
|
|
165 |
st.error(f"Error setting up knowledge base: {e}")
|
166 |
return None, None, None
|
167 |
|
|
|
168 |
index, embedding_model, text_chunks = setup_knowledge_base()
|
|
|
169 |
if index is None:
|
170 |
st.stop()
|
171 |
|
172 |
st.success("β
Knowledge base ready. Monitoring WhatsApp...")
|
173 |
|
|
|
|
|
|
|
|
|
|
|
174 |
with st.spinner("β³ Checking for new WhatsApp messages..."):
|
175 |
question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
|
176 |
if question and msg_index > st.session_state.last_index:
|
@@ -185,4 +185,4 @@ if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
|
|
185 |
else:
|
186 |
st.caption("β
No new message yet. Waiting for refresh...")
|
187 |
else:
|
188 |
-
st.warning("β Please provide all required credentials and conversation SID.")
|
|
|
13 |
import requests
|
14 |
from streamlit_autorefresh import st_autorefresh
|
15 |
|
|
|
16 |
# --- Document Loaders ---
|
17 |
def extract_text_from_pdf(pdf_path):
|
18 |
try:
|
|
|
50 |
D, I = index.search(np.array([question_embedding]), k)
|
51 |
return [text_chunks[i] for i in I[0]]
|
52 |
|
|
|
53 |
def generate_answer_with_groq(question, context, retries=3, delay=2):
|
54 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
55 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
|
98 |
except Exception as e:
|
99 |
return f"β οΈ Groq API Error: {e}"
|
100 |
|
|
|
101 |
def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
|
102 |
client = Client(account_sid, auth_token)
|
103 |
messages = client.conversations.v1.conversations(conversation_sid).messages.list(limit=10)
|
|
|
114 |
except Exception as e:
|
115 |
return str(e)
|
116 |
|
117 |
+
# --- Streamlit UI ---
|
118 |
st.set_page_config(page_title="Quasa β A Smart WhatsApp Chatbot", layout="wide")
|
119 |
st.title("π± Quasa β A Smart WhatsApp Chatbot")
|
120 |
|
|
|
124 |
account_sid = st.secrets.get("TWILIO_SID")
|
125 |
auth_token = st.secrets.get("TWILIO_TOKEN")
|
126 |
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
|
127 |
+
conversation_sid = st.secrets.get("TWILIO_CONVERSATION_SID")
|
128 |
|
129 |
+
if not all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
|
130 |
st.warning("β οΈ Some secrets not found. Please enter missing credentials below:")
|
131 |
account_sid = st.text_input("Twilio SID", value=account_sid or "")
|
132 |
auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
|
133 |
GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
|
134 |
+
conversation_sid = st.text_input("Twilio Conversation SID", value=conversation_sid or "")
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
|
137 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
|
|
158 |
st.error(f"Error setting up knowledge base: {e}")
|
159 |
return None, None, None
|
160 |
|
161 |
+
st.info("π Loading knowledge base...")
|
162 |
index, embedding_model, text_chunks = setup_knowledge_base()
|
163 |
+
|
164 |
if index is None:
|
165 |
st.stop()
|
166 |
|
167 |
st.success("β
Knowledge base ready. Monitoring WhatsApp...")
|
168 |
|
169 |
+
enable_autorefresh = st.checkbox("π Enable Auto-Refresh", value=True)
|
170 |
+
interval_seconds = st.selectbox("Refresh Interval (seconds)", options=[5, 10, 15, 30, 60], index=4)
|
171 |
+
if enable_autorefresh:
|
172 |
+
st_autorefresh(interval=interval_seconds * 1000, key="auto-refresh")
|
173 |
+
|
174 |
with st.spinner("β³ Checking for new WhatsApp messages..."):
|
175 |
question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
|
176 |
if question and msg_index > st.session_state.last_index:
|
|
|
185 |
else:
|
186 |
st.caption("β
No new message yet. Waiting for refresh...")
|
187 |
else:
|
188 |
+
st.warning("β Please provide all required credentials and conversation SID.")
|