Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,49 +1,98 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
|
|
|
| 4 |
|
| 5 |
-
# ⚡
|
| 6 |
REPO_ID = "unsloth/Qwen2.5-Coder-0.5B-Instruct-GGUF"
|
| 7 |
FILENAME = "Qwen2.5-Coder-0.5B-Instruct-Q4_K_M.gguf"
|
| 8 |
-
|
| 9 |
-
# Descarga el modelo (cacheado después del primer uso)
|
| 10 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 11 |
|
| 12 |
-
# Carga el modelo con parámetros optimizados
|
| 13 |
llm = Llama(
|
| 14 |
model_path=model_path,
|
| 15 |
-
n_ctx=1024,
|
| 16 |
-
n_threads=
|
| 17 |
-
n_batch=
|
| 18 |
use_mmap=True,
|
| 19 |
verbose=False
|
| 20 |
)
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
"""
|
| 30 |
-
|
| 31 |
-
prompt,
|
| 32 |
-
max_tokens=128,
|
| 33 |
-
temperature=0.6,
|
| 34 |
-
top_p=0.9,
|
| 35 |
-
stop=["<|im_end|>", "<|im_start|>"],
|
| 36 |
-
echo=False
|
| 37 |
-
)
|
| 38 |
-
return output["choices"][0]["text"].strip()
|
| 39 |
-
|
| 40 |
-
# Interfaz de chat
|
| 41 |
iface = gr.ChatInterface(
|
| 42 |
-
fn=
|
| 43 |
-
title="
|
| 44 |
-
description="
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
| 48 |
-
|
| 49 |
-
iface.launch(server_name="0.0.0.0")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
+
import traceback
|
| 5 |
|
| 6 |
+
# ⚡ Descarga y carga del modelo (solo una vez, se cachea)
|
| 7 |
REPO_ID = "unsloth/Qwen2.5-Coder-0.5B-Instruct-GGUF"
|
| 8 |
FILENAME = "Qwen2.5-Coder-0.5B-Instruct-Q4_K_M.gguf"
|
|
|
|
|
|
|
| 9 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 10 |
|
|
|
|
| 11 |
llm = Llama(
|
| 12 |
model_path=model_path,
|
| 13 |
+
n_ctx=1024, # Contexto suficiente para respuestas
|
| 14 |
+
n_threads=2, # Usa ambos núcleos virtuales
|
| 15 |
+
n_batch=128,
|
| 16 |
use_mmap=True,
|
| 17 |
verbose=False
|
| 18 |
)
|
| 19 |
|
| 20 |
+
SYSTEM_PROMPT = (
|
| 21 |
+
"Eres TixAI, un asistente de IA superinteligente especializado en Roblox, scripting Luau, "
|
| 22 |
+
"construcción de juegos y marketing. Responde SIEMPRE en español, de forma concisa y útil. "
|
| 23 |
+
"Incluye ejemplos de código cuando sea relevante."
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
def format_prompt(message, history):
|
| 27 |
+
"""Construye el prompt con el formato de chat de Qwen2.5."""
|
| 28 |
+
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 29 |
+
for user_msg, bot_msg in history:
|
| 30 |
+
prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
|
| 31 |
+
prompt += f"<|im_start|>assistant\n{bot_msg}<|im_end|>\n"
|
| 32 |
+
prompt += f"<|im_start|>user\n{message}<|im_end|>\n"
|
| 33 |
+
prompt += "<|im_start|>assistant\n"
|
| 34 |
+
return prompt
|
| 35 |
+
|
| 36 |
+
def generate_response(message, history):
|
| 37 |
+
"""Genera la respuesta token a token (streaming)."""
|
| 38 |
+
prompt = format_prompt(message, history)
|
| 39 |
+
try:
|
| 40 |
+
# Streaming: devolvemos cada token según se genera
|
| 41 |
+
stream = llm(
|
| 42 |
+
prompt,
|
| 43 |
+
max_tokens=256,
|
| 44 |
+
temperature=0.6,
|
| 45 |
+
top_p=0.9,
|
| 46 |
+
stop=["<|im_end|>", "<|im_start|>"],
|
| 47 |
+
stream=True,
|
| 48 |
+
)
|
| 49 |
+
partial = ""
|
| 50 |
+
for chunk in stream:
|
| 51 |
+
token = chunk["choices"][0]["text"]
|
| 52 |
+
partial += token
|
| 53 |
+
yield partial
|
| 54 |
+
except Exception as e:
|
| 55 |
+
# En caso de error, lo mostramos en el chat
|
| 56 |
+
yield f"❌ Error interno: {traceback.format_exc()}"
|
| 57 |
+
|
| 58 |
+
# 🎨 Interfaz estilo ChatGPT / DeepSeek
|
| 59 |
+
custom_css = """
|
| 60 |
+
.gradio-container {
|
| 61 |
+
max-width: 800px;
|
| 62 |
+
margin: auto;
|
| 63 |
+
font-family: 'Inter', system-ui, sans-serif;
|
| 64 |
+
}
|
| 65 |
+
.bubble {
|
| 66 |
+
border-radius: 18px;
|
| 67 |
+
padding: 12px 18px;
|
| 68 |
+
margin: 8px 0;
|
| 69 |
+
max-width: 80%;
|
| 70 |
+
line-height: 1.5;
|
| 71 |
+
}
|
| 72 |
+
.user {
|
| 73 |
+
background: #f0f0f0;
|
| 74 |
+
color: #111;
|
| 75 |
+
}
|
| 76 |
+
.bot {
|
| 77 |
+
background: #e3f2fd;
|
| 78 |
+
color: #111;
|
| 79 |
+
}
|
| 80 |
+
footer {visibility: hidden} /* Oculta el "Flag" de Hugging Face */
|
| 81 |
"""
|
| 82 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
iface = gr.ChatInterface(
|
| 84 |
+
fn=generate_response,
|
| 85 |
+
title="🤖 TixAI — Roblox Expert",
|
| 86 |
+
description="Tu asistente definitivo para scripting, construcción y marketing en Roblox.",
|
| 87 |
+
theme=gr.themes.Soft(),
|
| 88 |
+
css=custom_css,
|
| 89 |
+
examples=[
|
| 90 |
+
"¿Cómo crear un sistema de mascotas que sigan al jugador?",
|
| 91 |
+
"Dame un script para un obby con checkpoints.",
|
| 92 |
+
"¿Qué gamepasses me recomiendas para monetizar un simulador?"
|
| 93 |
+
],
|
| 94 |
+
cache_examples=False,
|
| 95 |
)
|
| 96 |
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
iface.launch(server_name="0.0.0.0")
|