Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -24,61 +24,52 @@ languages = sorted([ 'Aragonese', 'Asturian', 'Basque', 'Bulgarian', 'Catalan',
|
|
24 |
def generate_output(task, source, target, input_text, mt_text=None):
|
25 |
date_string = datetime.today().strftime('%Y-%m-%d')
|
26 |
|
27 |
-
if task == "Translation":
|
28 |
-
prompt = f"Translate the following text from {source} into {target}.\n{source}: {input_text.strip()} \n{target}:"
|
29 |
-
elif task == "Post-editing":
|
30 |
-
if not mt_text:
|
31 |
-
return "Please provide machine translation (MT) for post-editing.", ""
|
32 |
-
prompt = f"Please fix any mistakes in the following {source}-{target} machine translation or keep it unedited if it's correct.\nSource: {input_text.strip()} \nMT: {mt_text.strip()} \nCorrected:"
|
33 |
-
elif task == "Document translation":
|
34 |
-
prompt = f"Please translate this text from {source} into {target}.\n{source}: {input_text.strip()}\n{target}:"
|
35 |
-
elif task == "Grammar checker":
|
36 |
-
prompt = f"Please fix any mistakes in the following {source} sentence or keep it unedited if it's correct.\nSentence: {input_text.strip()} \nCorrected:"
|
37 |
-
elif task == "Named-entity recognition":
|
38 |
-
prompt = """Analyse the following tokenized text and mark the tokens containing named entities.
|
39 |
-
Use the following annotation guidelines with these tags for named entities:
|
40 |
-
- ORG (Refers to named groups or organizations)
|
41 |
-
- PER (Refers to individual people or named groups of people)
|
42 |
-
- LOC (Refers to physical places or natural landmarks)
|
43 |
-
- MISC (Refers to entities that don't fit into standard categories).
|
44 |
-
Prepend B- to the first token of a given entity and I- to the remaining ones if they exist.
|
45 |
-
If a token is not a named entity, label it as O.
|
46 |
-
Input: """ + str(input_text.strip()) + "\nMarked:"
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
|
76 |
with gr.Blocks() as demo:
|
77 |
gr.Markdown("# 馃 SalamandraTA 7B - Multitask Demo")
|
78 |
gr.Markdown("Explore the translation, grammar correction, NER and post-editing capabilities of the SalamandraTA 7B model.")
|
79 |
|
80 |
with gr.Row():
|
81 |
-
task_selector = gr.Radio(["Translation", "
|
82 |
|
83 |
with gr.Row():
|
84 |
source_lang = gr.Dropdown(choices=languages, value="Catalan", label="Source Language")
|
@@ -97,8 +88,6 @@ with gr.Blocks() as demo:
|
|
97 |
["Translation", "Catalan", "Galician", "Als antics egipcis del per铆ode de l'Imperi Nou els fascinaven els monuments dels seus predecessors, que llavors tenien m茅s de mil anys.", ""],
|
98 |
["Post-editing", "Catalan", "English", "Rafael Nadal i Maria Magdalena van inspirar a una generaci贸 sencera.", "Rafael Christmas and Maria the Muffin inspired an entire generation each in their own way."],
|
99 |
["Grammar checker", "Catalan", "", "Entonses, el meu jefe m鈥檋a dit que he de treballar els fins de setmana.", ""],
|
100 |
-
["Named-entity recognition", "", "", "['La', 'defensa', 'del', 'antiguo', 'responsable', 'de', 'la', 'RFEF', 'confirma', 'que', 'interpondr谩', 'un', 'recurso.']", ""],
|
101 |
-
["Document translation", "English", "Asturian", doc_level_example, ""]
|
102 |
],
|
103 |
inputs=[task_selector, source_lang, target_lang, input_textbox, mt_textbox]
|
104 |
)
|
|
|
24 |
def generate_output(task, source, target, input_text, mt_text=None):
|
25 |
date_string = datetime.today().strftime('%Y-%m-%d')
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
sentences = input_text.split('\n')
|
29 |
+
sentences = [s for s in sentences if len(s.strip()) > 0]
|
30 |
+
generated_text = []
|
31 |
+
|
32 |
+
for sentence in sentences:
|
33 |
+
sentence = sentence.strip()
|
34 |
+
|
35 |
+
if task == "Translation":
|
36 |
+
prompt = f"Translate the following text from {source} into {target}.\n{source}: {sentence.strip()} \n{target}:"
|
37 |
+
elif task == "Post-editing":
|
38 |
+
if not mt_text:
|
39 |
+
return "Please provide machine translation (MT) for post-editing.", ""
|
40 |
+
prompt = f"Please fix any mistakes in the following {source}-{target} machine translation or keep it unedited if it's correct.\nSource: {sentence.strip()} \nMT: {mt_text.strip()} \nCorrected:"
|
41 |
+
elif task == "Grammar checker":
|
42 |
+
prompt = f"Please fix any mistakes in the following {source} sentence or keep it unedited if it's correct.\nSentence: {sentence.strip()} \nCorrected:"
|
43 |
+
|
44 |
+
messages = [{"role": "user", "content": prompt}]
|
45 |
+
final_prompt = tokenizer.apply_chat_template(
|
46 |
+
messages,
|
47 |
+
tokenize=False,
|
48 |
+
add_generation_prompt=True,
|
49 |
+
date_string=date_string
|
50 |
+
)
|
51 |
+
|
52 |
+
inputs = tokenizer(final_prompt, return_tensors="pt", add_special_tokens=False).to(model.device)
|
53 |
+
input_length = inputs.input_ids.shape[1]
|
54 |
+
|
55 |
+
output = model.generate(
|
56 |
+
input_ids=inputs.input_ids,
|
57 |
+
max_new_tokens=4000,
|
58 |
+
early_stopping=True,
|
59 |
+
num_beams=5
|
60 |
+
)
|
61 |
+
|
62 |
+
decoded = tokenizer.decode(output[0, input_length:], skip_special_tokens=True).strip()
|
63 |
+
generated_text.append(decoded)
|
64 |
+
|
65 |
+
return '\n'.join(generated_text), ""
|
66 |
|
67 |
with gr.Blocks() as demo:
|
68 |
gr.Markdown("# 馃 SalamandraTA 7B - Multitask Demo")
|
69 |
gr.Markdown("Explore the translation, grammar correction, NER and post-editing capabilities of the SalamandraTA 7B model.")
|
70 |
|
71 |
with gr.Row():
|
72 |
+
task_selector = gr.Radio(["Translation", "Post-editing", "Grammar checker"], value="Translation", label="Select Task")
|
73 |
|
74 |
with gr.Row():
|
75 |
source_lang = gr.Dropdown(choices=languages, value="Catalan", label="Source Language")
|
|
|
88 |
["Translation", "Catalan", "Galician", "Als antics egipcis del per铆ode de l'Imperi Nou els fascinaven els monuments dels seus predecessors, que llavors tenien m茅s de mil anys.", ""],
|
89 |
["Post-editing", "Catalan", "English", "Rafael Nadal i Maria Magdalena van inspirar a una generaci贸 sencera.", "Rafael Christmas and Maria the Muffin inspired an entire generation each in their own way."],
|
90 |
["Grammar checker", "Catalan", "", "Entonses, el meu jefe m鈥檋a dit que he de treballar els fins de setmana.", ""],
|
|
|
|
|
91 |
],
|
92 |
inputs=[task_selector, source_lang, target_lang, input_textbox, mt_textbox]
|
93 |
)
|