Spaces:
Sleeping
Sleeping
Download app.py from cynstl/groundwork: direct link, hf CLI and curl.
- Browser
- Download file 2.98 kB
-
https://huggingface.co/spaces/cynstl/groundwork/resolve/main/app.py
- Command line
-
hf download hf://spaces/cynstl/groundwork/app.py
-
curl -L -o app.py https://huggingface.co/spaces/cynstl/groundwork/resolve/main/app.py
2.98 kB
| # Import `spaces` first — must happen before any torch/CUDA initialization | |
| # (torch is pulled in transitively via rag_chain → langchain_huggingface). | |
| # The try/except keeps local dev working (spaces is HF-Spaces-only). | |
| try: | |
| import spaces # noqa: F401 — imported for GPU-context registration, not called directly | |
| except ImportError: | |
| pass | |
| import gradio as gr | |
| from rag_chain import load_chain_components, answer_question, init_gpu | |
| # Register GPU entry point with ZeroGPU runtime (satisfies the | |
| # "No @spaces.GPU function detected" check). Must run before | |
| # load_chain_components. Returns a plain bool — picklable across the | |
| # ZeroGPU worker-process boundary. | |
| init_gpu() | |
| # Load index + LLM once at startup (not per-request) | |
| vectorstore, llm = load_chain_components() | |
| SAMPLE_QUESTIONS = [ | |
| "What do Kaplan et al. and the Chinchilla paper disagree about?", | |
| "How does the RAG paper combine retrieval with generation?", | |
| "Why do language models hallucinate, according to the sources?", | |
| "How was the disagreement between Kaplan and Chinchilla resolved?", | |
| ] | |
| def respond(question, history): | |
| if not question or not question.strip(): | |
| return history, "" | |
| answer_text, sources = answer_question(question, vectorstore, llm) | |
| # Format the citation key as a collapsible-looking block under the answer | |
| citation_block = "\n\n---\n**Sources**\n" | |
| for s in sources: | |
| citation_block += f"\n**[{s['n']}]** {s['citation']} — p.{s['page']}\n> {s['excerpt']}\n" | |
| full_response = answer_text + citation_block | |
| # Gradio 6.x Chatbot expects messages as dicts with 'role' and 'content' | |
| # keys (not (user, bot) tuples as in Gradio 5.x). Initial history may be None. | |
| if history is None: | |
| history = [] | |
| history = history + [ | |
| {"role": "user", "content": question}, | |
| {"role": "assistant", "content": full_response}, | |
| ] | |
| return history, "" | |
| with gr.Blocks(title="Groundwork") as demo: | |
| gr.Markdown( | |
| """ | |
| # Groundwork | |
| Ask a question spanning multiple GenAI research papers. Every claim in the | |
| answer is cited back to the exact source excerpt it came from. | |
| **Corpus:** Kaplan et al. 2020 (Scaling Laws) · Hoffmann et al. 2022 (Chinchilla) | |
| · Pearce & Song 2024 (Reconciling) · Lewis et al. 2020 (RAG) · Kalai et al. 2025 (Hallucination) | |
| """ | |
| ) | |
| chatbot = gr.Chatbot(label="Groundwork", height=450) | |
| msg = gr.Textbox( | |
| label="Your question", | |
| placeholder="e.g. What do Kaplan et al. and Chinchilla disagree about?", | |
| ) | |
| with gr.Row(): | |
| submit_btn = gr.Button("Ask", variant="primary") | |
| clear_btn = gr.ClearButton([msg, chatbot], value="Clear") | |
| gr.Examples(examples=SAMPLE_QUESTIONS, inputs=msg, label="Try one of these") | |
| msg.submit(respond, [msg, chatbot], [chatbot, msg]) | |
| submit_btn.click(respond, [msg, chatbot], [chatbot, msg]) | |
| if __name__ == "__main__": | |
| demo.launch() |