Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import gradio as gr
|
3 |
+
import json
|
4 |
+
from transformers import pipeline
|
5 |
+
|
6 |
+
# Use a pipeline with the specified model for translation
|
7 |
+
text_translator = pipeline("translation", model="facebook/nllb-200-distilled-600M", torch_dtype=torch.bfloat16)
|
8 |
+
|
9 |
+
# Load the JSON data from the file
|
10 |
+
with open('language.json', 'r') as file:
|
11 |
+
language_data = json.load(file)
|
12 |
+
|
13 |
+
def get_FLORES_code_from_language(language):
|
14 |
+
for entry in language_data:
|
15 |
+
if entry['Language'].lower() == language.lower():
|
16 |
+
return entry['FLORES-200 code']
|
17 |
+
return None
|
18 |
+
|
19 |
+
def translate_text(text, source_language, destination_language):
|
20 |
+
src_code = get_FLORES_code_from_language(source_language)
|
21 |
+
dest_code = get_FLORES_code_from_language(destination_language)
|
22 |
+
translation = text_translator(text, src_lang=src_code, tgt_lang=dest_code)
|
23 |
+
return translation[0]["translation_text"]
|
24 |
+
|
25 |
+
gr.close_all()
|
26 |
+
|
27 |
+
# Define the language options based on the JSON file
|
28 |
+
language_options = [entry['Language'] for entry in language_data]
|
29 |
+
|
30 |
+
demo = gr.Interface(
|
31 |
+
fn=translate_text,
|
32 |
+
inputs=[
|
33 |
+
gr.Textbox(label="Input text to translate", lines=6),
|
34 |
+
gr.Dropdown(language_options, label="Select Source Language"),
|
35 |
+
gr.Dropdown(language_options, label="Select Destination Language")
|
36 |
+
],
|
37 |
+
outputs=[gr.Textbox(label="Translated text", lines=4)],
|
38 |
+
title="@GenAILearniverse Project 4: Multi-language Translator",
|
39 |
+
description="THIS APPLICATION WILL BE USED TO TRANSLATE TEXT BETWEEN MULTIPLE LANGUAGES."
|
40 |
+
)
|
41 |
+
|
42 |
+
demo.launch()
|