Dddixyy commited on
Commit
b56eec1
·
verified ·
1 Parent(s): 5ece9aa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -9
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import gradio as gr
2
  from transformers import MarianMTModel, MarianTokenizer
3
  import torch
 
4
 
5
  # Load the model and tokenizer from the Hub
6
  model_name = "Dddixyy/latin-italian-translatorV2"
@@ -9,14 +10,24 @@ model = MarianMTModel.from_pretrained(model_name)
9
 
10
  # Translation function
11
  def translate_latin_to_italian(latin_text):
12
- # Make the first letter lowercase if the input is not empty
13
- if latin_text:
14
- latin_text = latin_text[0].lower() + latin_text[1:]
15
- inputs = tokenizer(latin_text, return_tensors="pt", padding=True, truncation=True)
16
- with torch.no_grad():
17
- generated_ids = model.generate(inputs["input_ids"])
18
- translation = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
19
- return translation[0]
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Define the Gradio interface
22
  interface = gr.Interface(
@@ -35,4 +46,4 @@ interface = gr.Interface(
35
 
36
  # Launch the app
37
  if __name__ == "__main__":
38
- interface.launch()
 
1
  import gradio as gr
2
  from transformers import MarianMTModel, MarianTokenizer
3
  import torch
4
+ import re
5
 
6
  # Load the model and tokenizer from the Hub
7
  model_name = "Dddixyy/latin-italian-translatorV2"
 
10
 
11
  # Translation function
12
  def translate_latin_to_italian(latin_text):
13
+ # Split input text into sentences while preserving line breaks
14
+ sentences = re.split(r'(?<=[.!?]) +', latin_text.strip())
15
+
16
+ translated_sentences = []
17
+
18
+ for sentence in sentences:
19
+ # Make the first letter lowercase if the sentence is not empty
20
+ if sentence:
21
+ sentence = sentence[0].lower() + sentence[1:]
22
+ inputs = tokenizer(sentence, return_tensors="pt", padding=True, truncation=True)
23
+ with torch.no_grad():
24
+ generated_ids = model.generate(inputs["input_ids"])
25
+ translation = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
26
+ translated_sentences.append(translation[0])
27
+
28
+ # Reassemble the translated sentences and keep original line breaks
29
+ translated_text = ' '.join(translated_sentences)
30
+ return translated_text
31
 
32
  # Define the Gradio interface
33
  interface = gr.Interface(
 
46
 
47
  # Launch the app
48
  if __name__ == "__main__":
49
+ interface.launch()