Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,20 @@
|
|
1 |
-
import
|
2 |
-
from transformers import pipeline
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
asr_pipeline = pipeline("automatic-speech-recognition", model="vrclc/w2v2bert_malayalam")
|
9 |
-
|
10 |
-
# Translation function
|
11 |
def translate(text):
|
12 |
-
|
|
|
|
|
|
|
13 |
return translated_text
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
# Interface setup
|
22 |
-
iface = gr.Interface(
|
23 |
-
fn=[translate, recognize_speech],
|
24 |
-
inputs=[
|
25 |
-
gr.Textbox(lines=5, label="Enter English Text to Translate"),
|
26 |
-
gr.File(label="Upload Audio File for Speech Recognition")
|
27 |
-
],
|
28 |
-
outputs=[
|
29 |
-
gr.Textbox(label="Malayalam Translation"),
|
30 |
-
gr.Textbox(label="Malayalam Speech Recognition")
|
31 |
-
],
|
32 |
-
title="English to Malayalam Translation and Speech Recognition",
|
33 |
-
theme="compact"
|
34 |
-
)
|
35 |
-
|
36 |
-
# Launch the interface
|
37 |
-
iface.launch()
|
|
|
1 |
+
from transformers import MarianMTModel, MarianTokenizer
|
|
|
2 |
|
3 |
+
# Step 1: Load the model and tokenizer
|
4 |
+
model_name = "Helsinki-NLP/opus-mt-en-ml"
|
5 |
+
tokenizer = MarianTokenizer.from_pretrained(model_name)
|
6 |
+
model = MarianMTModel.from_pretrained(model_name)
|
7 |
|
8 |
+
# Step 2: Define a translation function
|
|
|
|
|
|
|
9 |
def translate(text):
|
10 |
+
inputs = tokenizer(text, return_tensors="pt", padding=True)
|
11 |
+
# Perform translation
|
12 |
+
outputs = model.generate(**inputs, max_length=128, num_beams=4, early_stopping=True)
|
13 |
+
translated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
14 |
return translated_text
|
15 |
|
16 |
+
# Step 3: Example usage
|
17 |
+
english_text = "Hello, how are you?"
|
18 |
+
malayalam_translation = translate(english_text)
|
19 |
+
print(f"English: {english_text}")
|
20 |
+
print(f"Malayalam: {malayalam_translation}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|