pmgautam commited on
Commit
f4eef28
·
1 Parent(s): 07cc1af

Add app and requirements file

Browse files
Files changed (3) hide show
  1. README.md +0 -11
  2. app.py +35 -0
  3. requirements.txt +3 -0
README.md CHANGED
@@ -1,13 +1,2 @@
1
- ---
2
- title: English To Nepali Translation
3
- colorFrom: yellow
4
- colorTo: red
5
- sdk: gradio
6
- sdk_version: 3.9
7
- app_file: app.py
8
- pinned: false
9
- license: apache-2.0
10
- ---
11
-
12
  This repo contains a gradio app to translate from English to Nepali.
13
  * Model used: [NLLB](https://huggingface.co/facebook/nllb-200-distilled-600M)
 
 
 
 
 
 
 
 
 
 
 
 
1
  This repo contains a gradio app to translate from English to Nepali.
2
  * Model used: [NLLB](https://huggingface.co/facebook/nllb-200-distilled-600M)
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # imports
2
+ import gradio as gr
3
+ import pandas as pd
4
+ import torch
5
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
6
+
7
+ # select GPU if available
8
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
9
+
10
+ # setup model and tokenizer
11
+ model = AutoModelForSeq2SeqLM.from_pretrained(
12
+ "facebook/nllb-200-distilled-600M").to(device)
13
+ tokenizer = AutoTokenizer.from_pretrained("facebook/nllb-200-distilled-600M")
14
+
15
+
16
+ def predict(text):
17
+ """_summary_
18
+ predict function to do translation task
19
+ """
20
+ text = [text]
21
+ inputs = tokenizer(text, return_tensors="pt", padding=True).to(device)
22
+
23
+ translated_tokens = model.generate(
24
+ **inputs, forced_bos_token_id=tokenizer.lang_code_to_id["npi_Deva"], max_length=30
25
+ )
26
+ return tokenizer.batch_decode(translated_tokens, skip_special_tokens=True)[0]
27
+
28
+
29
+ # call gradio interface
30
+ examples = ["use this example to see translation in nepali",
31
+ "this text is to test english to nepali translation"]
32
+ gr.Interface(fn=predict,
33
+ inputs=gr.Textbox(),
34
+ outputs=gr.Textbox(),
35
+ examples=[examples]).launch()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ torch==1.12.0
2
+ transformers==4.24.0
3
+ gradio==3.9