Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -13,15 +13,13 @@ from groq import Groq
|
|
13 |
import PyPDF2
|
14 |
import requests
|
15 |
|
16 |
-
# Extract text from PDF with fallback
|
17 |
# --- Document Loaders ---
|
18 |
def extract_text_from_pdf(pdf_path):
|
19 |
try:
|
20 |
text = ""
|
21 |
with open(pdf_path, 'rb') as file:
|
22 |
pdf_reader = PyPDF2.PdfReader(file)
|
23 |
-
for
|
24 |
-
page = pdf_reader.pages[page_num]
|
25 |
page_text = page.extract_text()
|
26 |
if page_text:
|
27 |
text += page_text
|
@@ -51,7 +49,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")
|
@@ -121,18 +118,16 @@ def send_twilio_message(account_sid, auth_token, conversation_sid, body):
|
|
121 |
st.set_page_config(page_title="SMEHelpBot β WhatsApp Integration", layout="wide")
|
122 |
st.title("π± SMEHelpBot + WhatsApp (via Twilio)")
|
123 |
|
124 |
-
# Load from Hugging Face secrets
|
125 |
account_sid = st.secrets.get("TWILIO_SID")
|
126 |
auth_token = st.secrets.get("TWILIO_TOKEN")
|
127 |
-
conversation_sid = "CH61b7b444aa3e4392a7624e4a22928db7"
|
128 |
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
|
129 |
|
130 |
-
# Fallback for testing
|
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 |
if all([account_sid, auth_token, conversation_sid, GROQ_API_KEY]):
|
138 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
@@ -159,6 +154,8 @@ if all([account_sid, auth_token, conversation_sid, GROQ_API_KEY]):
|
|
159 |
|
160 |
st.success("β
Knowledge base ready. Monitoring WhatsApp...")
|
161 |
|
|
|
|
|
162 |
if st.button("π Check for New WhatsApp Query"):
|
163 |
with st.spinner("Checking messages..."):
|
164 |
question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
|
@@ -173,4 +170,4 @@ if all([account_sid, auth_token, conversation_sid, GROQ_API_KEY]):
|
|
173 |
else:
|
174 |
st.warning("No new messages from users found.")
|
175 |
else:
|
176 |
-
st.warning("β Please provide all required credentials.")
|
|
|
13 |
import PyPDF2
|
14 |
import requests
|
15 |
|
|
|
16 |
# --- Document Loaders ---
|
17 |
def extract_text_from_pdf(pdf_path):
|
18 |
try:
|
19 |
text = ""
|
20 |
with open(pdf_path, 'rb') as file:
|
21 |
pdf_reader = PyPDF2.PdfReader(file)
|
22 |
+
for page in pdf_reader.pages:
|
|
|
23 |
page_text = page.extract_text()
|
24 |
if page_text:
|
25 |
text += page_text
|
|
|
49 |
D, I = index.search(np.array([question_embedding]), k)
|
50 |
return [text_chunks[i] for i in I[0]]
|
51 |
|
|
|
52 |
def generate_answer_with_groq(question, context, retries=3, delay=2):
|
53 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
54 |
api_key = os.environ.get("GROQ_API_KEY")
|
|
|
118 |
st.set_page_config(page_title="SMEHelpBot β WhatsApp Integration", layout="wide")
|
119 |
st.title("π± SMEHelpBot + WhatsApp (via Twilio)")
|
120 |
|
121 |
+
# Load from Hugging Face secrets or fallback to manual input
|
122 |
account_sid = st.secrets.get("TWILIO_SID")
|
123 |
auth_token = st.secrets.get("TWILIO_TOKEN")
|
|
|
124 |
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
|
125 |
|
|
|
126 |
if not all([account_sid, auth_token, GROQ_API_KEY]):
|
127 |
st.warning("β οΈ Some secrets not found. Please enter missing credentials below:")
|
128 |
+
account_sid = st.text_input("π Twilio SID", value=account_sid or "")
|
129 |
+
auth_token = st.text_input("π Twilio Auth Token", type="password", value=auth_token or "")
|
130 |
+
GROQ_API_KEY = st.text_input("π GROQ API Key", type="password", value=GROQ_API_KEY or "")
|
131 |
|
132 |
if all([account_sid, auth_token, conversation_sid, GROQ_API_KEY]):
|
133 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
|
|
154 |
|
155 |
st.success("β
Knowledge base ready. Monitoring WhatsApp...")
|
156 |
|
157 |
+
conversation_sid = st.text_input("π¬ Enter Twilio Conversation SID", placeholder="e.g. CHxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
|
158 |
+
|
159 |
if st.button("π Check for New WhatsApp Query"):
|
160 |
with st.spinner("Checking messages..."):
|
161 |
question, sender, msg_index = fetch_latest_incoming_message(account_sid, auth_token, conversation_sid)
|
|
|
170 |
else:
|
171 |
st.warning("No new messages from users found.")
|
172 |
else:
|
173 |
+
st.warning("β Please provide all required credentials including Conversation SID.")
|