|
|
import gradio as gr |
|
|
from src.rag.pipeline import answer_question |
|
|
|
|
|
|
|
|
def format_answer(question: str) -> tuple[str, str]: |
|
|
"""Format answer with citations.""" |
|
|
answer, citations = answer_question(question) |
|
|
|
|
|
|
|
|
if citations: |
|
|
citations_text = "\n\n### π Sources:\n" |
|
|
for i, citation in enumerate(citations, 1): |
|
|
citations_text += f"{i}. **{citation['title']}** ({citation['year']})\n" |
|
|
else: |
|
|
citations_text = "" |
|
|
|
|
|
return answer, citations_text |
|
|
|
|
|
|
|
|
def run_gradio(): |
|
|
with gr.Blocks(title="PaperMate", theme=gr.themes.Soft()) as demo: |
|
|
gr.Markdown( |
|
|
""" |
|
|
# π PaperMate β Research Paper Q&A Assistant |
|
|
Ask questions about research papers and get answers backed by scientific literature. |
|
|
""" |
|
|
) |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
question = gr.Textbox( |
|
|
label="Your Question", |
|
|
placeholder="e.g., What techniques are used to handle out-of-vocabulary words in NLP?", |
|
|
lines=3, |
|
|
) |
|
|
btn = gr.Button("π Search & Answer", variant="primary") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(): |
|
|
output = gr.Textbox(label="Answer", lines=10, max_lines=20) |
|
|
citations = gr.Markdown(label="Sources") |
|
|
|
|
|
btn.click(fn=format_answer, inputs=question, outputs=[output, citations]) |
|
|
|
|
|
gr.Markdown( |
|
|
""" |
|
|
--- |
|
|
π‘ **Tip:** Questions are answered using relevant papers from the ArXiv dataset. |
|
|
""" |
|
|
) |
|
|
|
|
|
demo.launch() |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
run_gradio() |
|
|
|