Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -161,19 +161,17 @@ class SimpleRAGChatbot:
|
|
161 |
|
162 |
def call_together_ai(self, prompt, context):
|
163 |
"""Call Together.ai API with context"""
|
164 |
-
# Try to get API key
|
165 |
-
api_key =
|
|
|
|
|
|
|
|
|
|
|
166 |
|
|
|
167 |
if not api_key:
|
168 |
-
return
|
169 |
-
|
170 |
-
I can help you with information about our policies, hours, menu, and services based on my knowledge base.
|
171 |
-
|
172 |
-
To enable full AI responses, please set up the TOGETHER_API_KEY in your HuggingFace Spaces secrets.
|
173 |
-
|
174 |
-
Here's what I found in our knowledge base about your question:
|
175 |
-
|
176 |
-
""" + context[:500] + "..."
|
177 |
|
178 |
url = "https://api.together.xyz/v1/chat/completions"
|
179 |
|
@@ -212,9 +210,71 @@ class SimpleRAGChatbot:
|
|
212 |
result = response.json()
|
213 |
return result["choices"][0]["message"]["content"]
|
214 |
else:
|
215 |
-
|
|
|
216 |
except Exception as e:
|
217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
218 |
|
219 |
def answer_question(self, question):
|
220 |
"""Answer a question using simple RAG"""
|
@@ -313,4 +373,9 @@ demo = gr.ChatInterface(
|
|
313 |
|
314 |
# Launch the interface
|
315 |
if __name__ == "__main__":
|
316 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
161 |
|
162 |
def call_together_ai(self, prompt, context):
|
163 |
"""Call Together.ai API with context"""
|
164 |
+
# Try multiple ways to get the API key
|
165 |
+
api_key = (
|
166 |
+
os.getenv("TOGETHER_API_KEY") or
|
167 |
+
os.getenv("TOGETHER_API_KEY_SECRET") or
|
168 |
+
os.getenv("HF_TOKEN") or # Sometimes HF uses this
|
169 |
+
None
|
170 |
+
)
|
171 |
|
172 |
+
# If no API key, provide a smart fallback response
|
173 |
if not api_key:
|
174 |
+
return self.create_smart_fallback_response(prompt, context)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
175 |
|
176 |
url = "https://api.together.xyz/v1/chat/completions"
|
177 |
|
|
|
210 |
result = response.json()
|
211 |
return result["choices"][0]["message"]["content"]
|
212 |
else:
|
213 |
+
print(f"API Error: {response.status_code} - {response.text}")
|
214 |
+
return self.create_smart_fallback_response(prompt, context)
|
215 |
except Exception as e:
|
216 |
+
print(f"API Exception: {str(e)}")
|
217 |
+
return self.create_smart_fallback_response(prompt, context)
|
218 |
+
|
219 |
+
def create_smart_fallback_response(self, prompt, context):
|
220 |
+
"""Create a smart response without AI API"""
|
221 |
+
prompt_lower = prompt.lower()
|
222 |
+
|
223 |
+
# Extract key information from context
|
224 |
+
context_lines = [line.strip() for line in context.split('\n') if line.strip() and not line.strip().startswith('Information about')]
|
225 |
+
|
226 |
+
# Create a personalized response based on the question type
|
227 |
+
if any(word in prompt_lower for word in ['menu', 'food', 'dish', 'eat', 'pizza', 'pasta']):
|
228 |
+
response = "π **Our Menu Highlights:**\n\n"
|
229 |
+
for line in context_lines:
|
230 |
+
if line.startswith('- '):
|
231 |
+
response += line + "\n"
|
232 |
+
response += "\n⨠All our dishes are prepared fresh daily with high-quality ingredients!"
|
233 |
+
|
234 |
+
elif any(word in prompt_lower for word in ['baby', 'child', 'kid', 'family']):
|
235 |
+
response = "πΆ **Family-Friendly Features:**\n\n"
|
236 |
+
for line in context_lines:
|
237 |
+
if line.startswith('- '):
|
238 |
+
response += line + "\n"
|
239 |
+
response += "\nπ We love welcoming families and have everything you need for a comfortable dining experience!"
|
240 |
+
|
241 |
+
elif any(word in prompt_lower for word in ['refund', 'money', 'return', 'policy']):
|
242 |
+
response = "π° **Our Refund Policy:**\n\n"
|
243 |
+
for line in context_lines:
|
244 |
+
if line.startswith('- '):
|
245 |
+
response += line + "\n"
|
246 |
+
response += "\nπ Customer satisfaction is our top priority!"
|
247 |
+
|
248 |
+
elif any(word in prompt_lower for word in ['hour', 'open', 'weekend', 'time']):
|
249 |
+
response = "π **Restaurant Hours:**\n\n"
|
250 |
+
for line in context_lines:
|
251 |
+
if line.startswith('- '):
|
252 |
+
response += line + "\n"
|
253 |
+
response += "\nπ We recommend calling ahead for weekend reservations!"
|
254 |
+
|
255 |
+
elif any(word in prompt_lower for word in ['allergy', 'allergic', 'gluten', 'vegan']):
|
256 |
+
response = "π₯ **Dietary Accommodations:**\n\n"
|
257 |
+
for line in context_lines:
|
258 |
+
if line.startswith('- '):
|
259 |
+
response += line + "\n"
|
260 |
+
response += "\nβ οΈ Please always inform our staff about any allergies when ordering!"
|
261 |
+
|
262 |
+
elif any(word in prompt_lower for word in ['contact', 'phone', 'address', 'delivery']):
|
263 |
+
response = "π **Contact & Delivery Info:**\n\n"
|
264 |
+
for line in context_lines:
|
265 |
+
if line.startswith('- '):
|
266 |
+
response += line + "\n"
|
267 |
+
response += "\nπ We're here to serve you however you prefer!"
|
268 |
+
|
269 |
+
else:
|
270 |
+
# General response
|
271 |
+
response = "π€ **Here's what I found for you:**\n\n"
|
272 |
+
for line in context_lines[:5]: # Show first 5 relevant lines
|
273 |
+
if line.startswith('- '):
|
274 |
+
response += line + "\n"
|
275 |
+
response += "\n㪠Feel free to ask me more specific questions about our restaurant!"
|
276 |
+
|
277 |
+
return response
|
278 |
|
279 |
def answer_question(self, question):
|
280 |
"""Answer a question using simple RAG"""
|
|
|
373 |
|
374 |
# Launch the interface
|
375 |
if __name__ == "__main__":
|
376 |
+
demo.launch(
|
377 |
+
share=True,
|
378 |
+
show_error=True,
|
379 |
+
server_name="0.0.0.0",
|
380 |
+
server_port=7860
|
381 |
+
)
|