millan24 commited on
Commit
501f937
·
verified ·
1 Parent(s): 4ee2a49

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import gradio as gr
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ import torch
5
+
6
+ MODEL_NAME = "millan24/opus-books-en-fr-finetuned"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
9
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
10
+ model.eval()
11
+
12
+ def translate(text):
13
+ inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
14
+ with torch.no_grad():
15
+ outputs = model.generate(**inputs, max_length=128)
16
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
17
+
18
+ demo = gr.Interface(
19
+ fn=translate,
20
+ inputs=gr.Textbox(lines=4, placeholder="Enter English text here..."),
21
+ outputs=gr.Textbox(lines=4, label="French translation"),
22
+ title="English → French Translator",
23
+ description="Fine-tuned model on opus_books dataset (en-fr)",
24
+ examples=[
25
+ ["The sun was setting over the mountains."],
26
+ ["She opened the door and stepped into the room."],
27
+ ["It was a cold and rainy night in Paris."],
28
+ ]
29
+ )
30
+
31
+ demo.launch()