maiseumsChat / app.py
NitinMoturu's picture
Update app.py
0cf5a46 verified
"""
Hugging Face Spaces version of the chatbot - Gradio only
For API integration, use the original Flask app.py
"""
import gradio as gr
import os
import requests
from vector_store import query_vector_store, init_vector_store
import logging
logging.basicConfig(level=logging.INFO)
# Get API key from environment (HF Spaces secrets)
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
# Initialize vector store
print("Initializing vector store...")
init_vector_store()
print("Vector store initialized!")
def chat_with_eve(message, history):
"""Gradio chat function"""
try:
if not message.strip():
return "Please enter a message!"
# Get context from vector store
context = query_vector_store(message)
headers = {
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
"Content-Type": "application/json",
"HTTP-Referer": "https://huggingface.co/spaces/",
"X-Title": "mAIseums ChatBot"
}
payload = {
"model": "deepseek/deepseek-chat-v3-0324",
"messages": [
{
"role": "system",
"content": f"You are Eve, a warm and knowledgeable museum assistant. You help visitors learn about art, history, culture, and museum collections. Use this context to provide helpful, engaging responses: {context}"
},
{"role": "user", "content": message}
]
}
response = requests.post(
"https://openrouter.ai/api/v1/chat/completions",
headers=headers,
json=payload,
timeout=30
)
if response.status_code == 200:
data = response.json()
if "choices" in data and len(data["choices"]) > 0:
return data["choices"][0]["message"]["content"]
else:
return "I'm sorry, I didn't get a proper response. Please try again!"
else:
return f"I'm having trouble connecting right now (Error: {response.status_code}). Please try again in a moment!"
except requests.exceptions.Timeout:
return "The request timed out. Please try again with a shorter message."
except requests.exceptions.RequestException as e:
logging.error(f"Request error: {str(e)}")
return "I'm having connection issues. Please try again in a moment."
except Exception as e:
logging.error(f"Chat error: {str(e)}")
return "Something went wrong. Please try again or rephrase your question."
# Create the Gradio interface
demo = gr.ChatInterface(
fn=chat_with_eve,
title="🏛️ mAIseums - Meet Eve, Your Museum Assistant",
description="""
Welcome to mAIseums! I'm Eve, your virtual museum guide. Ask me anything about:
• Museum collections and exhibits
• Art history and cultural heritage
• Historical artifacts and their stories
• Visiting information and museum tips
I'm here to make your museum experience educational and enjoyable! ✨
""",
examples=[
"Tell me about the museum's art collection",
"What can you tell me about ancient civilizations?",
"I'm interested in Renaissance paintings",
"What exhibitions do you currently have?",
"Can you explain the history behind this artifact?",
"What are your visiting hours and ticket prices?"
]
)
if __name__ == "__main__":
# Launch the Gradio app
demo.launch()