deepugaur commited on
Commit
d4eb552
1 Parent(s): 616ed18

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st # Make sure to import streamlit
2
+ import torch
3
+ from transformers import MarianMTModel, MarianTokenizer
4
+ import speech_recognition as sr
5
+ from gtts import gTTS
6
+ import os
7
+
8
+ # Function to recognize speech from an audio file
9
+ def recognize_speech_from_file(audio_file):
10
+ recognizer = sr.Recognizer()
11
+ with sr.AudioFile(audio_file) as source:
12
+ audio = recognizer.record(source)
13
+ try:
14
+ return recognizer.recognize_google(audio)
15
+ except sr.UnknownValueError:
16
+ return "Could not understand audio"
17
+ except sr.RequestError:
18
+ return "Could not request results; check your internet connection"
19
+
20
+ # Function to translate text
21
+ def translate_text(text):
22
+ model_name = "Helsinki-NLP/opus-mt-en-hi"
23
+ tokenizer = MarianTokenizer.from_pretrained(model_name)
24
+ model = MarianMTModel.from_pretrained(model_name)
25
+ translated = model.generate(**tokenizer(text, return_tensors="pt", padding=True))
26
+ return tokenizer.decode(translated[0], skip_special_tokens=True)
27
+
28
+ # Function to convert text to audio
29
+ def convert_to_audio(text):
30
+ tts = gTTS(text, lang="hi")
31
+ audio_output = "output.mp3"
32
+ tts.save(audio_output)
33
+ return audio_output
34
+
35
+ st.title("English to Hindi Audio Translation")
36
+
37
+ # Make the translation service available without time restrictions
38
+ audio_file = st.file_uploader("Upload an audio file", type=["wav", "mp3"])
39
+ if audio_file is not None:
40
+ text = recognize_speech_from_file(audio_file)
41
+ st.write(f"Recognized English Text: {text}")
42
+ translated_text = translate_text(text)
43
+ st.write(f"Translated Hindi Text: {translated_text}")
44
+ audio_output = convert_to_audio(translated_text)
45
+ st.audio(audio_output, format='audio/mp3')