File size: 5,350 Bytes
2b86d4e
 
 
2d19f05
2b86d4e
 
 
 
 
 
 
 
 
 
 
 
 
135bdf2
2b86d4e
 
 
 
 
586af48
2b86d4e
 
 
 
 
 
 
 
a4f1aad
2b86d4e
 
a4f1aad
2b86d4e
 
 
586af48
2b86d4e
 
 
 
 
 
 
 
 
 
586af48
2b86d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586af48
2b86d4e
 
 
 
 
 
 
 
586af48
 
2b86d4e
 
 
586af48
 
2b86d4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
586af48
2b86d4e
 
 
 
135bdf2
2b86d4e
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import gradio as gr
import json
import os
from generate_translation import translate_es_to_mapudungun

__TITLE: str = "# Traductor Espa帽ol - Mapudungun"

__DESCRIPTION: str = """
Ingrese texto en espa帽ol para traducirlo al mapudungun.
Luego, puede dar feedback sobre la traducci贸n para ayudar a mejorar nuestro modelo.
"""

__FOOTER: str = """
---
(c) 2024 [Inria Chile Research Center](https://inria.cl)
"""

__LOGO_URI: str = """https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSQHNnGyXMYCS-zyYFq-4NLkRCF9AwQmxwdjw&s"""

# using eban's model
def translate_text(text):
    return translate_es_to_mapudungun(text)

# function to save feedback
def save_feedback(input_text, output_text, feedback, correction=None):
    feedback_data = {
        "input": input_text,
        "output": output_text,
        "feedback": feedback,
        "correction": correction
    }
    
    # ensure the feedback directory exists
    os.makedirs("feedback", exist_ok=True)
    
    # save feedback to a JSON file
    with open(f"feedback/feedback_{len(os.listdir('feedback'))}.json", "w") as f:
        json.dump(feedback_data, f)

# function to initial tests
def translate_and_store(input_text):
    translated_text = translate_text(input_text)
    return translated_text, *clear_feedback()

def clear_feedback():
    return (gr.update(value=""), 
            gr.update(value="", visible=False), 
            gr.update(visible=False), 
            gr.update(visible=False))

# feedback functions
def like_translation(input_text, output_text):
    save_feedback(input_text, output_text, "liked")
    return ("Gracias por tu feedback positivo!", 
            gr.update(visible=False), 
            gr.update(visible=False), 
            gr.update(visible=False))

def dislike_translation(input_text, output_text):
    return ("驴Puedes ingresar la traducci贸n correcta?", 
            gr.update(visible=True), 
            gr.update(visible=True), 
            gr.update(visible=True), 
            gr.update(value="", visible=False), 
            gr.update(visible=False))

def yes_provide_correction():
    return (gr.update(visible=True), 
            gr.update(visible=True), 
            gr.update(visible=False), 
            gr.update(visible=False))

def no_provide_correction(input_text, output_text):
    save_feedback(input_text, output_text, "disliked")
    return ("Gracias por usar la aplicaci贸n.", 
            gr.update(visible=False), 
            gr.update(visible=False), 
            gr.update(visible=False))

def submit_correction(input_text, output_text, correction):
    save_feedback(input_text, output_text, "disliked", correction)
    return ("Gracias. Tu correcci贸n nos ayudar谩 a mejorar nuestro modelo de traducci贸n.", 
            gr.update(value="", visible=False), 
            gr.update(visible=False), 
            gr.update(visible=False))

# create the gradio interface
with gr.Blocks() as demo:
    gr.Markdown(__TITLE)
    gr.Markdown(__DESCRIPTION)
    
    with gr.Row():
        input_text = gr.Textbox(
            label="Ingrese texto en espa帽ol",
            placeholder="Escriba aqu铆 su texto en espa帽ol...",
            lines=5,  
            max_lines=10,  
        )
        output_text = gr.Textbox(
            label="Traducci贸n al mapudungun",
            lines=5,  
            max_lines=10,  
        )
    
    translate_btn = gr.Button("Translate")

    gr.Markdown("## 驴Cu谩l es su opini贸n sobre la traducci贸n?")

    with gr.Row():
        like_btn = gr.Button("Me gusta la traducci贸n")
        dislike_btn = gr.Button("No me gusta")
    
    feedback_output = gr.Textbox(label="Feedback", interactive=False)
    
    with gr.Row(visible=False) as correction_choice_row:
        yes_btn = gr.Button("S铆")
        no_btn = gr.Button("No")
    
    correction_input = gr.Textbox(label="Traducci贸n correcta", visible=False)
    submit_correction_btn = gr.Button("Enviar correcci贸n", visible=False)
    
    translate_btn.click(
        translate_and_store, 
        inputs=input_text, 
        outputs=[output_text, feedback_output, correction_input, submit_correction_btn, correction_choice_row]
    )
    like_btn.click(
        like_translation, 
        inputs=[input_text, output_text], 
        outputs=[feedback_output, correction_input, submit_correction_btn, correction_choice_row]
    )
    dislike_btn.click(
        dislike_translation, 
        inputs=[input_text, output_text], 
        outputs=[feedback_output, correction_choice_row, yes_btn, no_btn, correction_input, submit_correction_btn]
    )
    yes_btn.click(
        yes_provide_correction, 
        outputs=[correction_input, submit_correction_btn, yes_btn, no_btn]
    )
    no_btn.click(
        no_provide_correction, 
        inputs=[input_text, output_text], 
        outputs=[feedback_output, correction_input, submit_correction_btn, correction_choice_row]
    )
    submit_correction_btn.click(
        submit_correction, 
        inputs=[input_text, output_text, correction_input], 
        outputs=[feedback_output, correction_input, submit_correction_btn, correction_choice_row]
    )
    
    
    with gr.Row():
        with gr.Column(scale=2):
            gr.Markdown(__FOOTER)
        with gr.Column(scale=1):
            gr.HTML(value=f"<img width='92%' src='{__LOGO_URI}' alt='logo'/>")
demo.launch()