Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,6 +12,7 @@ import os
|
|
| 12 |
#sft_model = "somosnlp/RecetasDeLaAbuela_mistral-7b-instruct-v0.2-bnb-4bit"
|
| 13 |
#base_model_name = "unsloth/Mistral-7B-Instruct-v0.2"
|
| 14 |
sft_model = "somosnlp/RecetasDeLaAbuela_gemma-2b-it-bnb-4bit"
|
|
|
|
| 15 |
base_model_name = "unsloth/gemma-2b-it-bnb-4bit"
|
| 16 |
|
| 17 |
bnb_config = BitsAndBytesConfig(
|
|
@@ -66,7 +67,13 @@ stopping_criteria = ListOfTokensStoppingCriteria(tokenizer, stop_tokens)
|
|
| 66 |
# Añade tu criterio de parada a una StoppingCriteriaList
|
| 67 |
stopping_criteria_list = StoppingCriteriaList([stopping_criteria])
|
| 68 |
|
| 69 |
-
def generate_text(prompt, context, max_length=2100):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
prompt=prompt.replace("\n", "").replace("¿","").replace("?","")
|
| 71 |
input_text = f'''<bos><start_of_turn>system ¿{context}?<end_of_turn><start_of_turn>user ¿{prompt}?<end_of_turn><start_of_turn>model'''
|
| 72 |
inputs = tokenizer.encode(input_text, return_tensors="pt", add_special_tokens=False).to("cuda:0")
|
|
@@ -82,9 +89,9 @@ def generate_text(prompt, context, max_length=2100):
|
|
| 82 |
outputs = model.generate(generation_config=generation_config, input_ids=inputs, stopping_criteria=stopping_criteria_list,)
|
| 83 |
return tokenizer.decode(outputs[0], skip_special_tokens=False) #True
|
| 84 |
|
| 85 |
-
def mostrar_respuesta(pregunta, contexto):
|
| 86 |
try:
|
| 87 |
-
res= generate_text(pregunta, contexto, max_length=500)
|
| 88 |
return str(res)
|
| 89 |
except Exception as e:
|
| 90 |
return str(e)
|
|
@@ -96,9 +103,14 @@ mis_ejemplos = [
|
|
| 96 |
["¿Como se cocinan unos autenticos frijoles?"],
|
| 97 |
]
|
| 98 |
|
|
|
|
|
|
|
|
|
|
| 99 |
iface = gr.Interface(
|
| 100 |
fn=mostrar_respuesta,
|
| 101 |
-
inputs=[gr.
|
|
|
|
|
|
|
| 102 |
outputs=[gr.Textbox(label="Respuesta", lines=2),],
|
| 103 |
title="Recetas de la Abuel@",
|
| 104 |
description="Introduce tu pregunta sobre recetas de cocina.",
|
|
|
|
| 12 |
#sft_model = "somosnlp/RecetasDeLaAbuela_mistral-7b-instruct-v0.2-bnb-4bit"
|
| 13 |
#base_model_name = "unsloth/Mistral-7B-Instruct-v0.2"
|
| 14 |
sft_model = "somosnlp/RecetasDeLaAbuela_gemma-2b-it-bnb-4bit"
|
| 15 |
+
sft_model2 = "somosnlp/RecetasDeLaAbuela_mistral-7b-instruct-v0.2-bnb-4bit"
|
| 16 |
base_model_name = "unsloth/gemma-2b-it-bnb-4bit"
|
| 17 |
|
| 18 |
bnb_config = BitsAndBytesConfig(
|
|
|
|
| 67 |
# Añade tu criterio de parada a una StoppingCriteriaList
|
| 68 |
stopping_criteria_list = StoppingCriteriaList([stopping_criteria])
|
| 69 |
|
| 70 |
+
def generate_text(modelin, prompt, context, max_length=2100):
|
| 71 |
+
print('Modelo es: '+modelin)
|
| 72 |
+
if (modelin is not sft_model):
|
| 73 |
+
sft_model = modelin
|
| 74 |
+
ft_model = PeftModel.from_pretrained(base_model, sft_model)
|
| 75 |
+
model = ft_model.merge_and_unload()
|
| 76 |
+
|
| 77 |
prompt=prompt.replace("\n", "").replace("¿","").replace("?","")
|
| 78 |
input_text = f'''<bos><start_of_turn>system ¿{context}?<end_of_turn><start_of_turn>user ¿{prompt}?<end_of_turn><start_of_turn>model'''
|
| 79 |
inputs = tokenizer.encode(input_text, return_tensors="pt", add_special_tokens=False).to("cuda:0")
|
|
|
|
| 89 |
outputs = model.generate(generation_config=generation_config, input_ids=inputs, stopping_criteria=stopping_criteria_list,)
|
| 90 |
return tokenizer.decode(outputs[0], skip_special_tokens=False) #True
|
| 91 |
|
| 92 |
+
def mostrar_respuesta(modelo, pregunta, contexto):
|
| 93 |
try:
|
| 94 |
+
res= generate_text(modelo, pregunta, contexto, max_length=500)
|
| 95 |
return str(res)
|
| 96 |
except Exception as e:
|
| 97 |
return str(e)
|
|
|
|
| 103 |
["¿Como se cocinan unos autenticos frijoles?"],
|
| 104 |
]
|
| 105 |
|
| 106 |
+
|
| 107 |
+
modelo = ["somosnlp/RecetasDeLaAbuela_gemma-2b-it-bnb-4bit", "somosnlp/RecetasDeLaAbuela_mistral-7b-instruct-v0.2-bnb-4bit"]
|
| 108 |
+
|
| 109 |
iface = gr.Interface(
|
| 110 |
fn=mostrar_respuesta,
|
| 111 |
+
inputs=[gr.Dropdown(modelos, type="index"),
|
| 112 |
+
gr.Textbox(label="Pregunta"),
|
| 113 |
+
gr.Textbox(label="Contexto", value="You are a helpful AI assistant. Eres un experto cocinero hispanoamericano."),],
|
| 114 |
outputs=[gr.Textbox(label="Respuesta", lines=2),],
|
| 115 |
title="Recetas de la Abuel@",
|
| 116 |
description="Introduce tu pregunta sobre recetas de cocina.",
|