Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,2 +1,82 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
|
| 4 |
+
# Lecture du lexique
|
| 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(' ', '_')
|
| 8 |
+
return df
|
| 9 |
+
|
| 10 |
+
lexicon = read_lexicon("lexicon.csv")
|
| 11 |
+
|
| 12 |
+
# Récupération de l'ID du pictogramme depuis le lexique
|
| 13 |
+
def get_id_picto_from_predicted_lemma(df_lexicon, lemma):
|
| 14 |
+
lemma = lemma.strip().lower()
|
| 15 |
+
if lemma.endswith("!"):
|
| 16 |
+
lemma = lemma[:-1]
|
| 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 |
+
# Génération de contenu HTML pour afficher les pictogrammes
|
| 21 |
+
def generate_html(ids):
|
| 22 |
+
html_content = '''
|
| 23 |
+
<style>
|
| 24 |
+
figure {
|
| 25 |
+
display: inline-block;
|
| 26 |
+
text-align: center;
|
| 27 |
+
font-family: Arial, sans-serif;
|
| 28 |
+
margin: 10px;
|
| 29 |
+
}
|
| 30 |
+
figcaption {
|
| 31 |
+
color: black;
|
| 32 |
+
background-color: white;
|
| 33 |
+
border-radius: 5px;
|
| 34 |
+
margin-top: 5px;
|
| 35 |
+
padding: 2px;
|
| 36 |
+
}
|
| 37 |
+
img {
|
| 38 |
+
background-color: white;
|
| 39 |
+
margin: 0;
|
| 40 |
+
padding: 0;
|
| 41 |
+
border-radius: 6px;
|
| 42 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.2);
|
| 43 |
+
}
|
| 44 |
+
</style>
|
| 45 |
+
'''
|
| 46 |
+
for picto_id, lemma in ids:
|
| 47 |
+
if picto_id != 0: # pictogramme trouvé
|
| 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="100" height="100"/>
|
| 52 |
+
<figcaption>{lemma}</figcaption>
|
| 53 |
+
</figure>
|
| 54 |
+
'''
|
| 55 |
+
else: # pictogramme non trouvé
|
| 56 |
+
html_content += f'''
|
| 57 |
+
<figure>
|
| 58 |
+
<figcaption>Token "{lemma}" non trouvé dans le lexique</figcaption>
|
| 59 |
+
</figure>
|
| 60 |
+
'''
|
| 61 |
+
return html_content
|
| 62 |
+
|
| 63 |
+
# Fonction principale de traitement
|
| 64 |
+
def process_text(input_text):
|
| 65 |
+
tokens = input_text.strip().split() # Découper l'entrée utilisateur en tokens
|
| 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("Saisissez une phrase pour voir les pictogrammes correspondants.")
|
| 73 |
+
|
| 74 |
+
with gr.Row():
|
| 75 |
+
input_text = gr.Textbox(label="Entrez votre texte", placeholder="Exemple : bonjour comment ça va")
|
| 76 |
+
output_html = gr.HTML(label="Résultats")
|
| 77 |
+
|
| 78 |
+
submit_btn = gr.Button("Afficher les pictogrammes")
|
| 79 |
+
submit_btn.click(process_text, inputs=input_text, outputs=output_html)
|
| 80 |
+
|
| 81 |
+
# Lancer l'application
|
| 82 |
+
demo.launch()
|