File size: 1,465 Bytes
a448356
 
 
a584a85
 
 
a448356
a584a85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a448356
 
a584a85
 
a448356
 
a584a85
 
 
 
 
 
 
 
a448356
 
 
 
 
 
a584a85
 
 
 
 
 
 
 
a448356
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# -*- coding: utf-8 -*-
from transformers import pipeline
import gradio as gr
from gradio.components import Textbox
import csv
import ast

import ipdb

examples = []

# 打开 CSV 文件以供读取
with open('Preprocess_validation.csv', mode='r') as csv_file:
        csv_reader = csv.reader(csv_file)
        
        # 读取前五行数据
        for i, row in enumerate(csv_reader):
                if i >= 5:
                        break
                if i > 0:
                        row_q = ast.literal_eval(row[4])['text'][0]
                        row_c = row[10]
                        examples.append([row_q, row_c])

inputs = [
        Textbox(lines=3, label="Question"),
        Textbox(lines=12, label="Context paragraph"),
]

question_answering = pipeline(
        "question-answering", 
        model = pipeline(
                "question-answering",
                model="xjlulu/ntu_adl_span_selection_cluecorpussmall",
                framework="pt"
        )
)

def generate_answer(question, context):
        result = question_answering(question=question, context=context)
        return result['answer']

iface = gr.Interface(
        fn=generate_answer,
        inputs=inputs,
        outputs=gr.Textbox(lines=10, label="Answer"),
        title="Question Answering",
        description="Answer questions based on a given context paragraph",
        live=False,
        examples=examples,
        theme="huggingface"
)

iface.launch()