Spaces:
Runtime error
Runtime error
use Gradio Blocks for this prototype
Browse files- .vscode/settings.json +5 -0
- app.py +72 -38
.vscode/settings.json
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"python.pythonPath": "/Users/sheonhan/.pyenv/shims/python",
|
3 |
+
"python.analysis.extraPaths": ["~/hugging-face/language-demo"]
|
4 |
+
|
5 |
+
}
|
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import requests
|
2 |
import os
|
3 |
|
4 |
import fasttext
|
@@ -33,48 +32,83 @@ def identify_language(text):
|
|
33 |
model = fasttext.load_model(model_full_path)
|
34 |
predictions = model.predict(text, k=1) # e.g., (('__label__eng_Latn',), array([0.81148803]))
|
35 |
|
36 |
-
|
37 |
-
language_code = predictions[0][0][
|
|
|
38 |
return language_code
|
39 |
|
40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
def translate(text, src_lang, tgt_lang):
|
|
|
|
|
|
|
|
|
|
|
42 |
translation_pipeline = pipeline(
|
43 |
-
"translation", model=model, tokenizer=tokenizer, src_lang=
|
44 |
result = translation_pipeline(text)
|
45 |
return result[0]['translation_text']
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
["
|
64 |
-
|
65 |
-
|
66 |
-
gr.
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
|
3 |
import fasttext
|
|
|
32 |
model = fasttext.load_model(model_full_path)
|
33 |
predictions = model.predict(text, k=1) # e.g., (('__label__eng_Latn',), array([0.81148803]))
|
34 |
|
35 |
+
CHAR_TO_STRIP = 9 # To strip away '__label__' from language code
|
36 |
+
language_code = predictions[0][0][CHAR_TO_STRIP:]
|
37 |
+
|
38 |
return language_code
|
39 |
|
40 |
|
41 |
+
def display(user_lang, text):
|
42 |
+
user_lang_code = language_code_map[user_lang]
|
43 |
+
language_code = identify_language(text)
|
44 |
+
|
45 |
+
translate_button_visibility = language_code != user_lang_code
|
46 |
+
|
47 |
+
detected_language_text = f"""
|
48 |
+
Detected Language: {language_code}\n
|
49 |
+
User Content Language: {user_lang_code}\n
|
50 |
+
{"" if translate_button_visibility else "[NOT TRANSLATABLE] Detected Language and Content Language are the same"}
|
51 |
+
"""
|
52 |
+
|
53 |
+
return text, gr.update(value="", placeholder="Leave a comment"), gr.update(value=detected_language_text), gr.update(visible=translate_button_visibility, variant="primary")
|
54 |
+
|
55 |
+
|
56 |
def translate(text, src_lang, tgt_lang):
|
57 |
+
CHAR_TO_STRIP = 22 # To strip away 'Detected Language: ' from language code
|
58 |
+
LANGUAGE_CODE_LENGTH = 8 # To strip away 'Detected Language: ' from language code
|
59 |
+
src_lang_code = src_lang[CHAR_TO_STRIP:CHAR_TO_STRIP + LANGUAGE_CODE_LENGTH]
|
60 |
+
tgt_lang_code = language_code_map[tgt_lang]
|
61 |
+
|
62 |
translation_pipeline = pipeline(
|
63 |
+
"translation", model=model, tokenizer=tokenizer, src_lang=src_lang_code, tgt_lang=tgt_lang_code, device=device)
|
64 |
result = translation_pipeline(text)
|
65 |
return result[0]['translation_text']
|
66 |
|
67 |
+
with gr.Blocks() as demo:
|
68 |
+
gr.HTML(
|
69 |
+
f"""
|
70 |
+
<div style="text-align: center; margin: 0 auto;">
|
71 |
+
<div style=" display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem;">
|
72 |
+
<h1 style="font-weight: 900; margin-bottom: 7px;margin-top:5px">
|
73 |
+
{title}
|
74 |
+
</h1>
|
75 |
+
</div>
|
76 |
+
<p style="margin-bottom: 10px; font-size: 94%; line-height: 23px;">
|
77 |
+
{description}
|
78 |
+
</p>
|
79 |
+
</div>
|
80 |
+
"""
|
81 |
+
)
|
82 |
+
|
83 |
+
user_langugage_radio = gr.Radio(["English", "Spanish", "Korean", "French", "German", "Japanese"],
|
84 |
+
value="English", label="User Content Language")
|
85 |
+
|
86 |
+
comment_input_textbox = gr.Textbox(
|
87 |
+
lines=3, label="Write a Comment", placeholder="Leave a comment")
|
88 |
+
comment_out_textbox = gr.Textbox(lines=3, label="Comment")
|
89 |
+
detected_lang_markdown = gr.Markdown("", elem_id="detect-lang-md")
|
90 |
+
|
91 |
+
comment_btn = gr.Button("Comment")
|
92 |
+
|
93 |
+
translate_btn = gr.Button("Translate", visible=False)
|
94 |
+
detected_language_value = gr.Textbox("", visible=False)
|
95 |
+
|
96 |
+
|
97 |
+
comment_btn.click(display,
|
98 |
+
inputs=[user_langugage_radio, comment_input_textbox],
|
99 |
+
outputs=[
|
100 |
+
comment_out_textbox,
|
101 |
+
comment_input_textbox,
|
102 |
+
detected_lang_markdown,
|
103 |
+
translate_btn
|
104 |
+
])
|
105 |
+
|
106 |
+
translate_btn.click(translate,
|
107 |
+
inputs=[
|
108 |
+
comment_out_textbox,
|
109 |
+
detected_lang_markdown,
|
110 |
+
user_langugage_radio
|
111 |
+
],
|
112 |
+
outputs=comment_out_textbox)
|
113 |
+
|
114 |
+
demo.launch()
|