Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -13,59 +13,24 @@ SHOW_MODEL_PARAMETERS_IN_UI = os.environ.get("SHOW_MODEL_PARAMETERS_IN_UI", defa
|
|
13 |
setup()
|
14 |
|
15 |
|
16 |
-
rag = RAG(
|
17 |
-
hf_token=os.getenv("HF_TOKEN"),
|
18 |
-
embeddings_model=os.getenv("EMBEDDINGS"),
|
19 |
-
model_name=os.getenv("MODEL"),
|
20 |
-
|
21 |
|
22 |
-
)
|
23 |
|
24 |
-
|
25 |
-
|
26 |
try:
|
27 |
-
|
28 |
-
return output, context, source
|
29 |
except HTTPError as err:
|
30 |
if err.code == 400:
|
31 |
gr.Warning(
|
32 |
"The inference endpoint is only available Monday through Friday, from 08:00 to 20:00 CET."
|
33 |
)
|
|
|
34 |
except:
|
35 |
gr.Warning(
|
36 |
"Inference endpoint is not available right now. Please try again later."
|
37 |
)
|
38 |
-
|
39 |
-
|
40 |
-
def submit_input(input_, sysprompt_, num_chunks, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature):
|
41 |
-
if input_.strip() == "":
|
42 |
-
gr.Warning("Not possible to inference an empty input")
|
43 |
-
return None
|
44 |
-
|
45 |
-
|
46 |
-
model_parameters = {
|
47 |
-
"NUM_CHUNKS": num_chunks,
|
48 |
-
"max_new_tokens": max_new_tokens,
|
49 |
-
"repetition_penalty": repetition_penalty,
|
50 |
-
"top_k": top_k,
|
51 |
-
"top_p": top_p,
|
52 |
-
"do_sample": do_sample,
|
53 |
-
"temperature": temperature
|
54 |
-
}
|
55 |
-
|
56 |
-
output, context, source = generate(input_, sysprompt_, model_parameters)
|
57 |
-
sources_markup = ""
|
58 |
-
|
59 |
-
for url in source:
|
60 |
-
sources_markup += f'<a href="{url}" target="_blank">{url}</a><br>'
|
61 |
-
|
62 |
-
return output.strip(), sources_markup, context
|
63 |
-
|
64 |
-
|
65 |
-
def change_interactive(text):
|
66 |
-
if len(text) == 0:
|
67 |
-
return gr.update(interactive=True), gr.update(interactive=False)
|
68 |
-
return gr.update(interactive=True), gr.update(interactive=True)
|
69 |
|
70 |
|
71 |
def clear():
|
@@ -75,48 +40,17 @@ def clear():
|
|
75 |
None,
|
76 |
None,
|
77 |
gr.Slider(value=2.0),
|
78 |
-
gr.Slider(value=MAX_NEW_TOKENS),
|
79 |
-
gr.Slider(value=1.0),
|
80 |
-
gr.Slider(value=50),
|
81 |
-
gr.Slider(value=0.99),
|
82 |
-
gr.Checkbox(value=False),
|
83 |
-
gr.Slider(value=0.35),
|
84 |
)
|
85 |
|
86 |
|
87 |
def gradio_app():
|
88 |
with gr.Blocks(theme=theme) as demo:
|
89 |
-
with gr.Row():
|
90 |
-
with gr.Column(scale=0.1):
|
91 |
-
gr.Image("rag_image.jpg", elem_id="flor-banner", scale=1, height=256, width=256, show_label=False, show_download_button = False, show_share_button = False)
|
92 |
-
with gr.Column():
|
93 |
-
gr.Markdown(
|
94 |
-
"""# Demo de Retrieval-Augmented Generation per documents legals
|
95 |
-
🔍 **Retrieval-Augmented Generation** (RAG) és una tecnologia de IA que permet interrogar un repositori de documents amb preguntes
|
96 |
-
en llenguatge natural, i combina tècniques de recuperació d'informació avançades amb models generatius per redactar una resposta
|
97 |
-
fent servir només la informació existent en els documents del repositori.
|
98 |
-
|
99 |
-
🎯 **Objectiu:** Aquest és un primer demostrador amb la normativa vigent publicada al Diari Oficial de la Generalitat de Catalunya, en el
|
100 |
-
repositori del EADOP (Entitat Autònoma del Diari Oficial i de Publicacions). Aquesta primera versió explora prop de 2000 documents en català,
|
101 |
-
i genera la resposta fent servir un model 7b experimental, entrenat amb el dataset de QA generativa projecte-aina/RAG_Multilingual.
|
102 |
-
|
103 |
-
⚠️ **Advertencies**: Primera versió experimental. El contingut generat per aquest model no està supervisat i pot ser incorrecte.
|
104 |
-
Si us plau, tingueu-ho en compte quan exploreu aquest recurs.
|
105 |
-
"""
|
106 |
-
)
|
107 |
with gr.Row(equal_height=True):
|
108 |
with gr.Column(variant="panel"):
|
109 |
input_ = Textbox(
|
110 |
lines=11,
|
111 |
label="Input",
|
112 |
placeholder="Quina és la finalitat del Servei Meteorològic de Catalunya?",
|
113 |
-
# value = "Quina és la finalitat del Servei Meteorològic de Catalunya?"
|
114 |
-
)
|
115 |
-
sysprompt_ = Textbox(
|
116 |
-
lines=2,
|
117 |
-
label="System",
|
118 |
-
placeholder="Below is a question that you should answer based on the given context. Write a response that answers the question using only information provided in the context.",
|
119 |
-
value = "Below is a question that you should answer based on the given context. Write a response that answers the question using only information provided in the context."
|
120 |
)
|
121 |
with gr.Row(variant="panel"):
|
122 |
clear_btn = Button(
|
@@ -133,50 +67,11 @@ def gradio_app():
|
|
133 |
value=2,
|
134 |
label="Number of chunks"
|
135 |
)
|
136 |
-
max_new_tokens = Slider(
|
137 |
-
minimum=50,
|
138 |
-
maximum=2000,
|
139 |
-
step=1,
|
140 |
-
value=MAX_NEW_TOKENS,
|
141 |
-
label="Max tokens"
|
142 |
-
)
|
143 |
-
repetition_penalty = Slider(
|
144 |
-
minimum=0.1,
|
145 |
-
maximum=2.0,
|
146 |
-
step=0.1,
|
147 |
-
value=1.0,
|
148 |
-
label="Repetition penalty"
|
149 |
-
)
|
150 |
-
top_k = Slider(
|
151 |
-
minimum=1,
|
152 |
-
maximum=100,
|
153 |
-
step=1,
|
154 |
-
value=50,
|
155 |
-
label="Top k"
|
156 |
-
)
|
157 |
-
top_p = Slider(
|
158 |
-
minimum=0.01,
|
159 |
-
maximum=0.99,
|
160 |
-
value=0.99,
|
161 |
-
label="Top p"
|
162 |
-
)
|
163 |
-
do_sample = Checkbox(
|
164 |
-
value=False,
|
165 |
-
label="Do sample"
|
166 |
-
)
|
167 |
-
temperature = Slider(
|
168 |
-
minimum=0.1,
|
169 |
-
maximum=1,
|
170 |
-
value=0.35,
|
171 |
-
label="Temperature"
|
172 |
-
)
|
173 |
-
|
174 |
-
parameters_compontents = [num_chunks, max_new_tokens, repetition_penalty, top_k, top_p, do_sample, temperature]
|
175 |
|
176 |
with gr.Column(variant="panel"):
|
177 |
output = Textbox(
|
178 |
lines=10,
|
179 |
-
label="
|
180 |
interactive=False,
|
181 |
show_copy_button=True
|
182 |
)
|
@@ -215,53 +110,18 @@ def gradio_app():
|
|
215 |
clear_btn.click(
|
216 |
fn=clear,
|
217 |
inputs=[],
|
218 |
-
outputs=[input_, output, source_context, context_evaluation]
|
219 |
queue=False,
|
220 |
api_name=False
|
221 |
)
|
222 |
|
223 |
submit_btn.click(
|
224 |
fn=submit_input,
|
225 |
-
inputs=[input_,
|
226 |
outputs=[output, source_context, context_evaluation],
|
227 |
-
api_name="get-
|
228 |
)
|
229 |
|
230 |
-
with gr.Row():
|
231 |
-
with gr.Column(scale=0.5):
|
232 |
-
gr.Examples(
|
233 |
-
examples=[
|
234 |
-
["""Què és l'EADOP (Entitat Autònoma del Diari Oficial i de Publicacions)?"""],
|
235 |
-
],
|
236 |
-
inputs=input_,
|
237 |
-
outputs=[output, source_context, context_evaluation],
|
238 |
-
fn=submit_input,
|
239 |
-
)
|
240 |
-
gr.Examples(
|
241 |
-
examples=[
|
242 |
-
["""Què diu el decret sobre la senyalització de les begudes alcohòliques i el tabac a Catalunya?"""],
|
243 |
-
],
|
244 |
-
inputs=input_,
|
245 |
-
outputs=[output, source_context, context_evaluation],
|
246 |
-
fn=submit_input,
|
247 |
-
)
|
248 |
-
gr.Examples(
|
249 |
-
examples=[
|
250 |
-
["""Com es pot inscriure una persona al Registre de catalans i catalanes residents a l'exterior?"""],
|
251 |
-
],
|
252 |
-
inputs=input_,
|
253 |
-
outputs=[output, source_context, context_evaluation],
|
254 |
-
fn=submit_input,
|
255 |
-
)
|
256 |
-
gr.Examples(
|
257 |
-
examples=[
|
258 |
-
["""Quina és la finalitat del Servei Meterològic de Catalunya ?"""],
|
259 |
-
],
|
260 |
-
inputs=input_,
|
261 |
-
outputs=[output, source_context, context_evaluation],
|
262 |
-
fn=submit_input,
|
263 |
-
)
|
264 |
-
|
265 |
demo.launch(show_api=True)
|
266 |
|
267 |
|
|
|
13 |
setup()
|
14 |
|
15 |
|
16 |
+
rag = RAG(embeddings_model=os.getenv("EMBEDDINGS"))
|
|
|
|
|
|
|
|
|
17 |
|
|
|
18 |
|
19 |
+
def eadop_rag(prompt, num_chunks):
|
20 |
+
model_parameters = {"NUM_CHUNKS": num_chunks}
|
21 |
try:
|
22 |
+
retrun rag.get_context(prompt, model_parameters)
|
|
|
23 |
except HTTPError as err:
|
24 |
if err.code == 400:
|
25 |
gr.Warning(
|
26 |
"The inference endpoint is only available Monday through Friday, from 08:00 to 20:00 CET."
|
27 |
)
|
28 |
+
return None, None, None
|
29 |
except:
|
30 |
gr.Warning(
|
31 |
"Inference endpoint is not available right now. Please try again later."
|
32 |
)
|
33 |
+
return None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
|
36 |
def clear():
|
|
|
40 |
None,
|
41 |
None,
|
42 |
gr.Slider(value=2.0),
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
)
|
44 |
|
45 |
|
46 |
def gradio_app():
|
47 |
with gr.Blocks(theme=theme) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
with gr.Row(equal_height=True):
|
49 |
with gr.Column(variant="panel"):
|
50 |
input_ = Textbox(
|
51 |
lines=11,
|
52 |
label="Input",
|
53 |
placeholder="Quina és la finalitat del Servei Meteorològic de Catalunya?",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
)
|
55 |
with gr.Row(variant="panel"):
|
56 |
clear_btn = Button(
|
|
|
67 |
value=2,
|
68 |
label="Number of chunks"
|
69 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
71 |
with gr.Column(variant="panel"):
|
72 |
output = Textbox(
|
73 |
lines=10,
|
74 |
+
label="Context",
|
75 |
interactive=False,
|
76 |
show_copy_button=True
|
77 |
)
|
|
|
110 |
clear_btn.click(
|
111 |
fn=clear,
|
112 |
inputs=[],
|
113 |
+
outputs=[input_, output, source_context, context_evaluation, num_chunks],
|
114 |
queue=False,
|
115 |
api_name=False
|
116 |
)
|
117 |
|
118 |
submit_btn.click(
|
119 |
fn=submit_input,
|
120 |
+
inputs=[input_, num_chunks],
|
121 |
outputs=[output, source_context, context_evaluation],
|
122 |
+
api_name="get-eadop-rag"
|
123 |
)
|
124 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
demo.launch(show_api=True)
|
126 |
|
127 |
|