kasper-boy commited on
Commit
d3087fa
1 Parent(s): 4e531b9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
+
4
+ # Load pre-trained model and tokenizer
5
+ model_name = "t5-small"
6
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+
9
+ # Function to translate text
10
+ def translate_text(text, source_lang, target_lang):
11
+ input_text = f"translate {source_lang} to {target_lang}: {text}"
12
+ inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True)
13
+ outputs = model.generate(**inputs)
14
+ translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
15
+ return translation
16
+
17
+ # List of Indian languages
18
+ indian_languages = [
19
+ "as", "bn", "gu", "hi", "kn", "ml", "mr", "or", "pa", "ta", "te", "ur"
20
+ ]
21
+
22
+ # Supported languages
23
+ languages = ["en", "fr", "de", "es", "it"] + indian_languages
24
+
25
+ # Create Gradio interface
26
+ def translate_interface(text, source_lang, target_lang):
27
+ return translate_text(text, source_lang, target_lang)
28
+
29
+ iface = gr.Interface(
30
+ fn=translate_interface,
31
+ inputs=[
32
+ gr.Textbox(lines=2, placeholder="Enter text to translate"),
33
+ gr.Dropdown(choices=languages, label="Source Language"),
34
+ gr.Dropdown(choices=languages, label="Target Language")
35
+ ],
36
+ outputs="text",
37
+ title="Hugging Face Translation App",
38
+ description="Translate text from one language to another using a T5 model."
39
+ )
40
+
41
+ if __name__ == "__main__":
42
+ iface.launch()