Update app.py
Browse files
app.py
CHANGED
|
@@ -7,19 +7,24 @@ tokenizer = AutoTokenizer.from_pretrained(model_name)
|
|
| 7 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def modernisa(lines=None, file_obj=None):
|
|
|
|
| 10 |
if file_obj:
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
with open(output_file) as output:
|
| 24 |
output.write(generated_text)
|
| 25 |
return generated_text, output
|
|
@@ -43,11 +48,11 @@ porque el pensamiento creo"""
|
|
| 43 |
gr.Interface(
|
| 44 |
fn=modernisa,
|
| 45 |
inputs=[
|
| 46 |
-
gr.inputs.Textbox(lines=12, label="Enter
|
| 47 |
gr.inputs.File(file_count="single", label="Or upload a plain text file (.txt)", type="file", optional=True),
|
| 48 |
],
|
| 49 |
outputs=[
|
| 50 |
gr.outputs.HTML(label="Modern Spanish"),
|
| 51 |
-
gr.outputs.File(label="Download file")
|
| 52 |
]
|
| 53 |
).launch(inline=False)
|
|
|
|
| 7 |
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def modernisa(lines=None, file_obj=None):
|
| 10 |
+
is_file_valid = False
|
| 11 |
if file_obj:
|
| 12 |
+
is_file_valid = file_obj.name.endswith(".txt")
|
| 13 |
+
if is_file_valid:
|
| 14 |
+
with open(file_obj.name) as file:
|
| 15 |
+
lines = file.read()
|
| 16 |
+
generated_text = ""
|
| 17 |
+
if lines:
|
| 18 |
+
lines = [line for line in lines.strip().split('\n') if line.strip()]
|
| 19 |
+
text2text_generator = pipeline("text2text-generation", model=model, tokenizer=tokenizer)
|
| 20 |
+
outputs = []
|
| 21 |
+
for line in lines:
|
| 22 |
+
outputs += text2text_generator([line], max_length=150)
|
| 23 |
+
generated_text = "<br/>".join(output["generated_text"] for output in outputs)
|
| 24 |
+
if is_file_valid and file_obj:
|
| 25 |
+
output_file = f"{file_obj.name.rsplit('.', 1)[0]}_modernized.txt"
|
| 26 |
+
else:
|
| 27 |
+
output_file = "modernized.txt"
|
| 28 |
with open(output_file) as output:
|
| 29 |
output.write(generated_text)
|
| 30 |
return generated_text, output
|
|
|
|
| 48 |
gr.Interface(
|
| 49 |
fn=modernisa,
|
| 50 |
inputs=[
|
| 51 |
+
gr.inputs.Textbox(lines=12, label="Enter Spanish Golden Age text", default=random.choice(samples)),
|
| 52 |
gr.inputs.File(file_count="single", label="Or upload a plain text file (.txt)", type="file", optional=True),
|
| 53 |
],
|
| 54 |
outputs=[
|
| 55 |
gr.outputs.HTML(label="Modern Spanish"),
|
| 56 |
+
gr.outputs.File(label="Download file"),
|
| 57 |
]
|
| 58 |
).launch(inline=False)
|