oslohaze commited on
Commit
f2a7654
·
verified ·
1 Parent(s): 0260bf5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -32
app.py CHANGED
@@ -1,37 +1,20 @@
1
- import gradio as gr
2
- from transformers import pipeline
3
 
4
- # Translation pipeline
5
- translation_pipeline = pipeline("translation_en_to_ml")
 
 
6
 
7
- # ASR pipeline
8
- asr_pipeline = pipeline("automatic-speech-recognition", model="vrclc/w2v2bert_malayalam")
9
-
10
- # Translation function
11
  def translate(text):
12
- translated_text = translation_pipeline(text, max_length=40)[0]['translation_text']
 
 
 
13
  return translated_text
14
 
15
- # ASR function
16
- def recognize_speech(audio_file):
17
- result = asr_pipeline(audio_file)
18
- transcription = result[0]['transcription']
19
- return transcription
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}")