|
import gradio as gr |
|
import json |
|
import re |
|
from huggingface_hub import InferenceClient |
|
|
|
client = InferenceClient( |
|
"mistralai/Mistral-7B-Instruct-v0.1" |
|
) |
|
|
|
system_prompt = """[INST] <<SYS>> |
|
You are a helpful, respectful and honest assistant. Always answer in spanish. Never answer in English. Always answer as |
|
helpfully as possible, while being safe. |
|
|
|
If a question does not make any sense, or is not factually coherent, explain |
|
why instead of answering something not correct. If you don't know the answer |
|
to a question, please don't share false information. |
|
|
|
Your goal is to provide answers relating to a legal document, making classifications of arguments inside an allegation.<</SYS>> |
|
""" |
|
|
|
rag_text = """ |
|
""" |
|
|
|
format_text = """{ |
|
'tipo': "clasificaci贸n del argumento (Argumento tipo X)", |
|
'descripcion': "Descripcion breve del argumento", |
|
'texto_referencia': "texto original completo perteneciente a dicho argumento" |
|
}""" |
|
|
|
prompt_template_text = """ |
|
--- Lista Argumentos --- |
|
* Argumento tipo 49 (Desvirtuaci贸n de los hechos, el denunciado no est谩 de acuerdo con el relato de los hechos) |
|
* Argumento tipo 304 (El denunciado solicita medios de prueba) |
|
* Argumento tipo 994 (El denunciado solicita fotograf铆a como medio de prueba, desafiando el relato del agente denunciante) |
|
* Argumento tipo 1002 (Prescripci贸n, el delito ha prescrito) |
|
* Argumento tipo 2014 (Principio de proporcionalidad, denunciado considera sanci贸n excesiva) |
|
* Argumento tipo 2027 (Niega desobediencia de se帽ales de tr谩fico de prohibici贸n) |
|
* Argumento tipo 2002 (Denuncia no notificada en el momento en el que se formul贸) |
|
* Argumento tipo 1001 (Defecto de forma en la denuncia, la sanci贸n contiene alg煤n error) |
|
""" |
|
|
|
recorte = """ |
|
PRIMERA.- Que los hechos descritos en la denuncia no se corresponden con la realidad de lo |
|
sucedido, por lo que los niego expresamente. La carga de la prueba corresponde al 贸rgano instructor |
|
del procedimiento, en virtud del principio de presunci贸n de inocencia recogido en el art.24 de la |
|
Constituci贸n Espa帽ola y en el art. 53 de la Ley 39/2015 de 1 de Octubre del Procedimiento |
|
Administrativo Com煤n de las Administraciones P煤blicas. |
|
""" |
|
|
|
def format_prompt(message): |
|
prompt = "<s>" |
|
prompt += f"[INST] {message} [/INST]" |
|
return prompt |
|
|
|
def generate(prompt): |
|
|
|
generate_kwargs = dict( |
|
temperature=0.9, |
|
max_new_tokens=1024, |
|
top_p=0.95, |
|
repetition_penalty=1.0, |
|
do_sample=True, |
|
seed=42, |
|
) |
|
|
|
formatted_prompt = format_prompt(prompt) |
|
|
|
output = client.text_generation(formatted_prompt, **generate_kwargs) |
|
|
|
return output |
|
|
|
|
|
def process_input(text, rag, prompt_template): |
|
|
|
if not(rag): |
|
rag_text = " " |
|
|
|
|
|
|
|
prompt = f""" |
|
{system_prompt} |
|
|
|
Teniendo en cuenta que los argumentos se clasifican de la siguiente manera: |
|
{prompt_template_text} |
|
|
|
Partiendo del siguiente fragmento de texto: |
|
|
|
Fragmento: |
|
--------------------------------------------------------------- |
|
{text} |
|
-------------------------------------------------------------- |
|
|
|
Identifica y clasifica los argumentos expuestos en el anterior fragmento de texto siguiendo estos pasos: |
|
Devuelve una lista de JSON, cuya longitud ser谩 igual al n煤mero de argumentos encontrados, que contenga los argumentos clasificados con el siguiente formato: |
|
{format_text} |
|
|
|
|
|
Recuerda responder en espa帽ol. Los argumentos que no se puedan clasificar o que *claramente* no encajen con ning煤n tipo predefinido ser谩n clasificados como 'Desconocido'. Recuerda que los tipos de argumentos de representan con un n煤mero que puedes encontrar en la informaci贸n anterior. |
|
Respira profundamente y piensa paso a paso. |
|
[/INST] |
|
|
|
|
|
""" |
|
output = generate(prompt) |
|
|
|
|
|
with open('output2.json', 'w') as f: |
|
json.dump(output, f) |
|
|
|
return output |
|
|
|
def create_interface(): |
|
|
|
input_text = gr.Textbox(label="Input") |
|
rag_checkbox = gr.Checkbox(label="RAG") |
|
prompt_template = gr.Checkbox(label="PromptTemplate") |
|
output_text = gr.Textbox(label="Output") |
|
classification_types_checkboxes = gr.CheckboxGroup(label="Clasificacion Tipo") |
|
|
|
|
|
def fn(text, rag, prompt_template): |
|
|
|
output = process_input(text, rag, prompt_template) |
|
matches = re.findall(r'"tipo"\s*:\s*"(.*?)"', output) |
|
classification_types = matches |
|
classification_types_options = [(option, option) for option in classification_types] |
|
classification_types_checkboxes = gr.CheckboxGroup(label="Clasificacion Tipo", choices=classification_types_options, interactive = True) |
|
|
|
return output, classification_types_checkboxes |
|
|
|
|
|
def mark_checkboxes(output, classification_types_options): |
|
|
|
for option in classification_types_options: |
|
classification_types_checkboxes.set_checkbox_value(optio |
|
|
|
|
|
def save_output(output, classification_types_options): |
|
|
|
with open('output.txt', 'w') as f: |
|
f.write(output) |
|
|
|
for option in classification_types_options: |
|
classification_types_checkboxes.set_checkbox_value(option[0], True) |
|
|
|
n[0], True) |
|
|
|
examples = [ |
|
[recorte, False, True] |
|
] |
|
|
|
|
|
iface = gr.Interface( |
|
fn=fn, |
|
inputs=[input_text, rag_checkbox, prompt_template], |
|
outputs=[output_text, classification_types_checkboxes], |
|
examples=examples |
|
live=True, |
|
fn_on_change=mark_checkboxes, |
|
fn_on_change=save_output |
|
) |
|
|
|
return iface |
|
|
|
iface = create_interface() |
|
iface.launch() |