Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,46 +3,73 @@ import requests
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
pipe = pipeline("translation", "guymorlan/TokenizerLabeller")
|
| 7 |
|
| 8 |
-
# download json and open
|
| 9 |
r = requests.get("https://huggingface.co/guymorlan/TokenizerLabeller/raw/main/playaling_words.json")
|
| 10 |
data = json.loads(r.text)
|
| 11 |
|
| 12 |
-
# build gradio interface
|
| 13 |
def predict(input):
|
| 14 |
-
|
| 15 |
out = pipe(input)[0]['translation_text']
|
| 16 |
raw = out
|
| 17 |
out = [x.strip() for x in out.split(" + ")]
|
| 18 |
|
| 19 |
output = f"""
|
| 20 |
-
|
| 21 |
|
| 22 |
for o in out:
|
| 23 |
oo = [x.strip() for x in o.split("+")]
|
| 24 |
-
|
| 25 |
for ooo in oo:
|
| 26 |
if ooo in data:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
else:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
output += "+".join(newout) + " | "
|
| 35 |
-
|
| 36 |
output += "</div>"
|
| 37 |
|
| 38 |
output += """
|
| 39 |
-
<
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
</
|
| 44 |
"""
|
| 45 |
-
|
| 46 |
return output
|
| 47 |
|
| 48 |
-
gr.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
js = """
|
| 7 |
+
async () => {
|
| 8 |
+
function showCard(event, title, content) {
|
| 9 |
+
document.getElementById('hovercard').style.visibility = 'visible';
|
| 10 |
+
document.getElementById('card_title').innerText = title;
|
| 11 |
+
document.getElementById('card_content').innerText = content;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
function hideCard(event) {
|
| 15 |
+
document.getElementById('hovercard').style.visibility = 'hidden';
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
globalThis.showCard = showCard;
|
| 19 |
+
globalThis.hideCard = hideCard;
|
| 20 |
+
}
|
| 21 |
+
"""
|
| 22 |
+
|
| 23 |
pipe = pipeline("translation", "guymorlan/TokenizerLabeller")
|
| 24 |
|
|
|
|
| 25 |
r = requests.get("https://huggingface.co/guymorlan/TokenizerLabeller/raw/main/playaling_words.json")
|
| 26 |
data = json.loads(r.text)
|
| 27 |
|
|
|
|
| 28 |
def predict(input):
|
|
|
|
| 29 |
out = pipe(input)[0]['translation_text']
|
| 30 |
raw = out
|
| 31 |
out = [x.strip() for x in out.split(" + ")]
|
| 32 |
|
| 33 |
output = f"""
|
| 34 |
+
<div style='direction: rtl; text-align: right; font-size: 18px; font-family: Arial, sans-serif; line-height: 1.5'>{raw}<br><br>"""
|
| 35 |
|
| 36 |
for o in out:
|
| 37 |
oo = [x.strip() for x in o.split("+")]
|
| 38 |
+
output += "<span style='background-color: #E0E0E0; border-radius: 5px; padding: 5px; margin-right: 5px; display: inline-block;'>"
|
| 39 |
for ooo in oo:
|
| 40 |
if ooo in data:
|
| 41 |
+
output += f"""
|
| 42 |
+
<span style='background-color: #4CAF50; color: #FFFFFF; border: 1px solid #4CAF50; border-radius: 5px; padding: 2px; margin-right: 2px; font-family: "Courier New", Courier, monospace;'
|
| 43 |
+
onmouseover='showCard(event, "{data[ooo]['translation']}", "{data[ooo]['features']}")'
|
| 44 |
+
onmouseout='hideCard(event)' onclick='showCard(event, "{data[ooo]['translation']}", "{data[ooo]['features']}")'>{data[ooo]['word']}</span>
|
| 45 |
+
"""
|
| 46 |
else:
|
| 47 |
+
output += ooo
|
| 48 |
+
output += "</span> "
|
|
|
|
|
|
|
| 49 |
output += "</div>"
|
| 50 |
|
| 51 |
output += """
|
| 52 |
+
<div id='hovercard' style='position: absolute; visibility: hidden; background: #FFFFFF; padding: 10px;
|
| 53 |
+
border: 1px solid #9E9E9E; border-radius: 5px; font-family: Arial, sans-serif;'>
|
| 54 |
+
<h3 id='card_title' style='color: #000000;'></h3>
|
| 55 |
+
<p id='card_content' style='color: #000000;'></p>
|
| 56 |
+
</div>
|
| 57 |
"""
|
|
|
|
| 58 |
return output
|
| 59 |
|
| 60 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="Ammiya Tokenizer and Labeler") as demo:
|
| 61 |
+
gr.HTML("<h2><span style='color: #2563eb'>Colloquial Arabic</span></h2> Tokenizer and Annotator")
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
input = gr.Textbox(label="Input", placeholder="Enter English Text", lines=1)
|
| 65 |
+
gr.Examples(["بديش اروح معك", "مكنتش هون قبل ما جيت"], input)
|
| 66 |
+
btn = gr.Button(label="Analyze")
|
| 67 |
+
with gr.Column():
|
| 68 |
+
with gr.Box():
|
| 69 |
+
html = gr.HTML()
|
| 70 |
+
btn.click(predict, inputs=[input], outputs=[html])
|
| 71 |
+
input.submit(predict, inputs = [input], outputs=[html])
|
| 72 |
+
|
| 73 |
+
demo.load(_js=js)
|
| 74 |
+
demo.launch()
|
| 75 |
+
|