Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
def read_lexicon(lexicon_file):
|
| 6 |
df = pd.read_csv(lexicon_file, sep='\t')
|
| 7 |
df['keyword_no_cat'] = df['lemma'].str.split(' #').str[0].str.strip().str.replace(' ', '_')
|
|
@@ -9,7 +10,8 @@ def read_lexicon(lexicon_file):
|
|
| 9 |
|
| 10 |
lexicon = read_lexicon("lexicon.csv")
|
| 11 |
|
| 12 |
-
|
|
|
|
| 13 |
def get_id_picto_from_predicted_lemma(df_lexicon, lemma):
|
| 14 |
lemma = lemma.strip().lower()
|
| 15 |
if lemma.endswith("!"):
|
|
@@ -17,7 +19,8 @@ def get_id_picto_from_predicted_lemma(df_lexicon, lemma):
|
|
| 17 |
id_picto = df_lexicon.loc[df_lexicon['keyword_no_cat'] == lemma, 'id_picto'].tolist()
|
| 18 |
return (id_picto[0], lemma) if id_picto else (0, lemma)
|
| 19 |
|
| 20 |
-
|
|
|
|
| 21 |
def generate_html(ids):
|
| 22 |
html_content = '''
|
| 23 |
<style>
|
|
@@ -44,40 +47,40 @@ def generate_html(ids):
|
|
| 44 |
</style>
|
| 45 |
'''
|
| 46 |
for picto_id, lemma in ids:
|
| 47 |
-
if picto_id != 0: #
|
| 48 |
img_url = f"https://static.arasaac.org/pictograms/{picto_id}/{picto_id}_500.png"
|
| 49 |
html_content += f'''
|
| 50 |
<figure>
|
| 51 |
-
<img src="{img_url}" alt="{lemma}" width="
|
| 52 |
<figcaption>{lemma}</figcaption>
|
| 53 |
</figure>
|
| 54 |
'''
|
| 55 |
-
else: #
|
| 56 |
html_content += f'''
|
| 57 |
<figure>
|
| 58 |
-
<figcaption>Token "{lemma}"
|
| 59 |
</figure>
|
| 60 |
'''
|
| 61 |
return html_content
|
| 62 |
|
| 63 |
-
|
|
|
|
| 64 |
def process_text(input_text):
|
| 65 |
-
tokens = input_text.strip().split() #
|
| 66 |
pictogram_ids = [get_id_picto_from_predicted_lemma(lexicon, token) for token in tokens]
|
| 67 |
return generate_html(pictogram_ids)
|
| 68 |
|
|
|
|
| 69 |
# Configuration de l'interface Gradio
|
| 70 |
with gr.Blocks() as demo:
|
| 71 |
gr.Markdown("## Visualize Pictograms Application")
|
| 72 |
-
gr.Markdown("
|
| 73 |
|
| 74 |
-
# Zone de texte et résultats dans une colonne
|
| 75 |
with gr.Column():
|
| 76 |
-
input_text = gr.Textbox(label="
|
| 77 |
-
output_html = gr.HTML(label="
|
| 78 |
|
| 79 |
-
|
| 80 |
-
submit_btn = gr.Button("Afficher les pictogrammes")
|
| 81 |
submit_btn.click(process_text, inputs=input_text, outputs=output_html)
|
| 82 |
|
| 83 |
# Lancer l'application
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
|
| 4 |
+
|
| 5 |
+
# Read the lexicon
|
| 6 |
def read_lexicon(lexicon_file):
|
| 7 |
df = pd.read_csv(lexicon_file, sep='\t')
|
| 8 |
df['keyword_no_cat'] = df['lemma'].str.split(' #').str[0].str.strip().str.replace(' ', '_')
|
|
|
|
| 10 |
|
| 11 |
lexicon = read_lexicon("lexicon.csv")
|
| 12 |
|
| 13 |
+
|
| 14 |
+
# Get the ARASAAC pictogram ID from the lexicon
|
| 15 |
def get_id_picto_from_predicted_lemma(df_lexicon, lemma):
|
| 16 |
lemma = lemma.strip().lower()
|
| 17 |
if lemma.endswith("!"):
|
|
|
|
| 19 |
id_picto = df_lexicon.loc[df_lexicon['keyword_no_cat'] == lemma, 'id_picto'].tolist()
|
| 20 |
return (id_picto[0], lemma) if id_picto else (0, lemma)
|
| 21 |
|
| 22 |
+
|
| 23 |
+
# HTML content to show the pictogram images
|
| 24 |
def generate_html(ids):
|
| 25 |
html_content = '''
|
| 26 |
<style>
|
|
|
|
| 47 |
</style>
|
| 48 |
'''
|
| 49 |
for picto_id, lemma in ids:
|
| 50 |
+
if picto_id != 0: # pictogram id exists
|
| 51 |
img_url = f"https://static.arasaac.org/pictograms/{picto_id}/{picto_id}_500.png"
|
| 52 |
html_content += f'''
|
| 53 |
<figure>
|
| 54 |
+
<img src="{img_url}" alt="{lemma}" width="150" height="150"/>
|
| 55 |
<figcaption>{lemma}</figcaption>
|
| 56 |
</figure>
|
| 57 |
'''
|
| 58 |
+
else: # not found pictogram
|
| 59 |
html_content += f'''
|
| 60 |
<figure>
|
| 61 |
+
<figcaption>Token "{lemma}" not in the lexicon</figcaption>
|
| 62 |
</figure>
|
| 63 |
'''
|
| 64 |
return html_content
|
| 65 |
|
| 66 |
+
|
| 67 |
+
# Process the input of the user
|
| 68 |
def process_text(input_text):
|
| 69 |
+
tokens = input_text.strip().split() # cut the sequence into tokens
|
| 70 |
pictogram_ids = [get_id_picto_from_predicted_lemma(lexicon, token) for token in tokens]
|
| 71 |
return generate_html(pictogram_ids)
|
| 72 |
|
| 73 |
+
|
| 74 |
# Configuration de l'interface Gradio
|
| 75 |
with gr.Blocks() as demo:
|
| 76 |
gr.Markdown("## Visualize Pictograms Application")
|
| 77 |
+
gr.Markdown("Enter a sequence of pictogram tokens")
|
| 78 |
|
|
|
|
| 79 |
with gr.Column():
|
| 80 |
+
input_text = gr.Textbox(label="", placeholder="Example : bonjour je appeler plan_taille")
|
| 81 |
+
output_html = gr.HTML(label="ARASAAC Pictograms")
|
| 82 |
|
| 83 |
+
submit_btn = gr.Button("Generate")
|
|
|
|
| 84 |
submit_btn.click(process_text, inputs=input_text, outputs=output_html)
|
| 85 |
|
| 86 |
# Lancer l'application
|