tanya17 commited on
Commit
f35102e
·
verified ·
1 Parent(s): 1cdca66

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -0
app.py ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2
+ import gradio as gr
3
+
4
+ # Correct model name for English to Amharic
5
+ model_name = "Helsinki-NLP/opus-mt-en-cus" # Cus = Cushitic languages (includes Amharic)
6
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
7
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
8
+
9
+ def translate(text):
10
+ if not text.strip():
11
+ return "⚠️ Please enter some text."
12
+
13
+ try:
14
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
15
+ outputs = model.generate(**inputs)
16
+ return tokenizer.decode(outputs[0], skip_special_tokens=True)
17
+ except Exception as e:
18
+ return f"⚠️ Translation error: {str(e)}"
19
+
20
+ demo = gr.Interface(
21
+ fn=translate,
22
+ inputs=gr.Textbox(lines=3, label="Enter English Text"),
23
+ outputs=gr.Textbox(label="Amharic Translation"),
24
+ title="🌍 English to Amharic Translator",
25
+ description="✔ Powered by Helsinki-NLP model.",
26
+ examples=[
27
+ ["Good morning"],
28
+ ["Thank you very much"],
29
+ ["How much does this cost?"]
30
+ ]
31
+ )
32
+
33
+ if __name__ == "__main__":
34
+ demo.launch()