Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
import torch
|
| 4 |
from torch.nn.functional import softmax
|
| 5 |
|
| 6 |
-
# Cargar el modelo y el tokenizer de google
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-2b")
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
"google
|
| 10 |
-
|
| 11 |
-
|
| 12 |
)
|
|
|
|
| 13 |
|
| 14 |
-
# Función para generar el prompt dependiendo del idioma seleccionado
|
| 15 |
def generar_prompt(message, tipo_clasificacion, idioma):
|
| 16 |
if idioma == "Español":
|
| 17 |
politicas = {
|
|
@@ -84,28 +84,12 @@ def generar_prompt(message, tipo_clasificacion, idioma):
|
|
| 84 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, language, harm_type):
|
| 85 |
prompt = generar_prompt(message, harm_type, language)
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
with torch.no_grad():
|
| 90 |
-
logits = model(**inputs).logits
|
| 91 |
-
|
| 92 |
-
# Extraer los logits para los tokens 'Yes'/'No' en inglés o 'Sí'/'No' en español
|
| 93 |
-
vocab = tokenizer.get_vocab()
|
| 94 |
-
if language == "Español":
|
| 95 |
-
selected_logits = logits[0, -1, [vocab['Sí'], vocab['No']]]
|
| 96 |
-
else:
|
| 97 |
-
selected_logits = logits[0, -1, [vocab['Yes'], vocab['No']]]
|
| 98 |
-
|
| 99 |
-
# Convertir los logits en una probabilidad con softmax
|
| 100 |
-
probabilities = softmax(selected_logits, dim=0)
|
| 101 |
-
|
| 102 |
-
# Devolver la probabilidad de 'Sí'/'Yes' y la respuesta generada
|
| 103 |
-
score_yes = probabilities[0].item()
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
response = f"Score for 'Yes' (violation): {score_yes:.4f}"
|
| 109 |
|
| 110 |
return response
|
| 111 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
| 4 |
from torch.nn.functional import softmax
|
| 5 |
|
| 6 |
+
# Cargar el modelo cuantizado y el tokenizer de google-shieldgemma-2b
|
|
|
|
| 7 |
model = AutoModelForCausalLM.from_pretrained(
|
| 8 |
+
"PrunaAI/google-shieldgemma-2b-bnb-4bit-smashed",
|
| 9 |
+
trust_remote_code=True,
|
| 10 |
+
device_map='auto'
|
| 11 |
)
|
| 12 |
+
tokenizer = AutoTokenizer.from_pretrained("google/shieldgemma-2b")
|
| 13 |
|
| 14 |
+
# Función para generar el prompt dependiendo del idioma seleccionado y el tipo de contenido
|
| 15 |
def generar_prompt(message, tipo_clasificacion, idioma):
|
| 16 |
if idioma == "Español":
|
| 17 |
politicas = {
|
|
|
|
| 84 |
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p, language, harm_type):
|
| 85 |
prompt = generar_prompt(message, harm_type, language)
|
| 86 |
|
| 87 |
+
# Tokenizar el mensaje de entrada
|
| 88 |
+
input_ids = tokenizer(prompt, return_tensors="pt").to(model.device)["input_ids"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
# Generar la salida con el modelo cuantizado
|
| 91 |
+
outputs = model.generate(input_ids, max_new_tokens=max_tokens, temperature=temperature, top_p=top_p)
|
| 92 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 93 |
|
| 94 |
return response
|
| 95 |
|