Update app.py
Browse files
app.py
CHANGED
|
@@ -3,8 +3,10 @@ import sys
|
|
| 3 |
import torch
|
| 4 |
import pickle
|
| 5 |
from fastapi import FastAPI
|
|
|
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from huggingface_hub import snapshot_download
|
|
|
|
| 8 |
|
| 9 |
# ======================
|
| 10 |
# CONFIG
|
|
@@ -18,8 +20,7 @@ DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
| 18 |
repo_path = snapshot_download(
|
| 19 |
repo_id=MODEL_REPO,
|
| 20 |
repo_type="model",
|
| 21 |
-
local_dir="mtptz_repo"
|
| 22 |
-
local_dir_use_symlinks=False
|
| 23 |
)
|
| 24 |
|
| 25 |
sys.path.insert(0, repo_path)
|
|
@@ -62,9 +63,8 @@ class Prompt(BaseModel):
|
|
| 62 |
text: str
|
| 63 |
|
| 64 |
@app.post("/generate")
|
| 65 |
-
def
|
| 66 |
user_input = prompt.text.strip()
|
| 67 |
-
|
| 68 |
if not user_input:
|
| 69 |
return {"reply": ""}
|
| 70 |
|
|
@@ -87,8 +87,145 @@ def generate_api(prompt: Prompt):
|
|
| 87 |
gen_tokens = gen_tokens[:gen_tokens.index(tokenizer.eos_id())]
|
| 88 |
|
| 89 |
response = tokenizer.decode(gen_tokens).strip()
|
| 90 |
-
|
| 91 |
if "###" in response:
|
| 92 |
response = response.split("###")[0].strip()
|
| 93 |
|
| 94 |
-
return {"reply": response}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import torch
|
| 4 |
import pickle
|
| 5 |
from fastapi import FastAPI
|
| 6 |
+
from fastapi.responses import HTMLResponse, JSONResponse
|
| 7 |
from pydantic import BaseModel
|
| 8 |
from huggingface_hub import snapshot_download
|
| 9 |
+
import uvicorn
|
| 10 |
|
| 11 |
# ======================
|
| 12 |
# CONFIG
|
|
|
|
| 20 |
repo_path = snapshot_download(
|
| 21 |
repo_id=MODEL_REPO,
|
| 22 |
repo_type="model",
|
| 23 |
+
local_dir="mtptz_repo"
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
sys.path.insert(0, repo_path)
|
|
|
|
| 63 |
text: str
|
| 64 |
|
| 65 |
@app.post("/generate")
|
| 66 |
+
def generate(prompt: Prompt):
|
| 67 |
user_input = prompt.text.strip()
|
|
|
|
| 68 |
if not user_input:
|
| 69 |
return {"reply": ""}
|
| 70 |
|
|
|
|
| 87 |
gen_tokens = gen_tokens[:gen_tokens.index(tokenizer.eos_id())]
|
| 88 |
|
| 89 |
response = tokenizer.decode(gen_tokens).strip()
|
|
|
|
| 90 |
if "###" in response:
|
| 91 |
response = response.split("###")[0].strip()
|
| 92 |
|
| 93 |
+
return {"reply": response}
|
| 94 |
+
|
| 95 |
+
# ======================
|
| 96 |
+
# CHAT WEB (HTML)
|
| 97 |
+
# ======================
|
| 98 |
+
@app.get("/", response_class=HTMLResponse)
|
| 99 |
+
def chat_ui():
|
| 100 |
+
return """
|
| 101 |
+
<!DOCTYPE html>
|
| 102 |
+
<html lang="es">
|
| 103 |
+
<head>
|
| 104 |
+
<meta charset="UTF-8">
|
| 105 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 106 |
+
<title>MTP Chat</title>
|
| 107 |
+
<style>
|
| 108 |
+
body {
|
| 109 |
+
margin: 0;
|
| 110 |
+
background: #343541;
|
| 111 |
+
font-family: system-ui, sans-serif;
|
| 112 |
+
display: flex;
|
| 113 |
+
justify-content: center;
|
| 114 |
+
height: 100vh;
|
| 115 |
+
}
|
| 116 |
+
.chat {
|
| 117 |
+
width: 100%;
|
| 118 |
+
max-width: 420px;
|
| 119 |
+
background: #40414f;
|
| 120 |
+
display: flex;
|
| 121 |
+
flex-direction: column;
|
| 122 |
+
}
|
| 123 |
+
header {
|
| 124 |
+
background: #202123;
|
| 125 |
+
color: white;
|
| 126 |
+
padding: 14px;
|
| 127 |
+
font-weight: bold;
|
| 128 |
+
}
|
| 129 |
+
.messages {
|
| 130 |
+
flex: 1;
|
| 131 |
+
padding: 14px;
|
| 132 |
+
overflow-y: auto;
|
| 133 |
+
}
|
| 134 |
+
.msg {
|
| 135 |
+
max-width: 85%;
|
| 136 |
+
padding: 12px 14px;
|
| 137 |
+
margin-bottom: 10px;
|
| 138 |
+
border-radius: 10px;
|
| 139 |
+
white-space: pre-wrap;
|
| 140 |
+
}
|
| 141 |
+
.user {
|
| 142 |
+
background: #0b93f6;
|
| 143 |
+
color: white;
|
| 144 |
+
margin-left: auto;
|
| 145 |
+
}
|
| 146 |
+
.bot {
|
| 147 |
+
background: #444654;
|
| 148 |
+
color: white;
|
| 149 |
+
}
|
| 150 |
+
.input {
|
| 151 |
+
display: flex;
|
| 152 |
+
padding: 10px;
|
| 153 |
+
background: #202123;
|
| 154 |
+
}
|
| 155 |
+
.input input {
|
| 156 |
+
flex: 1;
|
| 157 |
+
border: none;
|
| 158 |
+
padding: 12px;
|
| 159 |
+
border-radius: 8px;
|
| 160 |
+
font-size: 16px;
|
| 161 |
+
}
|
| 162 |
+
.input button {
|
| 163 |
+
background: #0b93f6;
|
| 164 |
+
border: none;
|
| 165 |
+
color: white;
|
| 166 |
+
padding: 0 16px;
|
| 167 |
+
border-radius: 8px;
|
| 168 |
+
margin-left: 8px;
|
| 169 |
+
}
|
| 170 |
+
</style>
|
| 171 |
+
</head>
|
| 172 |
+
<body>
|
| 173 |
+
|
| 174 |
+
<div class="chat">
|
| 175 |
+
<header>🤖 MTP</header>
|
| 176 |
+
<div id="messages" class="messages"></div>
|
| 177 |
+
<div class="input">
|
| 178 |
+
<input id="input" placeholder="Escribe un mensaje..." autocomplete="off">
|
| 179 |
+
<button onclick="send()">Enviar</button>
|
| 180 |
+
</div>
|
| 181 |
+
</div>
|
| 182 |
+
|
| 183 |
+
<script>
|
| 184 |
+
const messages = document.getElementById("messages");
|
| 185 |
+
const input = document.getElementById("input");
|
| 186 |
+
|
| 187 |
+
function add(text, cls) {
|
| 188 |
+
const div = document.createElement("div");
|
| 189 |
+
div.className = "msg " + cls;
|
| 190 |
+
div.textContent = text;
|
| 191 |
+
messages.appendChild(div);
|
| 192 |
+
messages.scrollTop = messages.scrollHeight;
|
| 193 |
+
}
|
| 194 |
+
|
| 195 |
+
async function send() {
|
| 196 |
+
const text = input.value.trim();
|
| 197 |
+
if (!text) return;
|
| 198 |
+
|
| 199 |
+
add(text, "user");
|
| 200 |
+
input.value = "";
|
| 201 |
+
|
| 202 |
+
const typing = document.createElement("div");
|
| 203 |
+
typing.className = "msg bot";
|
| 204 |
+
typing.textContent = "MTP está escribiendo…";
|
| 205 |
+
messages.appendChild(typing);
|
| 206 |
+
|
| 207 |
+
const res = await fetch("/generate", {
|
| 208 |
+
method: "POST",
|
| 209 |
+
headers: { "Content-Type": "application/json" },
|
| 210 |
+
body: JSON.stringify({ text })
|
| 211 |
+
});
|
| 212 |
+
|
| 213 |
+
const json = await res.json();
|
| 214 |
+
messages.removeChild(typing);
|
| 215 |
+
add(json.reply || "Sin respuesta", "bot");
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
input.addEventListener("keydown", e => {
|
| 219 |
+
if (e.key === "Enter") send();
|
| 220 |
+
});
|
| 221 |
+
</script>
|
| 222 |
+
|
| 223 |
+
</body>
|
| 224 |
+
</html>
|
| 225 |
+
"""
|
| 226 |
+
|
| 227 |
+
# ======================
|
| 228 |
+
# ENTRYPOINT (HF)
|
| 229 |
+
# ======================
|
| 230 |
+
if __name__ == "__main__":
|
| 231 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|