Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import json
|
| 4 |
+
from transformers import pipeline
|
| 5 |
+
|
| 6 |
+
# Load the translation pipeline
|
| 7 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
| 8 |
+
|
| 9 |
+
# Load the JSON data for language codes
|
| 10 |
+
with open('language.json', 'r') as file:
|
| 11 |
+
language_data = json.load(file)
|
| 12 |
+
|
| 13 |
+
# Get all available languages (excluding duplicates with different scripts)
|
| 14 |
+
available_languages = []
|
| 15 |
+
seen_languages = set()
|
| 16 |
+
for entry in language_data:
|
| 17 |
+
base_language = entry['Language'].split('(')[0].strip()
|
| 18 |
+
if base_language not in seen_languages:
|
| 19 |
+
available_languages.append(base_language)
|
| 20 |
+
seen_languages.add(base_language)
|
| 21 |
+
|
| 22 |
+
# Sort languages alphabetically
|
| 23 |
+
available_languages.sort()
|
| 24 |
+
|
| 25 |
+
# Function to retrieve FLORES-200 code for a given language
|
| 26 |
+
def get_FLORES_code_from_language(language):
|
| 27 |
+
# Try exact match first
|
| 28 |
+
for entry in language_data:
|
| 29 |
+
if entry['Language'].lower() == language.lower():
|
| 30 |
+
return entry['FLORES-200 code']
|
| 31 |
+
# Fallback to matching base language name
|
| 32 |
+
for entry in language_data:
|
| 33 |
+
if entry['Language'].lower().startswith(language.lower()):
|
| 34 |
+
return entry['FLORES-200 code']
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# Translation function
|
| 38 |
+
def translate_text(text, destination_language):
|
| 39 |
+
dest_code = get_FLORES_code_from_language(destination_language)
|
| 40 |
+
if dest_code is None:
|
| 41 |
+
return f"Error: Could not find FLORES code for language {destination_language}"
|
| 42 |
+
|
| 43 |
+
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code)
|
| 44 |
+
return translation[0]["translation_text"]
|
| 45 |
+
|
| 46 |
+
# Speech-to-text pipeline
|
| 47 |
+
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-small")
|
| 48 |
+
|
| 49 |
+
# Function to transcribe audio to text
|
| 50 |
+
def transcribe_audio(audio_file, destination_language):
|
| 51 |
+
transcription = speech_to_text(audio_file)["text"]
|
| 52 |
+
return translate_text(transcription, destination_language)
|
| 53 |
+
|
| 54 |
+
# Gradio interface
|
| 55 |
+
with gr.Blocks() as demo:
|
| 56 |
+
gr.Markdown("# AI-Powered Language Translation Bot")
|
| 57 |
+
gr.Markdown(
|
| 58 |
+
"This bot allows you to translate English text to various languages or convert audio input into transcribed text and translate it into your desired language."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
with gr.Tabs():
|
| 62 |
+
with gr.Tab("Text Translation"):
|
| 63 |
+
text_input = gr.Textbox(label="Input text to translate", lines=6)
|
| 64 |
+
language_dropdown = gr.Dropdown(
|
| 65 |
+
choices=available_languages, label="Select Destination Language"
|
| 66 |
+
)
|
| 67 |
+
translated_text_output = gr.Textbox(label="Translated Text", lines=4)
|
| 68 |
+
translate_button = gr.Button("Translate")
|
| 69 |
+
|
| 70 |
+
translate_button.click(
|
| 71 |
+
translate_text, inputs=[text_input, language_dropdown], outputs=[translated_text_output]
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
with gr.Tab("Audio Translation"):
|
| 75 |
+
audio_input = gr.Audio(label="Upload Audio File", type="filepath")
|
| 76 |
+
audio_language_dropdown = gr.Dropdown(
|
| 77 |
+
choices=available_languages, label="Select Destination Language"
|
| 78 |
+
)
|
| 79 |
+
audio_translated_text_output = gr.Textbox(label="Transcribed and Translated Text", lines=4)
|
| 80 |
+
audio_translate_button = gr.Button("Transcribe and Translate")
|
| 81 |
+
|
| 82 |
+
audio_translate_button.click(
|
| 83 |
+
transcribe_audio,
|
| 84 |
+
inputs=[audio_input, audio_language_dropdown],
|
| 85 |
+
outputs=[audio_translated_text_output],
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|