versae commited on
Commit
c131f81
1 Parent(s): 263bcbf

Add file IO

Browse files
Files changed (1) hide show
  1. app.py +6 -6
app.py CHANGED
@@ -13,21 +13,21 @@ def modernisa(lines=None, file_obj=None):
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
31
 
32
 
33
  samples = [
@@ -55,4 +55,4 @@ gr.Interface(
55
  gr.outputs.HTML(label="Modern Spanish"),
56
  gr.outputs.File(label="Download file"),
57
  ]
58
- ).launch(inline=False)
 
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 = [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, "w") as output:
29
+ output.write("\n".join(generated_text))
30
+ return "<br/>".join(generated_text), output
31
 
32
 
33
  samples = [
 
55
  gr.outputs.HTML(label="Modern Spanish"),
56
  gr.outputs.File(label="Download file"),
57
  ]
58
+ ).launch(inline=False)