File size: 1,689 Bytes
659981a
e0080b4
ec09ed1
f577737
e0080b4
659981a
83fef69
ad9e152
 
83fef69
ad9e152
83fef69
ad9e152
83fef69
ad9e152
 
 
 
0fbc2ce
ec09ed1
ad9e152
225c9d9
e0080b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import streamlit as st
import speech_recognition as sr
from transformers import pipeline

st.title("Speech-to-Text and Translation Chatbot")

# Choose the translation models from Hugging Face
translation_models = {
    "English to German": "Helsinki-NLP/opus-mt-en-de",
    "German to English": "Helsinki-NLP/opus-mt-de-en",
    "English to French": "Helsinki-NLP/opus-mt-en-fr",
    "French to English": "Helsinki-NLP/opus-mt-fr-en",
    "English to Urdu": "Helsinki-NLP/opus-mt-en-ur",
    "Urdu to English": "Helsinki-NLP/opus-mt-ur-en",
    # Add more language pairs as needed
}

selected_translation = st.selectbox("Select translation model", list(translation_models.keys()))

# Load the translation pipeline
translator = pipeline(task="translation", model=translation_models[selected_translation])

# Function to perform speech-to-text and translation
def speech_to_text_and_translate():
    recognizer = sr.Recognizer()

    st.info("Speak into your microphone...")

    with sr.Microphone() as source:
        audio_data = recognizer.listen(source, timeout=10)

    try:
        text = recognizer.recognize_google(audio_data)
        st.success(f"Speech-to-Text Result: {text}")

        # Perform translation
        translated_text = translator(text, max_length=500)[0]['translation_text']
        st.success(f"Translated Text: {translated_text}")

    except sr.UnknownValueError:
        st.warning("Speech recognition could not understand audio.")
    except sr.RequestError as e:
        st.error(f"Error with the speech recognition service: {e}")

# Use speech-to-text and translation
if st.button("Start Speech-to-Text and Translation"):
    speech_to_text_and_translate()