Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,27 @@
|
|
| 1 |
import os
|
| 2 |
-
import torch
|
| 3 |
import spaces
|
| 4 |
import gradio as gr
|
| 5 |
-
from
|
| 6 |
-
from
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
@spaces.GPU(duration=120)
|
|
@@ -28,34 +36,27 @@ def generate_response(message, history, system_prompt=""):
|
|
| 28 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 29 |
|
| 30 |
messages.append({"role": "user", "content": message})
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 36 |
-
generate_kwargs = dict(
|
| 37 |
-
model_inputs,
|
| 38 |
-
streamer=streamer,
|
| 39 |
-
max_new_tokens=2048,
|
| 40 |
-
do_sample=True,
|
| 41 |
temperature=0.6,
|
| 42 |
top_p=0.9,
|
|
|
|
| 43 |
)
|
| 44 |
-
|
| 45 |
-
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 46 |
-
thread.start()
|
| 47 |
-
|
| 48 |
partial_text = ""
|
| 49 |
-
for
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
|
| 53 |
-
# Interfaz Gradio
|
| 54 |
demo = gr.ChatInterface(
|
| 55 |
fn=generate_response,
|
| 56 |
-
title="Kimi-K2.6
|
| 57 |
additional_inputs=[
|
| 58 |
-
gr.Textbox("Eres un asistente experto en programaci贸n
|
| 59 |
]
|
| 60 |
)
|
| 61 |
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
+
from huggingface_hub import hf_hub_download
|
| 5 |
+
from llama_cpp import Llama
|
| 6 |
+
|
| 7 |
+
REPO_ID = "unsloth/Kimi-K2.6-GGUF"
|
| 8 |
+
# Nota: Si el modelo est谩 shardeado en varias partes (-00001-of-00014),
|
| 9 |
+
# llama.cpp requiere que descargues la primera parte y autom谩ticamente detecta el resto,
|
| 10 |
+
# o debes usar un archivo 煤nico cuantizado menor (p. ej. Q2_K o un modelo de menor tama帽o).
|
| 11 |
+
FILENAME = "UD-Q4_K_XL/Kimi-K2.6-UD-Q4_K_XL-00001-of-00014.gguf"
|
| 12 |
+
|
| 13 |
+
# Descargar archivo del Hub
|
| 14 |
+
model_path = hf_hub_download(
|
| 15 |
+
repo_id=REPO_ID,
|
| 16 |
+
filename=FILENAME
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
# Inicializar motor GGUF con soporte GPU
|
| 20 |
+
llm = Llama(
|
| 21 |
+
model_path=model_path,
|
| 22 |
+
n_gpu_layers=-1, # Enviar capas a la GPU
|
| 23 |
+
n_ctx=4096,
|
| 24 |
+
verbose=False
|
| 25 |
)
|
| 26 |
|
| 27 |
@spaces.GPU(duration=120)
|
|
|
|
| 36 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 37 |
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
+
|
| 40 |
+
response_stream = llm.create_chat_completion(
|
| 41 |
+
messages=messages,
|
| 42 |
+
max_tokens=2048,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
temperature=0.6,
|
| 44 |
top_p=0.9,
|
| 45 |
+
stream=True
|
| 46 |
)
|
| 47 |
+
|
|
|
|
|
|
|
|
|
|
| 48 |
partial_text = ""
|
| 49 |
+
for chunk in response_stream:
|
| 50 |
+
delta = chunk["choices"][0]["delta"]
|
| 51 |
+
if "content" in delta:
|
| 52 |
+
partial_text += delta["content"]
|
| 53 |
+
yield partial_text
|
| 54 |
|
|
|
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
fn=generate_response,
|
| 57 |
+
title="Kimi-K2.6 Inference",
|
| 58 |
additional_inputs=[
|
| 59 |
+
gr.Textbox("Eres un asistente experto en programaci贸n.", label="System Prompt")
|
| 60 |
]
|
| 61 |
)
|
| 62 |
|