Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,12 +2,10 @@ import gradio as gr
|
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
from peft import PeftModel
|
5 |
-
|
6 |
# !python -c "import torch; assert torch.cuda.get_device_capability()[0] >= 8, 'Hardware not supported for Flash Attention'"
|
7 |
import json
|
8 |
import torch
|
9 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, GemmaTokenizer, StoppingCriteria, StoppingCriteriaList, GenerationConfig
|
10 |
-
# from google.colab import userdata
|
11 |
import os
|
12 |
|
13 |
#sft_model = "somosnlp/gemma-FULL-RAC-Colombia_v2"
|
@@ -16,7 +14,6 @@ import os
|
|
16 |
sft_model = "somosnlp/RecetasDeLaAbuela_gemma-2b-it-bnb-4bit"
|
17 |
base_model_name = "unsloth/gemma-2b-it-bnb-4bit"
|
18 |
|
19 |
-
|
20 |
bnb_config = BitsAndBytesConfig(
|
21 |
load_in_4bit=True,
|
22 |
bnb_4bit_quant_type="nf4",
|
@@ -42,7 +39,6 @@ model.save_pretrained(".")
|
|
42 |
#model.to('cuda')
|
43 |
tokenizer.save_pretrained(".")
|
44 |
|
45 |
-
|
46 |
class ListOfTokensStoppingCriteria(StoppingCriteria):
|
47 |
"""
|
48 |
Clase para definir un criterio de parada basado en una lista de tokens específicos.
|
@@ -71,22 +67,9 @@ stopping_criteria = ListOfTokensStoppingCriteria(tokenizer, stop_tokens)
|
|
71 |
stopping_criteria_list = StoppingCriteriaList([stopping_criteria])
|
72 |
|
73 |
def generate_text(prompt, max_length=2100):
|
74 |
-
# prompt="""What were the main contributions of Eratosthenes to the development of mathematics in ancient Greece?"""
|
75 |
prompt=prompt.replace("\n", "").replace("¿","").replace("?","")
|
76 |
-
|
77 |
-
|
78 |
-
input_text = f'''<bos><start_of_turn>system
|
79 |
-
You are a helpful AI assistant.
|
80 |
-
Responde en formato json.
|
81 |
-
Eres un experto cocinero de la cocina hispanoamericana.<end_of_turn>
|
82 |
-
<start_of_turn>user
|
83 |
-
¿{prompt}?<end_of_turn>
|
84 |
-
<start_of_turn>model
|
85 |
-
'''
|
86 |
-
|
87 |
-
inputs = tokenizer.encode(input_text,
|
88 |
-
return_tensors="pt",
|
89 |
-
add_special_tokens=False).to("cuda:0")
|
90 |
max_new_tokens=max_length
|
91 |
generation_config = GenerationConfig(
|
92 |
max_new_tokens=max_new_tokens,
|
@@ -96,26 +79,15 @@ Eres un experto cocinero de la cocina hispanoamericana.<end_of_turn>
|
|
96 |
repetition_penalty=1.04, #1.1
|
97 |
do_sample=True,
|
98 |
)
|
99 |
-
outputs = model.generate(generation_config=generation_config,
|
100 |
-
input_ids=inputs,
|
101 |
-
stopping_criteria=stopping_criteria_list,)
|
102 |
return tokenizer.decode(outputs[0], skip_special_tokens=False) #True
|
103 |
|
104 |
-
|
105 |
-
|
106 |
def mostrar_respuesta(pregunta):
|
107 |
try:
|
108 |
res= generate_text(pregunta, max_length=500)
|
109 |
-
|
110 |
-
fin_json = res.rfind('}') + 1
|
111 |
-
json_str = res[inicio_json:fin_json]
|
112 |
-
json_obj = json.loads(json_str)
|
113 |
-
# print(json_obj)
|
114 |
-
return json_obj["Respuesta"]
|
115 |
except Exception as e:
|
116 |
-
|
117 |
-
json_obj['Respuesta']=str(e)
|
118 |
-
return json_obj
|
119 |
|
120 |
# Ejemplos de preguntas
|
121 |
ejemplos = [
|
@@ -127,12 +99,10 @@ ejemplos = [
|
|
127 |
iface = gr.Interface(
|
128 |
fn=mostrar_respuesta,
|
129 |
inputs=gr.Textbox(label="Pregunta"),
|
130 |
-
outputs=[
|
131 |
-
gr.Textbox(label="Respuesta", lines=2),
|
132 |
-
],
|
133 |
title="Recetas de la Abuel@",
|
134 |
description="Introduce tu pregunta sobre recetas de cocina.",
|
135 |
examples=ejemplos,
|
136 |
)
|
137 |
|
138 |
-
iface.queue(max_size=14).launch() # share=True,debug=True
|
|
|
2 |
import torch
|
3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
from peft import PeftModel
|
|
|
5 |
# !python -c "import torch; assert torch.cuda.get_device_capability()[0] >= 8, 'Hardware not supported for Flash Attention'"
|
6 |
import json
|
7 |
import torch
|
8 |
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig, GemmaTokenizer, StoppingCriteria, StoppingCriteriaList, GenerationConfig
|
|
|
9 |
import os
|
10 |
|
11 |
#sft_model = "somosnlp/gemma-FULL-RAC-Colombia_v2"
|
|
|
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(
|
18 |
load_in_4bit=True,
|
19 |
bnb_4bit_quant_type="nf4",
|
|
|
39 |
#model.to('cuda')
|
40 |
tokenizer.save_pretrained(".")
|
41 |
|
|
|
42 |
class ListOfTokensStoppingCriteria(StoppingCriteria):
|
43 |
"""
|
44 |
Clase para definir un criterio de parada basado en una lista de tokens específicos.
|
|
|
67 |
stopping_criteria_list = StoppingCriteriaList([stopping_criteria])
|
68 |
|
69 |
def generate_text(prompt, max_length=2100):
|
|
|
70 |
prompt=prompt.replace("\n", "").replace("¿","").replace("?","")
|
71 |
+
input_text = f'''<bos><start_of_turn>system You are a helpful AI assistant.Eres un experto cocinero de la cocina hispanoamericana.<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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
max_new_tokens=max_length
|
74 |
generation_config = GenerationConfig(
|
75 |
max_new_tokens=max_new_tokens,
|
|
|
79 |
repetition_penalty=1.04, #1.1
|
80 |
do_sample=True,
|
81 |
)
|
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):
|
86 |
try:
|
87 |
res= generate_text(pregunta, max_length=500)
|
88 |
+
return str(res)
|
|
|
|
|
|
|
|
|
|
|
89 |
except Exception as e:
|
90 |
+
return str(e)
|
|
|
|
|
91 |
|
92 |
# Ejemplos de preguntas
|
93 |
ejemplos = [
|
|
|
99 |
iface = gr.Interface(
|
100 |
fn=mostrar_respuesta,
|
101 |
inputs=gr.Textbox(label="Pregunta"),
|
102 |
+
outputs=[gr.Textbox(label="Respuesta", lines=2),],
|
|
|
|
|
103 |
title="Recetas de la Abuel@",
|
104 |
description="Introduce tu pregunta sobre recetas de cocina.",
|
105 |
examples=ejemplos,
|
106 |
)
|
107 |
|
108 |
+
iface.queue(max_size=14).launch() # share=True,debug=True
|