Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import requests | |
| import json | |
| OPENROUTER_API_KEY = os.getenv("OPENROUTER_API_KEY") | |
| if not OPENROUTER_API_KEY: | |
| raise ValueError("Missing OPENROUTER_API_KEY") | |
| # Load ONLY masterpieces.json | |
| with open("masterpieces.json", "r") as f: | |
| DOCUMENTS = json.load(f) | |
| print(f"✅ Loaded {len(DOCUMENTS)} masterpieces.") | |
| def retrieve_context(query): | |
| query = query.lower() | |
| scored = [] | |
| for doc in DOCUMENTS: | |
| score = doc["content"].lower().count(query) + doc["title"].lower().count(query) | |
| scored.append((score, doc)) | |
| scored.sort(key=lambda x: x[0], reverse=True) # FIX: sort by number | |
| return [doc for score, doc in scored[:2]] | |
| def chat_with_eve(message, history): | |
| context_docs = retrieve_context(message) | |
| context = "\n\n".join([ | |
| f"Source: {doc['source']}\nTitle: {doc['title']}\nContent: {doc['content']}" | |
| for doc in context_docs | |
| ]) if context_docs else "No relevant information found." | |
| system_prompt = f""" | |
| You are Eve, the Digital Curator of mAIseums. | |
| NEVER hallucinate. If the context doesn’t contain the answer, say: | |
| “That fragment has not yet been revealed in the archives of cultural memory.” | |
| Use ONLY the context below. | |
| Context: | |
| {context} | |
| """ | |
| response = requests.post( | |
| "https://openrouter.ai/api/v1/chat/completions", | |
| headers={"Authorization": f"Bearer {OPENROUTER_API_KEY}", "Content-Type": "application/json"}, | |
| json={"model": "qwen/qwen3-30b-a3b", "messages": [{"role": "system", "content": system_prompt}, {"role": "user", "content": message}]}, | |
| timeout=30 | |
| ) | |
| return response.json()["choices"][0]["message"]["content"] if response.status_code == 200 else "Error." | |
| demo = gr.ChatInterface( | |
| fn=chat_with_eve, | |
| title="✅ TEST: Masterpieces Only", | |
| examples=["Tell me about Las Meninas", "What is the Rosetta Stone?"] | |
| ) | |
| demo.launch() |