hernanlira commited on
Commit
2b86d4e
·
1 Parent(s): 9d92bda

adding app.py

Browse files
Files changed (5) hide show
  1. .DS_Store +0 -0
  2. app.py +158 -0
  3. environment.yml +177 -0
  4. generate_eban.py +12 -0
  5. logo.png +0 -0
.DS_Store ADDED
Binary file (6.15 kB). View file
 
app.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import json
3
+ import os
4
+ from generate_eban import translate_es_to_mapudungun
5
+
6
+ __TITLE: str = "# Traductor Español - Mapudungun"
7
+
8
+ __DESCRIPTION: str = """
9
+ Ingrese texto en español para traducirlo al mapudungun.
10
+ Luego, puede dar feedback sobre la traducción para ayudar a mejorar nuestro modelo.
11
+ """
12
+
13
+ __FOOTER: str = """
14
+ ---
15
+ (c) 2024 [Inria Chile Research Center](https://inria.cl)
16
+ """
17
+
18
+
19
+ # using eban's model
20
+ def translate_text(text):
21
+ return translate_es_to_mapudungun(text)
22
+
23
+ # Updated function to save feedback
24
+ def save_feedback(input_text, output_text, feedback, correction=None):
25
+ feedback_data = {
26
+ "input": input_text,
27
+ "output": output_text,
28
+ "feedback": feedback,
29
+ "correction": correction
30
+ }
31
+
32
+ # Ensure the feedback directory exists
33
+ os.makedirs("feedback", exist_ok=True)
34
+
35
+ # Save feedback to a JSON file
36
+ with open(f"feedback/feedback_{len(os.listdir('feedback'))}.json", "w") as f:
37
+ json.dump(feedback_data, f)
38
+
39
+ # Main translation function
40
+ def translate_and_store(input_text):
41
+ translated_text = translate_text(input_text)
42
+ return translated_text, *clear_feedback()
43
+
44
+ def clear_feedback():
45
+ return (gr.update(value=""),
46
+ gr.update(value="", visible=False),
47
+ gr.update(visible=False),
48
+ gr.update(visible=False))
49
+
50
+ # Updated feedback functions
51
+ def like_translation(input_text, output_text):
52
+ save_feedback(input_text, output_text, "liked")
53
+ return ("Gracias por tu feedback positivo!",
54
+ gr.update(visible=False),
55
+ gr.update(visible=False),
56
+ gr.update(visible=False))
57
+
58
+ def dislike_translation(input_text, output_text):
59
+ return ("¿Puedes ingresar la traducción correcta?",
60
+ gr.update(visible=True),
61
+ gr.update(visible=True),
62
+ gr.update(visible=True),
63
+ gr.update(value="", visible=False),
64
+ gr.update(visible=False))
65
+
66
+ def yes_provide_correction():
67
+ return (gr.update(visible=True),
68
+ gr.update(visible=True),
69
+ gr.update(visible=False),
70
+ gr.update(visible=False))
71
+
72
+ def no_provide_correction(input_text, output_text):
73
+ save_feedback(input_text, output_text, "disliked")
74
+ return ("Gracias por usar la aplicación.",
75
+ gr.update(visible=False),
76
+ gr.update(visible=False),
77
+ gr.update(visible=False))
78
+
79
+ def submit_correction(input_text, output_text, correction):
80
+ save_feedback(input_text, output_text, "disliked", correction)
81
+ return ("Gracias. Tu corrección nos ayudará a mejorar nuestro modelo de traducción.",
82
+ gr.update(value="", visible=False),
83
+ gr.update(visible=False),
84
+ gr.update(visible=False))
85
+
86
+ # Create the Gradio interface
87
+ with gr.Blocks() as demo:
88
+ gr.Markdown(__TITLE)
89
+ gr.Markdown(__DESCRIPTION)
90
+
91
+ with gr.Row():
92
+ input_text = gr.Textbox(
93
+ label="Ingrese texto en español",
94
+ placeholder="Escriba aquí su texto en español...",
95
+ lines=5, # Increased number of visible lines
96
+ max_lines=10, # Maximum number of lines before scrolling
97
+ )
98
+ output_text = gr.Textbox(
99
+ label="Traducción al mapudungun",
100
+ lines=5, # Increased number of visible lines
101
+ max_lines=10, # Maximum number of lines before scrolling
102
+ )
103
+
104
+ translate_btn = gr.Button("Translate")
105
+
106
+ gr.Markdown("## ¿Cuál es su opinión sobre la traducción?")
107
+
108
+ with gr.Row():
109
+ like_btn = gr.Button("Me gusta la traducción")
110
+ dislike_btn = gr.Button("No me gusta")
111
+
112
+ feedback_output = gr.Textbox(label="Feedback", interactive=False)
113
+
114
+ with gr.Row(visible=False) as correction_choice_row:
115
+ yes_btn = gr.Button("Sí")
116
+ no_btn = gr.Button("No")
117
+
118
+ correction_input = gr.Textbox(label="Traducción correcta", visible=False)
119
+ submit_correction_btn = gr.Button("Enviar corrección", visible=False)
120
+
121
+ translate_btn.click(
122
+ translate_and_store,
123
+ inputs=input_text,
124
+ outputs=[output_text, feedback_output, correction_input, submit_correction_btn, correction_choice_row]
125
+ )
126
+ like_btn.click(
127
+ like_translation,
128
+ inputs=[input_text, output_text],
129
+ outputs=[feedback_output, correction_input, submit_correction_btn, correction_choice_row]
130
+ )
131
+ dislike_btn.click(
132
+ dislike_translation,
133
+ inputs=[input_text, output_text],
134
+ outputs=[feedback_output, correction_choice_row, yes_btn, no_btn, correction_input, submit_correction_btn]
135
+ )
136
+ yes_btn.click(
137
+ yes_provide_correction,
138
+ outputs=[correction_input, submit_correction_btn, yes_btn, no_btn]
139
+ )
140
+ no_btn.click(
141
+ no_provide_correction,
142
+ inputs=[input_text, output_text],
143
+ outputs=[feedback_output, correction_input, submit_correction_btn, correction_choice_row]
144
+ )
145
+ submit_correction_btn.click(
146
+ submit_correction,
147
+ inputs=[input_text, output_text, correction_input],
148
+ outputs=[feedback_output, correction_input, submit_correction_btn, correction_choice_row]
149
+ )
150
+
151
+ # Add footer and logo
152
+ with gr.Row():
153
+ with gr.Column(scale=2):
154
+ gr.Markdown(__FOOTER)
155
+ with gr.Column(scale=1):
156
+ gr.Image("logo.png", show_label=False, container=False, interactive=False)
157
+
158
+ demo.launch()
environment.yml ADDED
@@ -0,0 +1,177 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ name: translator_env
2
+ channels:
3
+ - conda-forge
4
+ dependencies:
5
+ - aiofiles=23.2.1
6
+ - annotated-types=0.7.0
7
+ - anyio=4.6.0
8
+ - aom=3.9.1
9
+ - brotli=1.1.0
10
+ - brotli-bin=1.1.0
11
+ - brotli-python=1.1.0
12
+ - bzip2=1.0.8
13
+ - ca-certificates=2024.8.30
14
+ - cairo=1.18.0
15
+ - certifi=2024.8.30
16
+ - cffi=1.17.1
17
+ - charset-normalizer=3.3.2
18
+ - click=8.1.7
19
+ - colorama=0.4.6
20
+ - contourpy=1.3.0
21
+ - cycler=0.12.1
22
+ - dav1d=1.2.1
23
+ - dnspython=2.6.1
24
+ - email-validator=2.2.0
25
+ - email_validator=2.2.0
26
+ - exceptiongroup=1.2.2
27
+ - expat=2.6.3
28
+ - fastapi=0.115.0
29
+ - fastapi-cli=0.0.5
30
+ - ffmpeg=7.1.0
31
+ - ffmpy=0.3.0
32
+ - filelock=3.16.1
33
+ - font-ttf-dejavu-sans-mono=2.37
34
+ - font-ttf-inconsolata=3.000
35
+ - font-ttf-source-code-pro=2.038
36
+ - font-ttf-ubuntu=0.83
37
+ - fontconfig=2.14.2
38
+ - fonts-conda-ecosystem=1
39
+ - fonts-conda-forge=1
40
+ - fonttools=4.54.1
41
+ - freetype=2.12.1
42
+ - fribidi=1.0.10
43
+ - fsspec=2024.9.0
44
+ - gmp=6.3.0
45
+ - gradio=4.44.1
46
+ - gradio-client=1.3.0
47
+ - graphite2=1.3.13
48
+ - h11=0.14.0
49
+ - h2=4.1.0
50
+ - harfbuzz=9.0.0
51
+ - hpack=4.0.0
52
+ - httpcore=1.0.6
53
+ - httpx=0.27.2
54
+ - huggingface_hub=0.25.1
55
+ - hyperframe=6.0.1
56
+ - icu=75.1
57
+ - idna=3.10
58
+ - importlib-resources=6.4.5
59
+ - importlib_resources=6.4.5
60
+ - jinja2=3.1.4
61
+ - kiwisolver=1.4.7
62
+ - lame=3.100
63
+ - lcms2=2.16
64
+ - lerc=4.0.0
65
+ - libabseil=20240722.0
66
+ - libass=0.17.3
67
+ - libblas=3.9.0
68
+ - libbrotlicommon=1.1.0
69
+ - libbrotlidec=1.1.0
70
+ - libbrotlienc=1.1.0
71
+ - libcblas=3.9.0
72
+ - libcxx=19.1.1
73
+ - libdeflate=1.21
74
+ - libexpat=2.6.3
75
+ - libffi=3.4.2
76
+ - libgfortran=5.0.0
77
+ - libgfortran5=13.2.0
78
+ - libglib=2.82.1
79
+ - libhwloc=2.11.1
80
+ - libiconv=1.17
81
+ - libintl=0.22.5
82
+ - libjpeg-turbo=3.0.0
83
+ - liblapack=3.9.0
84
+ - libopenblas=0.3.27
85
+ - libopenvino=2024.4.0
86
+ - libopenvino-arm-cpu-plugin=2024.4.0
87
+ - libopenvino-auto-batch-plugin=2024.4.0
88
+ - libopenvino-auto-plugin=2024.4.0
89
+ - libopenvino-hetero-plugin=2024.4.0
90
+ - libopenvino-ir-frontend=2024.4.0
91
+ - libopenvino-onnx-frontend=2024.4.0
92
+ - libopenvino-paddle-frontend=2024.4.0
93
+ - libopenvino-pytorch-frontend=2024.4.0
94
+ - libopenvino-tensorflow-frontend=2024.4.0
95
+ - libopenvino-tensorflow-lite-frontend=2024.4.0
96
+ - libopus=1.3.1
97
+ - libpng=1.6.44
98
+ - libprotobuf=5.27.5
99
+ - libsqlite=3.46.1
100
+ - libtiff=4.7.0
101
+ - libvpx=1.14.1
102
+ - libwebp-base=1.4.0
103
+ - libxcb=1.17.0
104
+ - libxml2=2.12.7
105
+ - libzlib=1.3.1
106
+ - llvm-openmp=19.1.0
107
+ - markdown-it-py=3.0.0
108
+ - markupsafe=2.1.5
109
+ - matplotlib-base=3.9.2
110
+ - mdurl=0.1.2
111
+ - munkres=1.1.4
112
+ - ncurses=6.5
113
+ - numpy=2.1.1
114
+ - openh264=2.4.1
115
+ - openjpeg=2.5.2
116
+ - openssl=3.3.2
117
+ - orjson=3.10.7
118
+ - packaging=24.1
119
+ - pandas=2.2.3
120
+ - pcre2=10.44
121
+ - pillow=10.4.0
122
+ - pip=24.2
123
+ - pixman=0.43.4
124
+ - pthread-stubs=0.4
125
+ - pugixml=1.14
126
+ - pycparser=2.22
127
+ - pydantic=2.0.3
128
+ - pydantic-core=2.3.0
129
+ - pydub=0.25.1
130
+ - pygments=2.18.0
131
+ - pyparsing=3.1.4
132
+ - pysocks=1.7.1
133
+ - python=3.11.10
134
+ - python-dateutil=2.9.0
135
+ - python-multipart=0.0.12
136
+ - python-tzdata=2024.2
137
+ - python_abi=3.11
138
+ - pytz=2024.1
139
+ - pyyaml=6.0.2
140
+ - qhull=2020.2
141
+ - readline=8.2
142
+ - requests=2.32.3
143
+ - rich=13.9.1
144
+ - ruff=0.6.8
145
+ - semantic_version=2.10.0
146
+ - setuptools=75.1.0
147
+ - shellingham=1.5.4
148
+ - six=1.16.0
149
+ - snappy=1.2.1
150
+ - sniffio=1.3.1
151
+ - starlette=0.38.6
152
+ - svt-av1=2.2.1
153
+ - tbb=2021.13.0
154
+ - tk=8.6.13
155
+ - tomlkit=0.12.0
156
+ - tqdm=4.66.5
157
+ - typer=0.12.5
158
+ - typer-slim=0.12.5
159
+ - typer-slim-standard=0.12.5
160
+ - typing-extensions=4.12.2
161
+ - typing_extensions=4.12.2
162
+ - tzdata=2024a
163
+ - urllib3=2.2.3
164
+ - uvicorn=0.31.0
165
+ - websockets=12.0
166
+ - wheel=0.44.0
167
+ - x264=1!164.3095
168
+ - x265=3.5
169
+ - xorg-libxau=1.0.11
170
+ - xorg-libxdmcp=1.1.5
171
+ - xz=5.2.6
172
+ - yaml=0.2.5
173
+ - zipp=3.20.2
174
+ - zlib=1.3.1
175
+ - zstandard=0.23.0
176
+ - zstd=1.5.6
177
+ prefix: /Users/hernanlira/miniforge3/envs/translator_env
generate_eban.py ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
+
3
+ def translate_es_to_mapudungun(text_es):
4
+ pipe = pipeline("text2text-generation", model="ebanebssa/es-arn")
5
+ translated_text = pipe(text_es)[0]['generated_text']
6
+ return translated_text
7
+
8
+ # Example usage (can be commented out or removed)
9
+ if __name__ == "__main__":
10
+ text_es = input("Enter text in Spanish: ")
11
+ translated_text = translate_es_to_mapudungun(text_es)
12
+ print("Translated text (Mapudungun):", translated_text)
logo.png ADDED