Spaces:
Runtime error
Runtime error
Kwadwo Agyapon-Ntra
commited on
Commit
·
3303565
1
Parent(s):
9d723a5
Added code for question answering using the text from a web page as context
Browse files- app.py +40 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Load the trained model
|
7 |
+
qa_model = pipeline("question-answering")
|
8 |
+
|
9 |
+
def extract_answer(question, url):
|
10 |
+
"""Get context from URL and use it to answer the question"""
|
11 |
+
|
12 |
+
# Retrieve actual page content
|
13 |
+
html = requests.get(url).content
|
14 |
+
# Create BS4 object to handle HTML data
|
15 |
+
soup = BeautifulSoup(html, 'html.parser')
|
16 |
+
|
17 |
+
for data in soup(['style', 'script', 'meta', 'link', 'noscript']):
|
18 |
+
# Remove tags
|
19 |
+
data.decompose()
|
20 |
+
|
21 |
+
# Get and clean up plain text
|
22 |
+
context = soup.get_text()
|
23 |
+
while "\n\n" in context:
|
24 |
+
context = context.replace("\n\n", "\n")
|
25 |
+
|
26 |
+
answer_dict = qa_model(question = question, context = context)
|
27 |
+
return answer_dict['answer']
|
28 |
+
|
29 |
+
title = "Webpage Question Answering"
|
30 |
+
description = "Using a webpage as context for extractive question answering."
|
31 |
+
enable_queue=True
|
32 |
+
|
33 |
+
iface = gr.Interface(
|
34 |
+
fn=extract_answer,
|
35 |
+
inputs=["text", "text"],
|
36 |
+
outputs="text",
|
37 |
+
title=title,
|
38 |
+
description=description
|
39 |
+
)
|
40 |
+
iface.launch(enable_queue=enable_queue)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
transformers[torch]
|
2 |
+
beautifulsoup4
|