File size: 4,627 Bytes
149cddd
 
b9278d3
149cddd
 
 
 
 
 
 
 
b9278d3
 
 
 
 
 
 
149cddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import gradio as gr
import json
import zipfile
import pandas as pd
from sentence_transformers import SentenceTransformer
# Importa le funzioni dal tuo script
from scripts.aulsign import AulSign
from scripts.scripts.sign2text_mapping import sign2text

# Configurazione iniziale del modello e dei dati
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.")

    # Modello di embedding
    model_name = "mixedbread-ai/mxbai-embed-large-v1"
    model = SentenceTransformer(model_name)

    # Percorsi dei file
    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'

    # Carica dati
    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

# Funzione per la modalità text2sign
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)}"
# Funzione per la modalità sign2text
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)}"

# Carica le risorse
model, corpus_embeddings_path, corpus_embeddings, sentences_train_embeddings, rules_prompt_path_text2sign, rules_prompt_path_sign2text = load_resources()

# Interfaccia Gradio
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
        )

# Avvia l'app
if __name__ == "__main__":
    demo.launch(share=True)