bilalfaye commited on
Commit
ba8613b
·
verified ·
1 Parent(s): 2c9247c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import torch
4
+
5
+ # Load the model and tokenizer
6
+ model_name = 'bilalfaye/nllb-200-distilled-600M-wo-fr-en'
7
+ device = "cuda" if torch.cuda.is_available() else "cpu"
8
+
9
+ # Define the translation pipeline
10
+ translator = pipeline(
11
+ "translation",
12
+ model=model_name,
13
+ device=device
14
+ )
15
+
16
+
17
+ # Define the translation function
18
+ def translate_chat(message, history, source_language, target_language):
19
+ # Mapping of languages to model codes
20
+ lang_map = {
21
+ "Wolof": "wol_Latn",
22
+ "English": "eng_Latn",
23
+ "French": "fra_Latn"
24
+ }
25
+
26
+ if source_language not in lang_map or target_language not in lang_map:
27
+ return "Invalid language selection."
28
+
29
+ src_lang = lang_map[source_language]
30
+ tgt_lang = lang_map[target_language]
31
+
32
+ if src_lang == tgt_lang:
33
+ return "Source and target languages must be different."
34
+
35
+ # Perform the translation
36
+ translation = translator(message, src_lang=src_lang, tgt_lang=tgt_lang)
37
+ return translation[0]["translation_text"]
38
+
39
+ # Gradio chat interface
40
+ interface = gr.ChatInterface(
41
+ fn=translate_chat,
42
+ additional_inputs=[
43
+ gr.Dropdown(
44
+ choices=["Wolof", "French", "English"],
45
+ label="Source Language",
46
+ value="Wolof", # Default value
47
+ ),
48
+ gr.Dropdown(
49
+ choices=["Wolof", "French", "English"],
50
+ label="Target Language",
51
+ value="English", # Default value
52
+ ),
53
+ ],
54
+ title="Wolof ↔ French ↔ English Translator",
55
+ description="Select the source and target languages (in the bottom) to translate between Wolof, French, and English.",
56
+ )
57
+
58
+ # Launch the app
59
+ interface.launch(debug=True, share=True)