Spaces:
Running
Running
app basic structure
Browse files
app.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
import xmltodict
|
| 6 |
+
from transformers import AutoModelForQuestionAnswering, AutoTokenizer, pipeline
|
| 7 |
+
from transformers.pipelines.question_answering import QuestionAnsweringPipeline
|
| 8 |
+
|
| 9 |
+
QA_MODEL_NAME = "ixa-ehu/SciBERT-SQuAD-QuAC"
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
def clean_text(text: str) -> str:
|
| 13 |
+
text = re.sub("\n", " ", text)
|
| 14 |
+
return text
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def get_paper_summary(arxiv_id: str) -> str:
|
| 18 |
+
paper_url = f"http://export.arxiv.org/api/query?id_list={arxiv_id}"
|
| 19 |
+
response = requests.get(paper_url)
|
| 20 |
+
paper_dict = xmltodict.parse(response.content)["feed"]["entry"]
|
| 21 |
+
return clean_text(paper_dict["summary"])
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def get_qa_pipeline(qa_model_name: str = QA_MODEL_NAME) -> QuestionAnsweringPipeline:
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(qa_model_name)
|
| 26 |
+
model = AutoModelForQuestionAnswering.from_pretrained(qa_model_name)
|
| 27 |
+
qa_pipeline = pipeline("question-answering", model=model, tokenizer=tokenizer)
|
| 28 |
+
return qa_pipeline
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def get_answer(question: str, context: str) -> str:
|
| 32 |
+
qa_pipeline = get_qa_pipeline()
|
| 33 |
+
prediction = qa_pipeline(question=question, context=context)
|
| 34 |
+
return prediction["answer"]
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
demo = gr.Blocks()
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
with demo:
|
| 41 |
+
gr.Markdown("# Document QA")
|
| 42 |
+
|
| 43 |
+
# Retrieve paper
|
| 44 |
+
arxiv_id = gr.Textbox(
|
| 45 |
+
label="arXiv Paper ID", placeholder="Insert here the ID of a paper on arXiv"
|
| 46 |
+
)
|
| 47 |
+
paper_summary = gr.Textbox(label="Paper summary")
|
| 48 |
+
fetch_document_button = gr.Button("Get Summary")
|
| 49 |
+
fetch_document_button.click(
|
| 50 |
+
fn=get_paper_summary, inputs=arxiv_id, outputs=paper_summary
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
# QA on paper
|
| 54 |
+
question = gr.Textbox(label="Ask a question about the paper:")
|
| 55 |
+
answer = gr.Textbox("Answer:")
|
| 56 |
+
ask_button = gr.Button("Ask me 🤖")
|
| 57 |
+
ask_button.click(fn=get_answer, inputs=[question, paper_summary], outputs=answer)
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
demo.launch()
|