|
|
import gradio as gr |
|
|
import json |
|
|
import zipfile |
|
|
import pandas as pd |
|
|
from sentence_transformers import SentenceTransformer |
|
|
|
|
|
from scripts.aulsign import AulSign |
|
|
from scripts.scripts.sign2text_mapping import sign2text |
|
|
|
|
|
|
|
|
def load_resources(): |
|
|
zip_file_path = 'tools/corpus_embeddings.json.zip' |
|
|
output_folder = 'tools/' |
|
|
with zipfile.ZipFile(zip_file_path, 'r') as zip_ref: |
|
|
zip_ref.extractall(output_folder) |
|
|
|
|
|
print("File estratto con successo.") |
|
|
|
|
|
|
|
|
model_name = "mixedbread-ai/mxbai-embed-large-v1" |
|
|
model = SentenceTransformer(model_name) |
|
|
|
|
|
|
|
|
corpus_embeddings_path = 'tools/corpus_embeddings.json' |
|
|
sentences_train_embeddings_path = 'tools/sentences_train_embeddings_filtered_01.json' |
|
|
rules_prompt_path_text2sign = 'tools/rules_prompt_text2sign.txt' |
|
|
rules_prompt_path_sign2text = 'tools/rules_prompt_sign2text.txt' |
|
|
|
|
|
|
|
|
with open(corpus_embeddings_path, 'r') as file: |
|
|
corpus_embeddings = pd.DataFrame(json.load(file)) |
|
|
|
|
|
with open(sentences_train_embeddings_path, 'r') as file: |
|
|
sentences_train_embeddings = pd.DataFrame(json.load(file)) |
|
|
|
|
|
return model, corpus_embeddings_path, corpus_embeddings, sentences_train_embeddings, rules_prompt_path_text2sign, rules_prompt_path_sign2text |
|
|
|
|
|
|
|
|
def text_to_sign(sentence_to_analyse): |
|
|
try: |
|
|
pseudo_can, fsw_seq, can_desc_association_seq, _ = AulSign( |
|
|
input=sentence_to_analyse, |
|
|
rules_prompt_path=rules_prompt_path_text2sign, |
|
|
train_sentences=sentences_train_embeddings, |
|
|
vocabulary=corpus_embeddings, |
|
|
model=model, |
|
|
ollama=False, |
|
|
modality="text2sign" |
|
|
) |
|
|
return fsw_seq,can_desc_association_seq |
|
|
except Exception as e: |
|
|
return f"Error: {str(e)}" |
|
|
|
|
|
def sign_to_text(fsw_to_analyse): |
|
|
try: |
|
|
mapped_input = sign2text(fsw_to_analyse,corpus_embeddings_path) |
|
|
print(mapped_input) |
|
|
except Exception as e: |
|
|
return f"Error on mapping : {str(e)}" |
|
|
|
|
|
try: |
|
|
translation = AulSign( |
|
|
input=mapped_input, |
|
|
rules_prompt_path=rules_prompt_path_sign2text, |
|
|
train_sentences=sentences_train_embeddings, |
|
|
vocabulary=corpus_embeddings, |
|
|
model=model, |
|
|
ollama=False, |
|
|
modality="sign2text" |
|
|
) |
|
|
return translation, mapped_input |
|
|
except Exception as e: |
|
|
return f"Error on AulSign: {str(e)}" |
|
|
|
|
|
|
|
|
model, corpus_embeddings_path, corpus_embeddings, sentences_train_embeddings, rules_prompt_path_text2sign, rules_prompt_path_sign2text = load_resources() |
|
|
|
|
|
|
|
|
with gr.Blocks() as demo: |
|
|
gr.Markdown("# AulSign Translator") |
|
|
gr.Markdown("Translate from Natural Language to Formal SignWriting (FSW) or viceversa.") |
|
|
|
|
|
with gr.Tab("Text to Sign"): |
|
|
text_input = gr.Textbox(label="Insert a sentence", placeholder="Digit here your sentence...") |
|
|
fsw_output = gr.Textbox(label="Output") |
|
|
intermediate_output = gr.Textbox(label="Intermediate Output") |
|
|
|
|
|
translate_button = gr.Button("Translate") |
|
|
translate_button.click(text_to_sign, inputs=text_input, outputs=[fsw_output,intermediate_output]) |
|
|
|
|
|
gr.Examples( |
|
|
examples=["This is a new ASL translator"], |
|
|
inputs=text_input, |
|
|
outputs=[fsw_output,intermediate_output], |
|
|
fn=text_to_sign |
|
|
) |
|
|
|
|
|
with gr.Tab("Sign to Text"): |
|
|
sign_input = gr.Textbox(label="Insert a FSW sequence", placeholder="Digit here your FSW sequence...") |
|
|
text_output = gr.Textbox(label="Output") |
|
|
intermediate_output = gr.Textbox(label="Intermediate Output") |
|
|
|
|
|
reconstruct_button = gr.Button("Translate") |
|
|
reconstruct_button.click(sign_to_text, inputs=sign_input, outputs=[text_output,intermediate_output]) |
|
|
|
|
|
gr.Examples( |
|
|
examples=["M518x584S10004492x534S22a04493x569S30a00482x483 AS33b00S19210S20500S26504M519x547S33b00482x482S20500466x512S26504464x532S19210498x511 M530x522S15a36502x510S1813e501x503S2890f470x478 M512x535S1f720492x466S20320497x485S1dc20488x505 M528x595S10009483x405S10021473x422S2e024488x453S10001491x488S10029493x504S15a48477x548S15a40515x548S22a14476x580S22a04515x580"], |
|
|
inputs=sign_input, |
|
|
outputs=[text_output,intermediate_output], |
|
|
fn=sign_to_text |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.launch(share=True) |
|
|
|