Spaces:
Sleeping
Sleeping
File size: 3,282 Bytes
7d34386 83175f3 7d34386 12f4fc7 ee6b298 7d34386 ee6b298 7d34386 ee6b298 7d34386 ee6b298 7d34386 ee6b298 7d34386 ee6b298 7d34386 ee6b298 7d34386 ee6b298 7d34386 5eb5edb ee6b298 7d34386 ee6b298 44ba4b0 7d34386 5eb5edb 7d34386 5eb5edb 7d34386 5eb5edb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
import gradio as gr
import os
from query import ask
print("🚀 Starting 80,000 Hours RAG system...")
from query import is_model_ready
print("✅ App ready! Model loading in background...")
def chat_interface(message: str, history):
"""Process question and return formatted response for chatbot.
Args:
message: User's question (string or dict with 'content' key)
history: Chat history (list of message dicts with 'role' and 'content')
Returns:
Formatted response with answer and citations
"""
# Handle both string and dict message formats
if isinstance(message, dict):
message = message.get('text', message.get('content', ''))
if not message or not message.strip():
return ""
result = ask(message, show_context=False)
# Format response: answer first, then divider, then citations
response = result["answer"]
# Add citations after divider
if result["citations"]:
response += "\n\n---\n\n**Citations:**\n\n"
for i, citation in enumerate(result["citations"], 1):
# Replace bullet points in citation text with newline + bullet icon
response += f"**[{i}]** [{citation['title']}]({citation['url']})\n\n"
return response
# --- Build Gradio UI ---
with gr.Blocks(title="80,000 Hours Q&A", theme=gr.themes.Soft(), css="""
footer {display: none !important;}
.examples button {
background: linear-gradient(to bottom, #ffffff, #f8f9fa) !important;
border: 2px solid #dee2e6 !important;
border-radius: 8px !important;
padding: 12px 16px !important;
box-shadow: 0 2px 4px rgba(0,0,0,0.05) !important;
transition: all 0.2s ease !important;
}
.examples button:hover {
border-color: #adb5bd !important;
box-shadow: 0 4px 8px rgba(0,0,0,0.1) !important;
transform: translateY(-1px) !important;
}
""") as demo:
# Title section
gr.Markdown("# 80,000 Hours Q&A")
gr.Markdown("*Ask questions about career planning and get answers backed by citations from 80,000 Hours articles.*")
gr.ChatInterface(
fn=chat_interface,
type="messages",
chatbot=gr.Chatbot(
height=400,
show_copy_button=True,
render_markdown=True,
layout="bubble",
type="messages"
),
textbox=gr.MultimodalTextbox(
placeholder="Ask about career planning...",
show_label=False,
submit_btn=True,
sources=[]
),
examples=[
"What skills will be most in demand in the next 5–10 years?",
"How can I work on the world's most pressing problems?",
"How do I figure out what I want to do with my life?",
],
cache_examples=False
)
# --- Launch Logic ---
if __name__ == "__main__":
# Detect if running on Hugging Face Spaces (or other managed env)
in_spaces = os.environ.get("SPACE_ID") is not None or os.environ.get("SYSTEM") == "spaces"
if in_spaces:
demo.launch() # Use platform defaults
else:
demo.launch(
server_name="0.0.0.0",
server_port=7860,
share=False
)
|