Skip to content

Commit

Permalink
Added real time translation model
Browse files Browse the repository at this point in the history
  • Loading branch information
770navyasharma authored Oct 5, 2024
1 parent 967d5a2 commit 59d90e4
Show file tree
Hide file tree
Showing 8 changed files with 202 additions and 0 deletions.
64 changes: 64 additions & 0 deletions Natural Language Processing/Translator-app/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 🌐 Real-Time Language Translator

A real-time language translation app built using Streamlit, Google Translate, and speech-to-text technology. This app allows users to speak in one language and get real-time translations in another, along with text-to-speech output for the translated text.

## Features
- **Speech Recognition:** Capture spoken input using a microphone.
- **Real-Time Translation:** Translate the captured speech into a chosen language.
- **Text-to-Speech:** Listen to the translated text in the target language.
- **Multiple Languages Supported:** Including English, Hindi, Tamil, Telugu, Marathi, Bengali, and more.

## Project Structure

streamlit_translation_app/ │ ├── main.py # Main Streamlit app file ├── translation.py # Core logic for speech capture, translation, and TTS ├── utils.py # Utility functions and language mappings ├── requirements.txt # Python dependencies ├── README.md # Project documentation └── assets/ └── styles.css # Custom CSS for UI


## Requirements
- Python 3.x
- A microphone for speech input

## Installation

1. Clone the repository:

```bash
git clone https://github.com/your_username/streamlit_translation_app.git
```

2. Navigate to the project directory:

```bash
cd streamlit_translation_app
```

3. Install the required dependencies:

```bash
pip install -r requirements.txt
```

4. Run the app:

```bash
streamlit run main.py
```

## How to Use

1. Select the source language (the language you will speak).
2. Select the target language (the language you want to translate to).
3. Click on **Start Listening** to capture speech.
4. Listen to the translated speech output and see the text on the screen.

## Customization

- **Language Support:** You can add more languages by updating the `LANGUAGES` dictionary in `utils.py`.
- **UI Styling:** Modify the `assets/styles.css` file to customize the look and feel of the app.

## Contributing

Feel free to contribute to this project by opening issues or submitting pull requests.

## License

This project is licensed under the MIT License. See `LICENSE` for more details.
Binary file not shown.
Binary file not shown.
24 changes: 24 additions & 0 deletions Natural Language Processing/Translator-app/assets/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
body {background-color: #0d1117; color: #c9d1d9;}
.main {padding: 20px;}
h1 {color: #58a6ff;}
.info {font-size: 18px; color: #58a6ff; animation: glow 1s infinite;}
.success {font-size: 18px; color: #34d058;}
.stButton > button {
background-color: #238636;
color: white;
border-radius: 12px;
padding: 10px 30px;
font-weight: bold;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.stButton > button:hover {
background-color: #2ea043;
}

@keyframes glow {
0% {box-shadow: 0 0 5px #58a6ff;}
50% {box-shadow: 0 0 20px #58a6ff;}
100% {box-shadow: 0 0 5px #58a6ff;}
}
37 changes: 37 additions & 0 deletions Natural Language Processing/Translator-app/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import streamlit as st
from translation import capture_and_translate
from utils import LANGUAGES
import time
import os

# Load custom CSS
def load_css():
with open("assets/styles.css") as f:
st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)

# UI Structure
def main():
st.title("🌐Real-Time Language Translator")
st.markdown("Translate spoken language into other languages in real-time with a sleek experience.")

load_css() # Load custom styling

# Language selection
source_lang_name = st.selectbox("🌍 Select Source Language", list(LANGUAGES.keys()))
target_lang_name = st.selectbox("🔄 Select Target Language", list(LANGUAGES.keys()))

source_lang = LANGUAGES[source_lang_name]
target_lang = LANGUAGES[target_lang_name]

# Button to start listening
if st.button("🎤 Start Listening", key="listen_button"):
audio_file = capture_and_translate(source_lang, target_lang)
if audio_file:
time.sleep(1) # Ensure pygame cleanup
try:
os.remove(audio_file)
except Exception as e:
st.error(f"⚠️ Error while deleting the file: {str(e)}")

if __name__ == "__main__":
main()
5 changes: 5 additions & 0 deletions Natural Language Processing/Translator-app/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
streamlit
googletrans==4.0.0rc1
gtts
pygame
SpeechRecognition
58 changes: 58 additions & 0 deletions Natural Language Processing/Translator-app/translation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import speech_recognition as sr
from googletrans import Translator
from gtts import gTTS
import pygame
import streamlit as st

# Initialize recognizer and translator
recognizer = sr.Recognizer()
translator = Translator()

# Function to capture and translate speech
def capture_and_translate(source_lang, target_lang):
with sr.Microphone() as source:
st.info("🎙️ Listening... Speak now.")

recognizer.adjust_for_ambient_noise(source, duration=1)
recognizer.energy_threshold = 200

try:
# Capture speech
audio = recognizer.listen(source, timeout=15, phrase_time_limit=15)
st.success("🔄 Processing...")

# Recognize speech
text = recognizer.recognize_google(audio, language=source_lang)
st.write(f"🗣️ Original ({source_lang}): {text}")

# Translate speech
translation = translator.translate(text, src=source_lang, dest=target_lang)
st.write(f"🔊 Translated ({target_lang}): {translation.text}")

# Convert translation to speech
tts = gTTS(text=translation.text, lang=target_lang)
audio_file = "translated_audio.mp3"
tts.save(audio_file)

# Play the audio
pygame.mixer.init()
pygame.mixer.music.load(audio_file)
pygame.mixer.music.play()

st.audio(audio_file)

while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)

pygame.mixer.music.stop()
pygame.mixer.quit()

return audio_file

except sr.WaitTimeoutError:
st.error("⚠️ No speech detected. Try speaking louder.")
except sr.UnknownValueError:
st.error("⚠️ Could not recognize speech.")
except Exception as e:
st.error(f"⚠️ Error: {str(e)}")
return None
14 changes: 14 additions & 0 deletions Natural Language Processing/Translator-app/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Available languages dictionary
LANGUAGES = {
'English': 'en',
'Hindi': 'hi',
'Tamil': 'ta',
'Telugu': 'te',
'Marathi': 'mr',
'Bengali': 'bn',
'Gujarati': 'gu',
'Kannada': 'kn',
'Malayalam': 'ml',
'Punjabi': 'pa',
'Urdu': 'ur'
}

0 comments on commit 59d90e4

Please sign in to comment.