perkan commited on
Commit
ce5be86
1 Parent(s): effe6d7

[FIX] => adding more debugging

Browse files
Files changed (1) hide show
  1. app.py +58 -32
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
- base_model = pipe = pipeline("translation", model="Helsinki-NLP/opus-mt-tc-base-en-sh")
7
- print("done 2")
8
- fine_tuned_model_1 = pipeline("translation", model="perkan/shortS-opus-mt-tc-base-en-sr")
9
- print("done 3")
10
- fine_tuned_model_2 = pipeline("translation", model="perkan/shortM-opus-mt-tc-base-en-sr")
11
- fine_tuned_model_3 = pipeline("translation", model="perkan/shortL-opus-mt-tc-base-en-sr")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  # Define translation functions
14
  def translate_base(text):
15
- return base_model(text)[0]['translation_text']
 
 
 
16
 
17
  def translate_fine_tuned(text, model):
18
- if model == 'model1':
19
- return fine_tuned_model_1(text)[0]['translation_text']
20
- elif model == 'model2':
21
- return fine_tuned_model_2(text)[0]['translation_text']
22
- elif model == 'model3':
23
- return fine_tuned_model_3(text)[0]['translation_text']
24
- else:
25
- return "Invalid model selected"
 
 
 
26
 
27
  # Create Gradio interface
28
  with gr.Blocks() as demo:
29
  gr.Markdown("### Translation Models")
30
 
31
- # with gr.Row():
32
- # with gr.Column():
33
- # gr.Markdown("#### Base Model")
34
- # base_input = gr.Textbox(placeholder="Enter text to translate", label="Input")
35
- # base_output = gr.Textbox(label="Translation")
36
- # base_translate_btn = gr.Button("Translate")
37
- # base_translate_btn.click(translate_base, inputs=base_input, outputs=base_output)
38
-
39
- # with gr.Column():
40
- # gr.Markdown("#### Fine-tuned Models")
41
- # fine_tuned_input = gr.Textbox(placeholder="Enter text to translate", label="Input")
42
- # model_select = gr.Dropdown(choices=["model1", "model2", "model3"], label="Select Model")
43
- # fine_tuned_output = gr.Textbox(label="Translation")
44
- # fine_tuned_translate_btn = gr.Button("Translate")
45
- # fine_tuned_translate_btn.click(translate_fine_tuned, inputs=[fine_tuned_input, model_select], outputs=fine_tuned_output)
46
-
47
- demo.launch()
 
 
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)