Spaces:
Sleeping
Sleeping
Sanjeeth
commited on
Add files via upload
Browse files- README.md +12 -1
- app.py +76 -0
- requirements.txt +4 -0
- test-file.py +26 -0
README.md
CHANGED
|
@@ -1 +1,12 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: MLOPS 1.1
|
| 3 |
+
emoji: 📉
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: pink
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Initialize the translation pipeline with T5-base model
|
| 6 |
+
translator = pipeline("translation", model="google-t5/t5-base")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
# Define available languages
|
| 10 |
+
AVAILABLE_LANGUAGES = ["French", "German", "Romanian"]
|
| 11 |
+
|
| 12 |
+
def translate(input_text, target_language):
|
| 13 |
+
target_language = target_language.lower()
|
| 14 |
+
|
| 15 |
+
# Set the task prefix based on the chosen language
|
| 16 |
+
if target_language == "french":
|
| 17 |
+
task_prefix = "translate English to French: "
|
| 18 |
+
elif target_language == "german":
|
| 19 |
+
task_prefix = "translate English to German: "
|
| 20 |
+
elif target_language == "romanian":
|
| 21 |
+
task_prefix = "translate English to Romanian: "
|
| 22 |
+
else:
|
| 23 |
+
return "Sorry, this language is not supported."
|
| 24 |
+
|
| 25 |
+
# Perform translation
|
| 26 |
+
full_input = task_prefix + input_text
|
| 27 |
+
translated = translator(full_input, max_length=128)
|
| 28 |
+
return translated[0]['translation_text']
|
| 29 |
+
|
| 30 |
+
def show_available_languages():
|
| 31 |
+
return ", ".join(AVAILABLE_LANGUAGES)
|
| 32 |
+
|
| 33 |
+
def run_tests():
|
| 34 |
+
test_cases = [
|
| 35 |
+
("Hello", "French"),
|
| 36 |
+
("How are you?", "German"),
|
| 37 |
+
("Good morning", "Romanian"),
|
| 38 |
+
("Hello", "Spanish")
|
| 39 |
+
]
|
| 40 |
+
|
| 41 |
+
results = []
|
| 42 |
+
for input_text, target_lang in test_cases:
|
| 43 |
+
try:
|
| 44 |
+
result = translate(input_text, target_lang)
|
| 45 |
+
status = "Passed" if result != "Sorry, this language is not supported." else "Failed"
|
| 46 |
+
results.append(f"Test '{input_text}' to {target_lang}: {status}\nResult: {result}")
|
| 47 |
+
except Exception as e:
|
| 48 |
+
results.append(f"Test '{input_text}' to {target_lang}: Failed - {str(e)}")
|
| 49 |
+
|
| 50 |
+
return "\n".join(results)
|
| 51 |
+
|
| 52 |
+
# Create Gradio interface
|
| 53 |
+
with gr.Blocks() as iface:
|
| 54 |
+
gr.Markdown("# Speech Translation using T5-base")
|
| 55 |
+
gr.Markdown("Translate English text to French, German, or Romanian using the T5-base model.")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
input_text = gr.Textbox(label="Enter English text", placeholder="Hi, how are you?")
|
| 59 |
+
output_text = gr.Textbox(label="Translation")
|
| 60 |
+
|
| 61 |
+
target_lang = gr.Dropdown(AVAILABLE_LANGUAGES, label="Target Language")
|
| 62 |
+
translate_btn = gr.Button("Translate")
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
show_langs_btn = gr.Button("Show Available Languages")
|
| 66 |
+
available_langs = gr.Textbox(label="Available Languages")
|
| 67 |
+
|
| 68 |
+
test_btn = gr.Button("Run Tests")
|
| 69 |
+
test_output = gr.Textbox(label="Test Results")
|
| 70 |
+
|
| 71 |
+
translate_btn.click(translate, inputs=[input_text, target_lang], outputs=output_text)
|
| 72 |
+
show_langs_btn.click(show_available_languages, inputs=None, outputs=available_langs)
|
| 73 |
+
test_btn.click(run_tests, inputs=None, outputs=test_output)
|
| 74 |
+
|
| 75 |
+
# Launch the app
|
| 76 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==3.50.2
|
| 2 |
+
transformers==4.36.2
|
| 3 |
+
torch==2.1.2
|
| 4 |
+
sentencepiece==0.1.99
|
test-file.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pytest
|
| 2 |
+
from app import translate
|
| 3 |
+
|
| 4 |
+
def test_translation_to_french():
|
| 5 |
+
print("Starting French translation test")
|
| 6 |
+
result = translate("Hello", "French")
|
| 7 |
+
print(f"French translation result: {result}")
|
| 8 |
+
assert result.lower() in ["bonjour", "salut"], f"Expected 'Bonjour' or 'Salut', but got {result}"
|
| 9 |
+
|
| 10 |
+
def test_translation_to_german():
|
| 11 |
+
print("Starting German translation test")
|
| 12 |
+
result = translate("Hello", "German")
|
| 13 |
+
print(f"German translation result: {result}")
|
| 14 |
+
assert result.lower() == "hallo", f"Expected 'Hallo', but got {result}"
|
| 15 |
+
|
| 16 |
+
def test_translation_to_romanian():
|
| 17 |
+
print("Starting Romanian translation test")
|
| 18 |
+
result = translate("Hello", "Romanian")
|
| 19 |
+
print(f"Romanian translation result: {result}")
|
| 20 |
+
assert result.lower() == "salut", f"Expected 'Salut', but got {result}"
|
| 21 |
+
|
| 22 |
+
def test_unsupported_language():
|
| 23 |
+
print("Starting unsupported language test")
|
| 24 |
+
result = translate("Hello", "Spanish")
|
| 25 |
+
print(f"Unsupported language result: {result}")
|
| 26 |
+
assert result == "Sorry, this language is not supported."
|