Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import subprocess
|
| 3 |
+
import sys
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
def install(package):
|
| 7 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
|
| 8 |
+
|
| 9 |
+
install("numpy")
|
| 10 |
+
install("torch")
|
| 11 |
+
install("transformers")
|
| 12 |
+
install("unidecode")
|
| 13 |
+
|
| 14 |
+
import numpy as np
|
| 15 |
+
import torch
|
| 16 |
+
from transformers import DebertaV2TokenizerFast, DebertaV2ForQuestionAnswering
|
| 17 |
+
from transformers.pipelines import QuestionAnsweringPipeline
|
| 18 |
+
from transformers import pipeline
|
| 19 |
+
from collections import Counter
|
| 20 |
+
from unidecode import unidecode
|
| 21 |
+
import re
|
| 22 |
+
import string
|
| 23 |
+
|
| 24 |
+
tokenizer = DebertaV2TokenizerFast.from_pretrained("osiria/deberta-italian-question-answering", revision="liteqa")
|
| 25 |
+
model = DebertaV2ForQuestionAnswering.from_pretrained("osiria/deberta-italian-question-answering", revision="liteqa")
|
| 26 |
+
|
| 27 |
+
class OsiriaQA(QuestionAnsweringPipeline):
|
| 28 |
+
|
| 29 |
+
def __init__(self, punctuation = ',;.:!?()[\]{}', **kwargs):
|
| 30 |
+
|
| 31 |
+
QuestionAnsweringPipeline.__init__(self, **kwargs)
|
| 32 |
+
self.post_regex_left = "^[\s" + punctuation + "]+"
|
| 33 |
+
self.post_regex_right = "[\s" + punctuation + "]+$"
|
| 34 |
+
|
| 35 |
+
def postprocess(self, output):
|
| 36 |
+
|
| 37 |
+
output = QuestionAnsweringPipeline.postprocess(self, model_outputs=output)
|
| 38 |
+
output_length = len(output["answer"])
|
| 39 |
+
output["answer"] = re.sub(self.post_regex_left, "", output["answer"])
|
| 40 |
+
output["start"] = output["start"] + (output_length - len(output["answer"]))
|
| 41 |
+
output_length = len(output["answer"])
|
| 42 |
+
output["answer"] = re.sub(self.post_regex_right, "", output["answer"])
|
| 43 |
+
output["end"] = output["end"] - (output_length - len(output["answer"]))
|
| 44 |
+
|
| 45 |
+
return output
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
device = torch.device("cpu")
|
| 49 |
+
model = model.to(device)
|
| 50 |
+
model.eval()
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
pipeline_qa = OsiriaQA(model = model, tokenizer = tokenizer)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
header = '''--------------------------------------------------------------------------------------------------
|
| 57 |
+
<style>
|
| 58 |
+
.vertical-text {
|
| 59 |
+
writing-mode: vertical-lr;
|
| 60 |
+
text-orientation: upright;
|
| 61 |
+
background-color:red;
|
| 62 |
+
}
|
| 63 |
+
</style>
|
| 64 |
+
<center>
|
| 65 |
+
<body>
|
| 66 |
+
<span class="vertical-text" style="background-color:lightgreen;border-radius: 3px;padding: 3px;">β</span>
|
| 67 |
+
<span class="vertical-text" style="background-color:orange;border-radius: 3px;padding: 3px;">βD</span>
|
| 68 |
+
<span class="vertical-text" style="background-color:lightblue;border-radius: 3px;padding: 3px;">ββββE</span>
|
| 69 |
+
<span class="vertical-text" style="background-color:tomato;border-radius: 3px;padding: 3px;">ββββM</span>
|
| 70 |
+
<span class="vertical-text" style="background-color:lightgrey;border-radius: 3px;padding: 3px;">βO</span>
|
| 71 |
+
<span class="vertical-text" style="background-color:#CF9FFF;border-radius: 3px;padding: 3px;">β</span>
|
| 72 |
+
</body>
|
| 73 |
+
</center>
|
| 74 |
+
<br>
|
| 75 |
+
'''
|
| 76 |
+
|
| 77 |
+
def extract(question, context):
|
| 78 |
+
|
| 79 |
+
res = pipeline_qa(context = context,
|
| 80 |
+
question = question)
|
| 81 |
+
|
| 82 |
+
out_text = context[0:res["start"]] + '<span style="background-color:lightgreen;border-radius: 3px;padding: 3px;"><b>α΄Ι΄s </b> ' + context[res["start"]:res["end"]] + '</span>' + context[res["end"]:]
|
| 83 |
+
|
| 84 |
+
return out_text
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
init_question= "Cos'Γ¨ l'Agenzia Spaziale Italiana?"
|
| 88 |
+
|
| 89 |
+
init_context = '''L'Agenzia Spaziale Italiana (ASI) Γ¨ un ente governativo italiano, istituito nel 1988, che ha il compito di predisporre e attuare la politica aerospaziale italiana. Dipende e utilizza i fondi ricevuti dal Governo italiano per finanziare il progetto, lo sviluppo e la gestione operativa di missioni spaziali, con obiettivi scientifici e applicativi.
|
| 90 |
+
Gestisce missioni spaziali in proprio e in collaborazione con i maggiori organismi spaziali internazionali, prima tra tutte l'Agenzia Spaziale Europea (dove l'Italia Γ¨ il terzo maggior contribuente dopo Francia e Germania, e a cui l'ASI corrisponde una parte del proprio budget), quindi la NASA e le altre agenzie spaziali nazionali. Per la realizzazione di satelliti e strumenti scientifici, l'ASI stipula contratti con le imprese, italiane e non, operanti nel settore aerospaziale.
|
| 91 |
+
Ha la sede principale a Roma e centri operativi a Matera (sede del Centro di geodesia spaziale Giuseppe Colombo) e Malindi, Kenya (sede del Centro spaziale Luigi Broglio). Il centro di Trapani-Milo, usato per i lanci di palloni stratosferici dal 1975, non Γ¨ piΓΉ operativo dal 2010.'''
|
| 92 |
+
|
| 93 |
+
init_output = extract(question = init_question, context = init_context)
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
with gr.Blocks(css="footer {visibility: hidden}", theme=gr.themes.Default(text_size="lg", spacing_size="lg")) as interface:
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
gr.Markdown(header)
|
| 100 |
+
with gr.Row():
|
| 101 |
+
context = gr.Text(label="Context", lines = 10, value = init_context)
|
| 102 |
+
with gr.Row():
|
| 103 |
+
question = gr.Text(label="Question", lines = 1, value = init_question)
|
| 104 |
+
with gr.Row():
|
| 105 |
+
gr.Examples([["Cosa fa l'Agenzia Spaziale Italiana?"],
|
| 106 |
+
["Qual Γ¨ la sigla dell'Agenzia Spaziale Italiana?"],
|
| 107 |
+
["Quando Γ¨ stata fondata l'ASI?"],
|
| 108 |
+
["Chi finanzia l'ASI?"],
|
| 109 |
+
["Chi altro contribuisce all'Agenzia Spaziale Europea oltre all'Italia?"],
|
| 110 |
+
["Dove ha sede l'Agenzia Spaziale Italiana?"],
|
| 111 |
+
["Dove si trova il centro spaziale Giuseppe Colombo?"],
|
| 112 |
+
["Dove si trova il centro spaziale Luigi Broglio?"],
|
| 113 |
+
["Il centro di Trapani-Milo Γ¨ ancora in funzione?"]],
|
| 114 |
+
inputs=[question])
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
button = gr.Button("Ask")
|
| 118 |
+
with gr.Row():
|
| 119 |
+
with gr.Column():
|
| 120 |
+
output = gr.Markdown(init_output)
|
| 121 |
+
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column():
|
| 124 |
+
gr.Markdown("<center>The input examples in this demo are extracted from https://it.wikipedia.org</center>")
|
| 125 |
+
|
| 126 |
+
button.click(extract, inputs=[question, context], outputs = [output])
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
interface.launch()
|