|
import torch |
|
import gradio as gr |
|
import json |
|
from transformers import pipeline |
|
|
|
|
|
text_translator = pipeline( |
|
"translation", |
|
model="facebook/nllb-200-distilled-600M", |
|
torch_dtype=torch.bfloat16 |
|
) |
|
|
|
|
|
with open('language.json', 'r') as file: |
|
language_data = json.load(file) |
|
|
|
|
|
available_languages = [] |
|
seen_languages = set() |
|
for entry in language_data: |
|
base_language = entry['Language'].split('(')[0].strip() |
|
if base_language not in seen_languages: |
|
available_languages.append(base_language) |
|
seen_languages.add(base_language) |
|
|
|
|
|
available_languages.sort() |
|
|
|
|
|
def get_FLORES_code_from_language(language): |
|
|
|
for entry in language_data: |
|
if entry['Language'].lower() == language.lower(): |
|
return entry['FLORES-200 code'] |
|
|
|
for entry in language_data: |
|
if entry['Language'].lower().startswith(language.lower()): |
|
return entry['FLORES-200 code'] |
|
return None |
|
|
|
|
|
def translate_text(text, destination_language): |
|
dest_code = get_FLORES_code_from_language(destination_language) |
|
if dest_code is None: |
|
return f"Error: Could not find FLORES code for language {destination_language}" |
|
|
|
try: |
|
|
|
translation = text_translator(text, src_lang="eng_Latn", tgt_lang=dest_code) |
|
return translation[0]["translation_text"] |
|
except Exception as e: |
|
return f"Error during translation: {str(e)}" |
|
|
|
|
|
speech_to_text = pipeline("automatic-speech-recognition", model="openai/whisper-small") |
|
|
|
|
|
def transcribe_audio(audio_file, destination_language): |
|
try: |
|
transcription_result = speech_to_text(audio_file) |
|
print(f"Transcription result: {transcription_result}") |
|
if "text" in transcription_result: |
|
transcription = transcription_result["text"] |
|
else: |
|
return "Error: Unable to transcribe audio." |
|
|
|
|
|
return translate_text(transcription, destination_language) |
|
except Exception as e: |
|
return f"Error during transcription: {str(e)}" |
|
|
|
|
|
with gr.Blocks(css=""" |
|
.background { |
|
background: linear-gradient(135deg, #0f2027, #203a43, #2c5364); |
|
color: white; |
|
font-family: 'Arial', sans-serif; |
|
} |
|
button { |
|
background-color: #4CAF50; |
|
color: white; |
|
border-radius: 5px; |
|
border: none; |
|
padding: 10px 20px; |
|
font-size: 16px; |
|
cursor: pointer; |
|
transition: background-color 0.3s; |
|
} |
|
button:hover { |
|
background-color: #45a049; |
|
} |
|
input, textarea, select { |
|
background-color: #f9f9f9; |
|
color: black; |
|
border-radius: 5px; |
|
border: 1px solid #ddd; |
|
padding: 10px; |
|
} |
|
""") as demo: |
|
with gr.Row(elem_classes="background"): |
|
gr.Markdown( |
|
""" |
|
# π AI-Powered Translation Chatbot |
|
**Translate text or audio into your desired language with professional precision.** |
|
""", |
|
elem_classes="background" |
|
) |
|
|
|
with gr.Tabs(elem_classes="background"): |
|
with gr.Tab("Text Translation"): |
|
text_input = gr.Textbox(label="Input Text", lines=6, placeholder="Enter your text here...") |
|
language_dropdown = gr.Dropdown( |
|
choices=available_languages, label="Select Destination Language" |
|
) |
|
translated_text_output = gr.Textbox(label="Translated Text", lines=4) |
|
translate_button = gr.Button("Translate") |
|
|
|
translate_button.click( |
|
translate_text, inputs=[text_input, language_dropdown], outputs=[translated_text_output] |
|
) |
|
|
|
with gr.Tab("Audio Translation"): |
|
audio_input = gr.Audio(label="Upload Audio File", type="filepath") |
|
audio_language_dropdown = gr.Dropdown( |
|
choices=available_languages, label="Select Destination Language" |
|
) |
|
audio_translated_text_output = gr.Textbox(label="Transcribed and Translated Text", lines=4) |
|
audio_translate_button = gr.Button("Transcribe and Translate") |
|
|
|
audio_translate_button.click( |
|
transcribe_audio, |
|
inputs=[audio_input, audio_language_dropdown], |
|
outputs=[audio_translated_text_output], |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|