Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
|
4 |
+
from transformers import T5ForConditionalGeneration, AutoTokenizer
|
5 |
+
# from transformers import pipeline
|
6 |
+
|
7 |
+
auth_token = os.environ.get("CLARIN_KNEXT")
|
8 |
+
|
9 |
+
model_name = "clarin-knext/plt5-large-poquad-ext-qa-autotoken"
|
10 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=auth_token)
|
11 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name, use_auth_token=auth_token)
|
12 |
+
|
13 |
+
|
14 |
+
default_generate_kwargs = {
|
15 |
+
"max_length": 192,
|
16 |
+
"num_beams": 2,
|
17 |
+
"length_penalty": 0,
|
18 |
+
"early_stopping": True,
|
19 |
+
}
|
20 |
+
|
21 |
+
# keywords_pipe = pipeline(model=model, tokenizer=tokenizer, **default_generate_kwargs)
|
22 |
+
|
23 |
+
examples = [
|
24 |
+
["extract question: Kiedy Arek ma spotkanie dzisiaj? context: Arek ma spotkanie przez cały dzeiń. Bardzo to lubi."]
|
25 |
+
|
26 |
+
|
27 |
+
|
28 |
+
def generate(context):
|
29 |
+
context = context + " </s>"
|
30 |
+
inputs = tokenizer(
|
31 |
+
context,
|
32 |
+
max_length=512,
|
33 |
+
add_special_tokens=True,
|
34 |
+
truncation=True,
|
35 |
+
padding=False,
|
36 |
+
return_tensors="pt"
|
37 |
+
)
|
38 |
+
|
39 |
+
outs = model.generate(
|
40 |
+
input_ids=inputs['input_ids'],
|
41 |
+
attention_mask=inputs['attention_mask'],
|
42 |
+
**default_generate_kwargs
|
43 |
+
)
|
44 |
+
prediction = tokenizer.decode(outs[0], skip_special_tokens=True)
|
45 |
+
return prediction
|
46 |
+
|
47 |
+
demo = gr.Interface(
|
48 |
+
fn=generate,
|
49 |
+
inputs=gr.inputs.Textbox(lines=5, label="Input Text"),
|
50 |
+
outputs=gr.outputs.Textbox(label="Keywords"),
|
51 |
+
examples=examples,
|
52 |
+
)
|
53 |
+
|
54 |
+
demo.launch()
|