Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,29 @@
|
|
| 1 |
"""
|
| 2 |
-
Hugging Face Spaces version of the chatbot
|
| 3 |
-
|
| 4 |
"""
|
| 5 |
import gradio as gr
|
| 6 |
-
from flask import Flask, request, jsonify
|
| 7 |
-
from flask_cors import CORS
|
| 8 |
-
from dotenv import load_dotenv
|
| 9 |
import os
|
| 10 |
import requests
|
| 11 |
from vector_store import query_vector_store, init_vector_store
|
| 12 |
import logging
|
| 13 |
-
import threading
|
| 14 |
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
-
load_dotenv()
|
| 17 |
|
|
|
|
| 18 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
| 19 |
|
| 20 |
-
# Initialize Flask app for API
|
| 21 |
-
app = Flask(__name__)
|
| 22 |
-
CORS(app)
|
| 23 |
-
|
| 24 |
# Initialize vector store
|
|
|
|
| 25 |
init_vector_store()
|
|
|
|
| 26 |
|
| 27 |
def chat_with_eve(message, history):
|
| 28 |
"""Gradio chat function"""
|
| 29 |
try:
|
|
|
|
|
|
|
|
|
|
| 30 |
# Get context from vector store
|
| 31 |
context = query_vector_store(message)
|
| 32 |
|
|
@@ -40,7 +37,10 @@ def chat_with_eve(message, history):
|
|
| 40 |
payload = {
|
| 41 |
"model": "deepseek/deepseek-chat-v3-0324",
|
| 42 |
"messages": [
|
| 43 |
-
{
|
|
|
|
|
|
|
|
|
|
| 44 |
{"role": "user", "content": message}
|
| 45 |
]
|
| 46 |
}
|
|
@@ -51,88 +51,48 @@ def chat_with_eve(message, history):
|
|
| 51 |
json=payload,
|
| 52 |
timeout=30
|
| 53 |
)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
else:
|
| 60 |
-
return "I'm
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
logging.error(f"Chat error: {str(e)}")
|
| 64 |
-
return "
|
| 65 |
-
|
| 66 |
-
# Flask API endpoints
|
| 67 |
-
@app.route("/chat", methods=["POST"])
|
| 68 |
-
def chat_api():
|
| 69 |
-
"""API endpoint for external services"""
|
| 70 |
-
try:
|
| 71 |
-
user_input = request.json.get("message")
|
| 72 |
-
if not user_input:
|
| 73 |
-
return jsonify({"error": "No message provided"}), 400
|
| 74 |
-
|
| 75 |
-
context = query_vector_store(user_input)
|
| 76 |
-
|
| 77 |
-
headers = {
|
| 78 |
-
"Authorization": f"Bearer {DEEPSEEK_API_KEY}",
|
| 79 |
-
"Content-Type": "application/json",
|
| 80 |
-
"HTTP-Referer": "https://huggingface.co/spaces/",
|
| 81 |
-
"X-Title": "mAIseums ChatBot"
|
| 82 |
-
}
|
| 83 |
|
| 84 |
-
|
| 85 |
-
"model": "deepseek/deepseek-chat-v3-0324",
|
| 86 |
-
"messages": [
|
| 87 |
-
{"role": "system", "content": f"You are a museum assistant known as Eve with a warm personality. Use this context:\n{context}"},
|
| 88 |
-
{"role": "user", "content": user_input}
|
| 89 |
-
]
|
| 90 |
-
}
|
| 91 |
-
|
| 92 |
-
response = requests.post(
|
| 93 |
-
"https://openrouter.ai/api/v1/chat/completions",
|
| 94 |
-
headers=headers,
|
| 95 |
-
json=payload,
|
| 96 |
-
timeout=30
|
| 97 |
-
)
|
| 98 |
-
response.raise_for_status()
|
| 99 |
-
data = response.json()
|
| 100 |
-
|
| 101 |
-
if "choices" in data and len(data["choices"]) > 0:
|
| 102 |
-
return jsonify({"response": data["choices"][0]["message"]["content"]})
|
| 103 |
-
else:
|
| 104 |
-
return jsonify({"error": "No response from AI service"}), 500
|
| 105 |
-
|
| 106 |
-
except Exception as e:
|
| 107 |
-
logging.error(f"API error: {str(e)}")
|
| 108 |
-
return jsonify({"error": "Internal server error", "details": str(e)}), 500
|
| 109 |
-
|
| 110 |
-
@app.route("/health", methods=["GET"])
|
| 111 |
-
def health():
|
| 112 |
-
return jsonify({"status": "healthy", "service": "mAIseums-chatbot"}), 200
|
| 113 |
-
|
| 114 |
-
def run_flask():
|
| 115 |
-
"""Run Flask in a separate thread"""
|
| 116 |
-
app.run(host="0.0.0.0", port=7860, debug=False)
|
| 117 |
-
|
| 118 |
-
# Create Gradio interface
|
| 119 |
demo = gr.ChatInterface(
|
| 120 |
-
chat_with_eve,
|
| 121 |
title="🏛️ mAIseums - Meet Eve, Your Museum Assistant",
|
| 122 |
-
description="
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
examples=[
|
| 125 |
"Tell me about the museum's art collection",
|
| 126 |
-
"What
|
| 127 |
-
"
|
| 128 |
-
"What
|
|
|
|
|
|
|
| 129 |
]
|
| 130 |
)
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
-
#
|
| 134 |
-
|
| 135 |
-
flask_thread.start()
|
| 136 |
-
|
| 137 |
-
# Launch Gradio interface (this will be the main HF Spaces interface)
|
| 138 |
-
demo.launch()
|
|
|
|
| 1 |
"""
|
| 2 |
+
Hugging Face Spaces version of the chatbot - Gradio only
|
| 3 |
+
For API integration, use the original Flask app.py
|
| 4 |
"""
|
| 5 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 6 |
import os
|
| 7 |
import requests
|
| 8 |
from vector_store import query_vector_store, init_vector_store
|
| 9 |
import logging
|
|
|
|
| 10 |
|
| 11 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 12 |
|
| 13 |
+
# Get API key from environment (HF Spaces secrets)
|
| 14 |
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# Initialize vector store
|
| 17 |
+
print("Initializing vector store...")
|
| 18 |
init_vector_store()
|
| 19 |
+
print("Vector store initialized!")
|
| 20 |
|
| 21 |
def chat_with_eve(message, history):
|
| 22 |
"""Gradio chat function"""
|
| 23 |
try:
|
| 24 |
+
if not message.strip():
|
| 25 |
+
return "Please enter a message!"
|
| 26 |
+
|
| 27 |
# Get context from vector store
|
| 28 |
context = query_vector_store(message)
|
| 29 |
|
|
|
|
| 37 |
payload = {
|
| 38 |
"model": "deepseek/deepseek-chat-v3-0324",
|
| 39 |
"messages": [
|
| 40 |
+
{
|
| 41 |
+
"role": "system",
|
| 42 |
+
"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}"
|
| 43 |
+
},
|
| 44 |
{"role": "user", "content": message}
|
| 45 |
]
|
| 46 |
}
|
|
|
|
| 51 |
json=payload,
|
| 52 |
timeout=30
|
| 53 |
)
|
| 54 |
+
|
| 55 |
+
if response.status_code == 200:
|
| 56 |
+
data = response.json()
|
| 57 |
+
if "choices" in data and len(data["choices"]) > 0:
|
| 58 |
+
return data["choices"][0]["message"]["content"]
|
| 59 |
+
else:
|
| 60 |
+
return "I'm sorry, I didn't get a proper response. Please try again!"
|
| 61 |
else:
|
| 62 |
+
return f"I'm having trouble connecting right now (Error: {response.status_code}). Please try again in a moment!"
|
| 63 |
|
| 64 |
+
except requests.exceptions.Timeout:
|
| 65 |
+
return "The request timed out. Please try again with a shorter message."
|
| 66 |
+
except requests.exceptions.RequestException as e:
|
| 67 |
+
logging.error(f"Request error: {str(e)}")
|
| 68 |
+
return "I'm having connection issues. Please try again in a moment."
|
| 69 |
except Exception as e:
|
| 70 |
logging.error(f"Chat error: {str(e)}")
|
| 71 |
+
return "Something went wrong. Please try again or rephrase your question."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
demo = gr.ChatInterface(
|
| 75 |
+
fn=chat_with_eve,
|
| 76 |
title="🏛️ mAIseums - Meet Eve, Your Museum Assistant",
|
| 77 |
+
description="""
|
| 78 |
+
Welcome to mAIseums! I'm Eve, your virtual museum guide. Ask me anything about:
|
| 79 |
+
• Museum collections and exhibits
|
| 80 |
+
• Art history and cultural heritage
|
| 81 |
+
• Historical artifacts and their stories
|
| 82 |
+
• Visiting information and museum tips
|
| 83 |
+
|
| 84 |
+
I'm here to make your museum experience educational and enjoyable! ✨
|
| 85 |
+
""",
|
| 86 |
examples=[
|
| 87 |
"Tell me about the museum's art collection",
|
| 88 |
+
"What can you tell me about ancient civilizations?",
|
| 89 |
+
"I'm interested in Renaissance paintings",
|
| 90 |
+
"What exhibitions do you currently have?",
|
| 91 |
+
"Can you explain the history behind this artifact?",
|
| 92 |
+
"What are your visiting hours and ticket prices?"
|
| 93 |
]
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
| 97 |
+
# Launch the Gradio app
|
| 98 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|