Spaces:
Sleeping
Sleeping
[FIX] => adding more debugging
Browse files
app.py
CHANGED
|
@@ -1,47 +1,73 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
print("done 1")
|
| 4 |
|
| 5 |
# Load models
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Define translation functions
|
| 14 |
def translate_base(text):
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
def translate_fine_tuned(text, model):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Create Gradio interface
|
| 28 |
with gr.Blocks() as demo:
|
| 29 |
gr.Markdown("### Translation Models")
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import pipeline
|
| 4 |
+
|
| 5 |
print("done 1")
|
| 6 |
|
| 7 |
# Load models
|
| 8 |
+
try:
|
| 9 |
+
base_model = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-base-en-sh")
|
| 10 |
+
print("Base model loaded successfully.")
|
| 11 |
+
except Exception as e:
|
| 12 |
+
print(f"Error loading base model: {e}")
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
fine_tuned_model_1 = pipeline("translation", model="perkan/shortS-opus-mt-tc-base-en-sr")
|
| 16 |
+
print("Fine-tuned model 1 loaded successfully.")
|
| 17 |
+
except Exception as e:
|
| 18 |
+
print(f"Error loading fine-tuned model 1: {e}")
|
| 19 |
+
|
| 20 |
+
try:
|
| 21 |
+
fine_tuned_model_2 = pipeline("translation", model="perkan/shortM-opus-mt-tc-base-en-sr")
|
| 22 |
+
print("Fine-tuned model 2 loaded successfully.")
|
| 23 |
+
except Exception as e:
|
| 24 |
+
print(f"Error loading fine-tuned model 2: {e}")
|
| 25 |
+
|
| 26 |
+
try:
|
| 27 |
+
fine_tuned_model_3 = pipeline("translation", model="perkan/shortL-opus-mt-tc-base-en-sr")
|
| 28 |
+
print("Fine-tuned model 3 loaded successfully.")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
print(f"Error loading fine-tuned model 3: {e}")
|
| 31 |
|
| 32 |
# Define translation functions
|
| 33 |
def translate_base(text):
|
| 34 |
+
try:
|
| 35 |
+
return base_model(text)[0]['translation_text']
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error during translation: {e}"
|
| 38 |
|
| 39 |
def translate_fine_tuned(text, model):
|
| 40 |
+
try:
|
| 41 |
+
if model == 'model1':
|
| 42 |
+
return fine_tuned_model_1(text)[0]['translation_text']
|
| 43 |
+
elif model == 'model2':
|
| 44 |
+
return fine_tuned_model_2(text)[0]['translation_text']
|
| 45 |
+
elif model == 'model3':
|
| 46 |
+
return fine_tuned_model_3(text)[0]['translation_text']
|
| 47 |
+
else:
|
| 48 |
+
return "Invalid model selected"
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error during translation: {e}"
|
| 51 |
|
| 52 |
# Create Gradio interface
|
| 53 |
with gr.Blocks() as demo:
|
| 54 |
gr.Markdown("### Translation Models")
|
| 55 |
|
| 56 |
+
with gr.Row():
|
| 57 |
+
with gr.Column():
|
| 58 |
+
gr.Markdown("#### Base Model")
|
| 59 |
+
base_input = gr.Textbox(placeholder="Enter text to translate", label="Input")
|
| 60 |
+
base_output = gr.Textbox(label="Translation")
|
| 61 |
+
base_translate_btn = gr.Button("Translate")
|
| 62 |
+
base_translate_btn.click(translate_base, inputs=base_input, outputs=base_output)
|
| 63 |
+
|
| 64 |
+
with gr.Column():
|
| 65 |
+
gr.Markdown("#### Fine-tuned Models")
|
| 66 |
+
fine_tuned_input = gr.Textbox(placeholder="Enter text to translate", label="Input")
|
| 67 |
+
model_select = gr.Dropdown(choices=["model1", "model2", "model3"], label="Select Model")
|
| 68 |
+
fine_tuned_output = gr.Textbox(label="Translation")
|
| 69 |
+
fine_tuned_translate_btn = gr.Button("Translate")
|
| 70 |
+
fine_tuned_translate_btn.click(translate_fine_tuned, inputs=[fine_tuned_input, model_select], outputs=fine_tuned_output)
|
| 71 |
+
|
| 72 |
+
port = int(os.getenv("GRADIO_SERVER_PORT", "7861"))
|
| 73 |
+
demo.launch(server_port=port)
|