Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import hf_hub_download
|
| 3 |
from llama_cpp import Llama
|
| 4 |
-
import traceback
|
| 5 |
|
| 6 |
-
# ⚡
|
| 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,
|
| 14 |
-
n_threads=2,
|
| 15 |
n_batch=128,
|
| 16 |
use_mmap=True,
|
| 17 |
verbose=False
|
|
@@ -24,7 +23,6 @@ SYSTEM_PROMPT = (
|
|
| 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"
|
|
@@ -34,10 +32,8 @@ def format_prompt(message, history):
|
|
| 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,
|
|
@@ -52,47 +48,41 @@ def generate_response(message, history):
|
|
| 52 |
partial += token
|
| 53 |
yield partial
|
| 54 |
except Exception as e:
|
| 55 |
-
|
| 56 |
-
yield f"❌ Error interno: {traceback.format_exc()}"
|
| 57 |
|
| 58 |
-
# 🎨 Interfaz estilo
|
| 59 |
custom_css = """
|
|
|
|
| 60 |
.gradio-container {
|
| 61 |
max-width: 800px;
|
| 62 |
margin: auto;
|
| 63 |
font-family: 'Inter', system-ui, sans-serif;
|
| 64 |
}
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
max-width: 80%;
|
| 70 |
-
line-height: 1.5;
|
| 71 |
}
|
| 72 |
-
.
|
| 73 |
-
background: #
|
| 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 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
)
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
| 98 |
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 |
|
| 5 |
+
# ⚡ Configuración del modelo
|
| 6 |
REPO_ID = "unsloth/Qwen2.5-Coder-0.5B-Instruct-GGUF"
|
| 7 |
FILENAME = "Qwen2.5-Coder-0.5B-Instruct-Q4_K_M.gguf"
|
| 8 |
model_path = hf_hub_download(repo_id=REPO_ID, filename=FILENAME)
|
| 9 |
|
| 10 |
llm = Llama(
|
| 11 |
model_path=model_path,
|
| 12 |
+
n_ctx=1024,
|
| 13 |
+
n_threads=2,
|
| 14 |
n_batch=128,
|
| 15 |
use_mmap=True,
|
| 16 |
verbose=False
|
|
|
|
| 23 |
)
|
| 24 |
|
| 25 |
def format_prompt(message, history):
|
|
|
|
| 26 |
prompt = f"<|im_start|>system\n{SYSTEM_PROMPT}<|im_end|>\n"
|
| 27 |
for user_msg, bot_msg in history:
|
| 28 |
prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
|
|
|
|
| 32 |
return prompt
|
| 33 |
|
| 34 |
def generate_response(message, history):
|
|
|
|
| 35 |
prompt = format_prompt(message, history)
|
| 36 |
try:
|
|
|
|
| 37 |
stream = llm(
|
| 38 |
prompt,
|
| 39 |
max_tokens=256,
|
|
|
|
| 48 |
partial += token
|
| 49 |
yield partial
|
| 50 |
except Exception as e:
|
| 51 |
+
yield f"❌ Error: {e}"
|
|
|
|
| 52 |
|
| 53 |
+
# 🎨 Interfaz moderna estilo DeepSeek / ChatGPT
|
| 54 |
custom_css = """
|
| 55 |
+
/* Centrar y limitar el ancho */
|
| 56 |
.gradio-container {
|
| 57 |
max-width: 800px;
|
| 58 |
margin: auto;
|
| 59 |
font-family: 'Inter', system-ui, sans-serif;
|
| 60 |
}
|
| 61 |
+
/* Estilo de los mensajes del chat (lo maneja el componente Chatbot) */
|
| 62 |
+
.message.user {
|
| 63 |
+
background-color: #f0f0f0 !important;
|
| 64 |
+
color: #111 !important;
|
|
|
|
|
|
|
| 65 |
}
|
| 66 |
+
.message.bot {
|
| 67 |
+
background-color: #e3f2fd !important;
|
| 68 |
+
color: #111 !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
}
|
| 70 |
footer {visibility: hidden} /* Oculta el "Flag" de Hugging Face */
|
| 71 |
"""
|
| 72 |
|
| 73 |
+
# Construcción con Blocks (necesario para usar theme en Gradio 6)
|
| 74 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=custom_css, title="TixAI — Roblox Expert") as iface:
|
| 75 |
+
gr.Markdown("# 🤖 TixAI — Roblox Expert")
|
| 76 |
+
gr.Markdown("Tu asistente definitivo para scripting, construcción y marketing en Roblox.")
|
| 77 |
+
chat = gr.ChatInterface(
|
| 78 |
+
fn=generate_response,
|
| 79 |
+
examples=[
|
| 80 |
+
"¿Cómo crear un sistema de mascotas que sigan al jugador?",
|
| 81 |
+
"Dame un script para un obby con checkpoints.",
|
| 82 |
+
"¿Qué gamepasses me recomiendas para monetizar un simulador?"
|
| 83 |
+
],
|
| 84 |
+
cache_examples=False,
|
| 85 |
+
)
|
| 86 |
|
| 87 |
if __name__ == "__main__":
|
| 88 |
iface.launch(server_name="0.0.0.0")
|