teszenofficial commited on
Commit
2de8f5b
Β·
verified Β·
1 Parent(s): 3f489ed

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -121
app.py DELETED
@@ -1,121 +0,0 @@
1
- import os
2
- import sys
3
- import torch
4
- import pickle
5
- import gradio as gr
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,
19
- repo_type="model",
20
- local_dir="mtptz_repo",
21
- local_dir_use_symlinks=False
22
- )
23
-
24
- sys.path.insert(0, repo_path)
25
-
26
- from model import MTPMiniModel
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 FUNCTION
57
- # ======================
58
- def generate(user_input, history):
59
- if history is None:
60
- history = []
61
-
62
- prompt = f"### InstrucciΓ³n:\n{user_input}\n\n### Respuesta:\n"
63
-
64
- tokens = [tokenizer.bos_id()] + tokenizer.encode(prompt)
65
- input_ids = torch.tensor([tokens], device=DEVICE)
66
-
67
- with torch.no_grad():
68
- output_ids = model.generate(
69
- input_ids,
70
- max_new_tokens=120,
71
- temperature=0.8,
72
- top_k=50,
73
- top_p=0.9
74
- )
75
-
76
- gen_tokens = output_ids[0, len(tokens):].tolist()
77
-
78
- if tokenizer.eos_id() in gen_tokens:
79
- gen_tokens = gen_tokens[:gen_tokens.index(tokenizer.eos_id())]
80
-
81
- response = tokenizer.decode(gen_tokens).strip()
82
-
83
- if "###" in response:
84
- response = response.split("###")[0].strip()
85
-
86
- history = history + [(user_input, response)]
87
- return history, history
88
-
89
- # ======================
90
- # GRADIO UI
91
- # ======================
92
- with gr.Blocks(
93
- css="footer{display:none}",
94
- title="MTP Chatbot"
95
- ) as demo:
96
-
97
- gr.Markdown("# πŸ€– MTP 1.1")
98
- gr.Markdown("Modelo de lenguaje experimental")
99
-
100
- chatbot = gr.Chatbot(height=450)
101
- state = gr.State([])
102
-
103
- textbox = gr.Textbox(
104
- placeholder="Escribe una pregunta…",
105
- show_label=False
106
- )
107
-
108
- textbox.submit(
109
- fn=generate,
110
- inputs=[textbox, state],
111
- outputs=[chatbot, state]
112
- )
113
-
114
- # ======================
115
- # LAUNCH
116
- # ======================
117
- demo.launch(
118
- server_name="0.0.0.0",
119
- server_port=7860,
120
- show_api=True
121
- )