File size: 1,125 Bytes
3303565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1603195
3303565
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
from bs4 import BeautifulSoup
from transformers import pipeline

# Load the trained model
qa_model = pipeline("question-answering")

def extract_answer(question, url):
    """Get context from URL and use it to answer the question"""
    
    # Retrieve actual page content
    html = requests.get(url).content
    # Create BS4 object to handle HTML data
    soup = BeautifulSoup(html, 'html.parser')

    for data in soup(['style', 'script', 'meta', 'link', 'noscript']):
        # Remove tags
        data.decompose()

    # Get and clean up plain text
    context = soup.get_text()
    while "\n\n" in context:
        context = context.replace("\n\n", "\n")
    
    answer_dict = qa_model(question = question, context = context)
    return answer_dict['answer']

title = "Webpage-Based Question Answering"
description = "Using a webpage as context for extractive question answering."
enable_queue=True

iface = gr.Interface(
    fn=extract_answer, 
    inputs=["text", "text"], 
    outputs="text",
    title=title,
    description=description
)
iface.launch(enable_queue=enable_queue)