davda54 commited on
Commit
6ed95ae
1 Parent(s): 95270f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -11
app.py CHANGED
@@ -31,15 +31,28 @@ PENALTY = 1.2
31
  TOP_K = 64
32
  TOP_P = 0.95
33
 
 
 
 
 
 
 
 
 
 
 
34
 
35
  def translate(source, source_language, target_language):
36
  return "This is a fake translation"
37
 
38
 
 
 
 
 
39
  import gradio as gr
40
 
41
- #with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
42
- with gr.Blocks() as demo:
43
 
44
  gr.Markdown("# Norwegian-English translation")
45
  # gr.HTML('<img src="https://huggingface.co/ltg/norbert3-base/resolve/main/norbert.png" width=6.75%>')
@@ -48,18 +61,21 @@ with gr.Blocks() as demo:
48
  # chatbot = gr.Chatbot(value=[[None, "Hei, hva kan jeg gjøre for deg? 😊"]])
49
 
50
  with gr.Row():
51
- with gr.Column(scale=1, variant="panel"):
52
  source_language = gr.Dropdown(
53
- ["English", "Norwegian (Bokmål)", "Norwegian (Nynorsk)"], value="English", show_label=False
54
  )
55
  source = gr.Textbox(
56
- label="Source text", placeholder="What do you want to translate?", show_label=False, lines=7, max_lines=100
57
  ) # .style(container=False)
58
  submit = gr.Button("Submit", variant="primary") # .style(full_width=True)
59
 
60
- with gr.Column(scale=1, variant="panel"):
 
 
 
61
  target_language = gr.Dropdown(
62
- ["English", "Norwegian (Bokmål)", "Norwegian (Nynorsk)"], value="Norwegian (Bokmål)", show_label=False
63
  )
64
  target = gr.Textbox(
65
  label="Translation", show_label=False, interactive=False, lines=7, max_lines=100
@@ -84,19 +100,35 @@ with gr.Blocks() as demo:
84
 
85
 
86
  submit_event = source.submit(
87
- fn=update_state_after_user, inputs=None, outputs=[source, target, source_language, target_language], queue=False
 
 
88
  ).then(
89
  fn=translate, inputs=[source, source_language, target_language], outputs=[target], queue=True
90
  ).then(
91
- fn=update_state_after_return, inputs=None, outputs=[source, target, source_language, target_language], queue=False
92
  )
93
 
94
  submit_click_event = submit.click(
95
- fn=update_state_after_user, inputs=None, outputs=[source, target, source_language, target_language], queue=False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  ).then(
97
  fn=translate, inputs=[source, source_language, target_language], outputs=[target], queue=True
98
  ).then(
99
- fn=update_state_after_return, inputs=None, outputs=[source, target, source_language, target_language], queue=False
100
  )
101
 
102
  demo.queue(max_size=32, concurrency_count=2)
 
31
  TOP_K = 64
32
  TOP_P = 0.95
33
 
34
+ LANGUAGES = [
35
+ "🇬🇧 English",
36
+ "🇳🇴 Norwegian (Bokmål)",
37
+ "🇳🇴 Norwegian (Nynorsk)"
38
+ ]
39
+
40
+
41
+ def set_default_target():
42
+ return "*Translating...*"
43
+
44
 
45
  def translate(source, source_language, target_language):
46
  return "This is a fake translation"
47
 
48
 
49
+ def switch_inputs(source, target, source_language, target_language):
50
+ return target, source, target_language, source_language
51
+
52
+
53
  import gradio as gr
54
 
55
+ with gr.Blocks(theme='sudeepshouche/minimalist') as demo:
 
56
 
57
  gr.Markdown("# Norwegian-English translation")
58
  # gr.HTML('<img src="https://huggingface.co/ltg/norbert3-base/resolve/main/norbert.png" width=6.75%>')
 
61
  # chatbot = gr.Chatbot(value=[[None, "Hei, hva kan jeg gjøre for deg? 😊"]])
62
 
63
  with gr.Row():
64
+ with gr.Column(scale=7, variant="panel"):
65
  source_language = gr.Dropdown(
66
+ LANGUAGES, value=LANGUAGES[0], show_label=False
67
  )
68
  source = gr.Textbox(
69
+ label="Source text", placeholder="What do you want to translate?", show_label=False, lines=7, max_lines=100, autofocus=True
70
  ) # .style(container=False)
71
  submit = gr.Button("Submit", variant="primary") # .style(full_width=True)
72
 
73
+ with gr.Column(scale=1, variant=None):
74
+ switch = gr.Button("🔄")
75
+
76
+ with gr.Column(scale=7, variant="panel"):
77
  target_language = gr.Dropdown(
78
+ LANGUAGES, value=LANGUAGES[1], show_label=False
79
  )
80
  target = gr.Textbox(
81
  label="Translation", show_label=False, interactive=False, lines=7, max_lines=100
 
100
 
101
 
102
  submit_event = source.submit(
103
+ fn=update_state_after_user, inputs=None, outputs=[source, submit, source_language, target_language], queue=False
104
+ ).then(
105
+ fn=set_default_target, inputs=[], outputs=[target], queue=False
106
  ).then(
107
  fn=translate, inputs=[source, source_language, target_language], outputs=[target], queue=True
108
  ).then(
109
+ fn=update_state_after_return, inputs=None, outputs=[source, submit, source_language, target_language], queue=False
110
  )
111
 
112
  submit_click_event = submit.click(
113
+ fn=update_state_after_user, inputs=None, outputs=[source, submit, source_language, target_language], queue=False
114
+ ).then(
115
+ fn=set_default_target, inputs=[], outputs=[target], queue=False
116
+ ).then(
117
+ fn=translate, inputs=[source, source_language, target_language], outputs=[target], queue=True
118
+ ).then(
119
+ fn=update_state_after_return, inputs=None, outputs=[source, submit, source_language, target_language], queue=False
120
+ )
121
+
122
+ switch_event = switch.click(
123
+ fn=switch_inputs, inputs=[source, target, source_language, target_language], outputs=[target, source, target_language, source_language], queue=False
124
+ ).then(
125
+ fn=update_state_after_user, inputs=None, outputs=[source, submit, source_language, target_language], queue=False
126
+ ).then(
127
+ fn=set_default_target, inputs=[], outputs=[target], queue=False
128
  ).then(
129
  fn=translate, inputs=[source, source_language, target_language], outputs=[target], queue=True
130
  ).then(
131
+ fn=update_state_after_return, inputs=None, outputs=[source, submit, source_language, target_language], queue=False
132
  )
133
 
134
  demo.queue(max_size=32, concurrency_count=2)