Update app.py
Browse files
app.py
CHANGED
|
@@ -14,21 +14,27 @@ HEADERS = {
|
|
| 14 |
}
|
| 15 |
|
| 16 |
# --- Core API Functions ---
|
| 17 |
-
def query_rag_system(query: str, max_cards: int = 10) -> dict:
|
| 18 |
"""Query the RAG system with a historical question."""
|
| 19 |
try:
|
| 20 |
payload = {"query": query, "max_cards": max_cards, "include_sources": True}
|
| 21 |
response = requests.post(f"{API_URL}/query", json=payload, headers=HEADERS)
|
|
|
|
| 22 |
return response.json()
|
|
|
|
|
|
|
| 23 |
except Exception as e:
|
| 24 |
return {"error": str(e)}
|
| 25 |
|
| 26 |
-
def search_cards_semantic(query: str, max_cards: int = 10) -> dict:
|
| 27 |
"""Perform semantic search on cards."""
|
| 28 |
try:
|
| 29 |
payload = {"query": query, "max_cards": max_cards}
|
| 30 |
response = requests.post(f"{API_URL}/search", json=payload, headers=HEADERS)
|
|
|
|
| 31 |
return response.json()
|
|
|
|
|
|
|
| 32 |
except Exception as e:
|
| 33 |
return {"error": str(e)}
|
| 34 |
|
|
@@ -48,10 +54,9 @@ def format_rag_response(result: dict) -> str:
|
|
| 48 |
|
| 49 |
if sources:
|
| 50 |
response += f"**π Sources ({len(sources)} documents):**\n"
|
| 51 |
-
|
|
|
|
| 52 |
response += f"{i}. {source}\n"
|
| 53 |
-
if len(sources) > 3:
|
| 54 |
-
response += f"... and {len(sources) - 3} more sources\n"
|
| 55 |
response += "\n"
|
| 56 |
|
| 57 |
if cards_used:
|
|
@@ -71,7 +76,8 @@ def format_search_response(result: dict) -> str:
|
|
| 71 |
|
| 72 |
response = f"π **Found {len(cards)} relevant historical entries:**\n\n"
|
| 73 |
|
| 74 |
-
|
|
|
|
| 75 |
title = card.get('title', 'Untitled')
|
| 76 |
summary = card.get('summary', 'No summary available')
|
| 77 |
relevance = card.get('relevance_score', 0)
|
|
@@ -82,9 +88,6 @@ def format_search_response(result: dict) -> str:
|
|
| 82 |
|
| 83 |
response += f"**{i}. {title}** (Relevance: {relevance:.3f})\n{summary}\n\n"
|
| 84 |
|
| 85 |
-
if len(cards) > 3:
|
| 86 |
-
response += f"*... and {len(cards) - 3} more results*\n"
|
| 87 |
-
|
| 88 |
processing_time = result.get("processing_time", 0)
|
| 89 |
response += f"β±οΈ Search completed in {processing_time:.2f}s"
|
| 90 |
|
|
@@ -95,8 +98,8 @@ def chatbot_respond(message: str, history: List[Tuple[str, str]], search_mode: b
|
|
| 95 |
if not message.strip():
|
| 96 |
return "", history
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
max_cards = 10
|
| 100 |
|
| 101 |
if search_mode:
|
| 102 |
# Use semantic search for browsing/exploring
|
|
@@ -202,14 +205,17 @@ def create_interface():
|
|
| 202 |
if __name__ == "__main__":
|
| 203 |
print("π Launching Historical Knowledge Assistant...")
|
| 204 |
print(f"π API URL: {API_URL}")
|
| 205 |
-
|
| 206 |
-
|
|
|
|
|
|
|
|
|
|
| 207 |
demo = create_interface()
|
| 208 |
|
| 209 |
# For Hugging Face Spaces deployment
|
| 210 |
demo.launch(
|
| 211 |
-
share=False,
|
| 212 |
-
server_name="0.0.0.0",
|
| 213 |
-
server_port=7860,
|
| 214 |
show_error=True
|
| 215 |
)
|
|
|
|
| 14 |
}
|
| 15 |
|
| 16 |
# --- Core API Functions ---
|
| 17 |
+
def query_rag_system(query: str, max_cards: int = 10) -> dict: # Default changed to 10
|
| 18 |
"""Query the RAG system with a historical question."""
|
| 19 |
try:
|
| 20 |
payload = {"query": query, "max_cards": max_cards, "include_sources": True}
|
| 21 |
response = requests.post(f"{API_URL}/query", json=payload, headers=HEADERS)
|
| 22 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 23 |
return response.json()
|
| 24 |
+
except requests.exceptions.RequestException as e:
|
| 25 |
+
return {"error": f"Network Error: {e}"}
|
| 26 |
except Exception as e:
|
| 27 |
return {"error": str(e)}
|
| 28 |
|
| 29 |
+
def search_cards_semantic(query: str, max_cards: int = 10) -> dict: # Default changed to 10
|
| 30 |
"""Perform semantic search on cards."""
|
| 31 |
try:
|
| 32 |
payload = {"query": query, "max_cards": max_cards}
|
| 33 |
response = requests.post(f"{API_URL}/search", json=payload, headers=HEADERS)
|
| 34 |
+
response.raise_for_status() # Raise an error for bad status codes
|
| 35 |
return response.json()
|
| 36 |
+
except requests.exceptions.RequestException as e:
|
| 37 |
+
return {"error": f"Network Error: {e}"}
|
| 38 |
except Exception as e:
|
| 39 |
return {"error": str(e)}
|
| 40 |
|
|
|
|
| 54 |
|
| 55 |
if sources:
|
| 56 |
response += f"**π Sources ({len(sources)} documents):**\n"
|
| 57 |
+
# MODIFICATION: Loop through all sources instead of just the top 3
|
| 58 |
+
for i, source in enumerate(sources, 1):
|
| 59 |
response += f"{i}. {source}\n"
|
|
|
|
|
|
|
| 60 |
response += "\n"
|
| 61 |
|
| 62 |
if cards_used:
|
|
|
|
| 76 |
|
| 77 |
response = f"π **Found {len(cards)} relevant historical entries:**\n\n"
|
| 78 |
|
| 79 |
+
# MODIFICATION: Show all results instead of just the top 3
|
| 80 |
+
for i, card in enumerate(cards, 1):
|
| 81 |
title = card.get('title', 'Untitled')
|
| 82 |
summary = card.get('summary', 'No summary available')
|
| 83 |
relevance = card.get('relevance_score', 0)
|
|
|
|
| 88 |
|
| 89 |
response += f"**{i}. {title}** (Relevance: {relevance:.3f})\n{summary}\n\n"
|
| 90 |
|
|
|
|
|
|
|
|
|
|
| 91 |
processing_time = result.get("processing_time", 0)
|
| 92 |
response += f"β±οΈ Search completed in {processing_time:.2f}s"
|
| 93 |
|
|
|
|
| 98 |
if not message.strip():
|
| 99 |
return "", history
|
| 100 |
|
| 101 |
+
# MODIFICATION: Set max_cards to 10 for both modes
|
| 102 |
+
max_cards = 10
|
| 103 |
|
| 104 |
if search_mode:
|
| 105 |
# Use semantic search for browsing/exploring
|
|
|
|
| 205 |
if __name__ == "__main__":
|
| 206 |
print("π Launching Historical Knowledge Assistant...")
|
| 207 |
print(f"π API URL: {API_URL}")
|
| 208 |
+
if API_TOKEN:
|
| 209 |
+
print(f"π Using API token: {API_TOKEN[:4]}...{API_TOKEN[-4:]}")
|
| 210 |
+
else:
|
| 211 |
+
print("π API token not found. Please set the API_TOKEN secret in your Space settings.")
|
| 212 |
+
|
| 213 |
demo = create_interface()
|
| 214 |
|
| 215 |
# For Hugging Face Spaces deployment
|
| 216 |
demo.launch(
|
| 217 |
+
share=False,
|
| 218 |
+
server_name="0.0.0.0",
|
| 219 |
+
server_port=7860,
|
| 220 |
show_error=True
|
| 221 |
)
|