Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from peft import PeftModel, PeftConfig
|
4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
5 |
+
|
6 |
+
peft_model_id = "rwheel/discriminacion_gitana_intervenciones"
|
7 |
+
config = PeftConfig.from_pretrained(peft_model_id)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(config.base_model_name_or_path, return_dict=True, load_in_8bit=True, device_map='auto')
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained(peft_model_id)
|
10 |
+
# Load the Lora model
|
11 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
12 |
+
|
13 |
+
def predecir_intervencion(text):
|
14 |
+
text = "<SH>" + text + " Intervención: "
|
15 |
+
batch = tokenizer(text, return_tensors='pt')
|
16 |
+
with torch.cuda.amp.autocast():
|
17 |
+
output_tokens = model.generate(**batch, max_new_tokens=256, eos_token_id=50258)
|
18 |
+
|
19 |
+
output = tokenizer.decode(output_tokens[0], skip_special_tokens=False)
|
20 |
+
|
21 |
+
aux = output.split("Intervención:")[1].strip()
|
22 |
+
intervencion = aux.split("Resultado:")[0].strip()
|
23 |
+
resultado = aux.split("Resultado:")[1].split("<EH>")[0].strip()
|
24 |
+
|
25 |
+
return intervencion, resultado
|
26 |
+
|
27 |
+
with gr.Blocks() as demo:
|
28 |
+
gr.Markdown("Predicción de intervenciones para mitigar el daño racista en el pueblo gitano")
|
29 |
+
with gr.Row():
|
30 |
+
hechos = gr.Textbox(placeholder="Un alumno gitano de un Instituto...")
|
31 |
+
with gr.Row():
|
32 |
+
intervencion = gr.Textbox()
|
33 |
+
resultado = gr.Textbox()
|
34 |
+
|
35 |
+
btn = gr.Button("Go")
|
36 |
+
btn.click(fn=predecir_intervencion, inputs=hechos, outputs=[intervencion, resultado])
|
37 |
+
|
38 |
+
demo.launch(share=True)
|