Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -50,30 +50,7 @@ def retrieve_chunks(question, index, embed_model, text_chunks, k=3):
|
|
50 |
D, I = index.search(np.array([question_embedding]), k)
|
51 |
return [text_chunks[i] for i in I[0]]
|
52 |
|
53 |
-
#
|
54 |
-
# prompt = f"Based on the following context, answer the question: '{question}'\n\nContext:\n{context}"
|
55 |
-
# groq_client = Groq(api_key=os.environ["GROQ_API_KEY"])
|
56 |
-
|
57 |
-
# for attempt in range(retries):
|
58 |
-
# try:
|
59 |
-
# response = groq_client.chat.completions.create(
|
60 |
-
# model="llama3-8b-8192",
|
61 |
-
# messages=[
|
62 |
-
# {"role": "system", "content": "You are an AI Assistant for Small Businesses."},
|
63 |
-
# {"role": "user", "content": prompt},
|
64 |
-
# ]
|
65 |
-
# )
|
66 |
-
# return response.choices[0].message.content
|
67 |
-
# except GroqError as e:
|
68 |
-
# if "503" in str(e) and attempt < retries - 1:
|
69 |
-
# time.sleep(delay)
|
70 |
-
# continue
|
71 |
-
# else:
|
72 |
-
# return f"⚠️ Groq API Error: {str(e)}"
|
73 |
-
|
74 |
-
#-----------------------------------------
|
75 |
-
import requests # ✅ Add this at the top of your file
|
76 |
-
|
77 |
def generate_answer_with_groq(question, context, retries=3, delay=2):
|
78 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
79 |
api_key = os.environ["GROQ_API_KEY"]
|
@@ -104,8 +81,6 @@ def generate_answer_with_groq(question, context, retries=3, delay=2):
|
|
104 |
else:
|
105 |
return f"⚠️ Groq API Error: {str(e)}"
|
106 |
|
107 |
-
#-----------------------------------------
|
108 |
-
|
109 |
# --- Twilio Chat Handlers ---
|
110 |
def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
|
111 |
client = Client(account_sid, auth_token)
|
@@ -130,7 +105,6 @@ st.title("📱 SMEHelpBot + WhatsApp (via Twilio)")
|
|
130 |
# Load from Hugging Face secrets
|
131 |
account_sid = st.secrets.get("TWILIO_SID")
|
132 |
auth_token = st.secrets.get("TWILIO_TOKEN")
|
133 |
-
conversation_sid = "CH0401ce390b374780b3eaded677d882b0"
|
134 |
GROQ_API_KEY = st.secrets.get("GROQ_API_KEY")
|
135 |
|
136 |
# Fallback for testing
|
@@ -140,7 +114,10 @@ if not all([account_sid, auth_token, GROQ_API_KEY]):
|
|
140 |
auth_token = st.text_input("Twilio Auth Token", type="password", value=auth_token or "")
|
141 |
GROQ_API_KEY = st.text_input("GROQ API Key", type="password", value=GROQ_API_KEY or "")
|
142 |
|
143 |
-
|
|
|
|
|
|
|
144 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
145 |
|
146 |
@st.cache_resource
|
@@ -179,4 +156,4 @@ if all([account_sid, auth_token, conversation_sid, GROQ_API_KEY]):
|
|
179 |
else:
|
180 |
st.warning("No new messages from users found.")
|
181 |
else:
|
182 |
-
st.warning("❗ Please provide all required credentials.")
|
|
|
50 |
D, I = index.search(np.array([question_embedding]), k)
|
51 |
return [text_chunks[i] for i in I[0]]
|
52 |
|
53 |
+
# --- GROQ Answer Generation ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
def generate_answer_with_groq(question, context, retries=3, delay=2):
|
55 |
url = "https://api.groq.com/openai/v1/chat/completions"
|
56 |
api_key = os.environ["GROQ_API_KEY"]
|
|
|
81 |
else:
|
82 |
return f"⚠️ Groq API Error: {str(e)}"
|
83 |
|
|
|
|
|
84 |
# --- Twilio Chat Handlers ---
|
85 |
def fetch_latest_incoming_message(account_sid, auth_token, conversation_sid):
|
86 |
client = Client(account_sid, auth_token)
|
|
|
105 |
# Load from Hugging Face secrets
|
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
|
|
|
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 |
+
if all([account_sid, auth_token, GROQ_API_KEY, conversation_sid]):
|
121 |
os.environ["GROQ_API_KEY"] = GROQ_API_KEY
|
122 |
|
123 |
@st.cache_resource
|
|
|
156 |
else:
|
157 |
st.warning("No new messages from users found.")
|
158 |
else:
|
159 |
+
st.warning("❗ Please provide all required credentials and conversation SID.")
|