teszenofficial commited on
Commit
a4f978d
·
verified ·
1 Parent(s): 4da8393

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -48
app.py CHANGED
@@ -6,14 +6,13 @@ import gradio as gr
6
  from huggingface_hub import snapshot_download
7
 
8
  # ======================
9
- # CONFIGURACIÓN
10
  # ======================
11
  MODEL_REPO = "teszenofficial/mtptz"
12
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
13
- MAX_NEW_TOKENS = 80
14
 
15
  # ======================
16
- # DESCARGA DEL MODELO
17
  # ======================
18
  repo_path = snapshot_download(
19
  repo_id=MODEL_REPO,
@@ -28,92 +27,77 @@ from model import MTPMiniModel
28
  from tokenizer import MTPTokenizer
29
 
30
  # ======================
31
- # CARGA DE PESOS
32
  # ======================
33
  with open(os.path.join(repo_path, "mtp_mini.pkl"), "rb") as f:
34
- data = pickle.load(f)
35
 
36
  tokenizer = MTPTokenizer(
37
  os.path.join(repo_path, "mtp_tokenizer.model")
38
  )
39
 
40
- cfg = data["config"]
41
 
42
  model = MTPMiniModel(
43
- vocab_size=data["vocab_size"],
44
- d_model=cfg["model"]["d_model"],
45
- n_layers=cfg["model"]["n_layers"],
46
- n_heads=cfg["model"]["n_heads"],
47
- d_ff=cfg["model"]["d_ff"],
48
- max_seq_len=cfg["model"]["max_seq_len"],
49
  dropout=0.0
50
  )
51
 
52
- model.load_state_dict(data["model_state_dict"])
53
  model.to(DEVICE)
54
  model.eval()
55
 
56
  # ======================
57
- # FUNCIÓN DE GENERACIÓN
58
  # ======================
59
  def generate(user_input, history):
60
- # Prompt simple y guiado (CLAVE)
61
- prompt = (
62
- "Instrucción:\n"
63
- f"{user_input.strip()}\n\n"
64
- "Respuesta clara y correcta:\n"
65
- )
66
 
67
- input_ids = [tokenizer.bos_id()] + tokenizer.encode(prompt)
68
- x = torch.tensor([input_ids], device=DEVICE)
69
 
70
  with torch.no_grad():
71
- output = model.generate(
72
- x,
73
- max_new_tokens=MAX_NEW_TOKENS,
74
- temperature=0.45,
75
- top_k=40,
76
- top_p=0.9,
77
- repetition_penalty=1.15
78
  )
79
 
80
- gen_tokens = output[0, len(input_ids):].tolist()
81
 
82
  if tokenizer.eos_id() in gen_tokens:
83
  gen_tokens = gen_tokens[:gen_tokens.index(tokenizer.eos_id())]
84
 
85
  response = tokenizer.decode(gen_tokens).strip()
86
 
 
 
 
87
  history = history + [(user_input, response)]
88
  return history, history
89
 
90
  # ======================
91
- # INTERFAZ GRADIO
92
  # ======================
93
  with gr.Blocks(css="footer{display:none}") as demo:
94
- gr.Markdown("# 🤖 MTP-1")
95
- gr.Markdown(
96
- "Modelo de lenguaje pequeño y experimental. "
97
- "Optimizado para respuestas claras y directas."
98
- )
99
 
100
  chatbot = gr.Chatbot()
101
  state = gr.State([])
102
 
103
- user_box = gr.Textbox(
104
- placeholder="Escribe una pregunta sencilla…",
105
  show_label=False
106
  )
107
 
108
- user_box.submit(
109
- generate,
110
- inputs=[user_box, state],
111
- outputs=[chatbot, state]
112
- )
113
-
114
- gr.Markdown(
115
- "⚠️ MTP-1 es un modelo pequeño. "
116
- "Las preguntas claras producen mejores respuestas."
117
- )
118
 
119
  demo.launch()
 
6
  from huggingface_hub import snapshot_download
7
 
8
  # ======================
9
+ # CONFIG
10
  # ======================
11
  MODEL_REPO = "teszenofficial/mtptz"
12
  DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
 
13
 
14
  # ======================
15
+ # DOWNLOAD MODEL
16
  # ======================
17
  repo_path = snapshot_download(
18
  repo_id=MODEL_REPO,
 
27
  from tokenizer import MTPTokenizer
28
 
29
  # ======================
30
+ # LOAD MODEL DATA
31
  # ======================
32
  with open(os.path.join(repo_path, "mtp_mini.pkl"), "rb") as f:
33
+ model_data = pickle.load(f)
34
 
35
  tokenizer = MTPTokenizer(
36
  os.path.join(repo_path, "mtp_tokenizer.model")
37
  )
38
 
39
+ config = model_data["config"]
40
 
41
  model = MTPMiniModel(
42
+ vocab_size=model_data["vocab_size"],
43
+ d_model=config["model"]["d_model"],
44
+ n_layers=config["model"]["n_layers"],
45
+ n_heads=config["model"]["n_heads"],
46
+ d_ff=config["model"]["d_ff"],
47
+ max_seq_len=config["model"]["max_seq_len"],
48
  dropout=0.0
49
  )
50
 
51
+ model.load_state_dict(model_data["model_state_dict"])
52
  model.to(DEVICE)
53
  model.eval()
54
 
55
  # ======================
56
+ # GENERATION (MISMA LÓGICA QUE chat.py)
57
  # ======================
58
  def generate(user_input, history):
59
+ prompt = f"### Instrucción:\n{user_input}\n\n### Respuesta:\n"
 
 
 
 
 
60
 
61
+ tokens = [tokenizer.bos_id()] + tokenizer.encode(prompt)
62
+ input_ids = torch.tensor([tokens], device=DEVICE)
63
 
64
  with torch.no_grad():
65
+ output_ids = model.generate(
66
+ input_ids,
67
+ max_new_tokens=100,
68
+ temperature=0.8,
69
+ top_k=50,
70
+ top_p=0.9
 
71
  )
72
 
73
+ gen_tokens = output_ids[0, len(tokens):].tolist()
74
 
75
  if tokenizer.eos_id() in gen_tokens:
76
  gen_tokens = gen_tokens[:gen_tokens.index(tokenizer.eos_id())]
77
 
78
  response = tokenizer.decode(gen_tokens).strip()
79
 
80
+ if "###" in response:
81
+ response = response.split("###")[0].strip()
82
+
83
  history = history + [(user_input, response)]
84
  return history, history
85
 
86
  # ======================
87
+ # GRADIO UI
88
  # ======================
89
  with gr.Blocks(css="footer{display:none}") as demo:
90
+ gr.Markdown("# 🤖 MTP Mini")
91
+ gr.Markdown("Modelo de lenguaje experimental")
 
 
 
92
 
93
  chatbot = gr.Chatbot()
94
  state = gr.State([])
95
 
96
+ textbox = gr.Textbox(
97
+ placeholder="Escribe una pregunta…",
98
  show_label=False
99
  )
100
 
101
+ textbox.submit(generate, [textbox, state], [chatbot, state])
 
 
 
 
 
 
 
 
 
102
 
103
  demo.launch()