Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,57 +1,63 @@
|
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
from fastapi import FastAPI, Request
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
from threading import Thread
|
| 7 |
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
MODEL_ID,
|
| 13 |
torch_dtype=torch.bfloat16,
|
| 14 |
-
device_map="auto"
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
-
app = FastAPI()
|
| 18 |
-
|
| 19 |
@spaces.GPU(duration=120)
|
| 20 |
-
def generate_response(
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"index": 0,
|
| 46 |
-
"message": {
|
| 47 |
-
"role": "assistant",
|
| 48 |
-
"content": response_text
|
| 49 |
-
},
|
| 50 |
-
"finish_reason": "stop"
|
| 51 |
-
}]
|
| 52 |
-
}
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
import spaces
|
| 4 |
import gradio as gr
|
|
|
|
|
|
|
| 5 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 6 |
from threading import Thread
|
| 7 |
|
| 8 |
+
# Si usas la versi贸n cuantizada para ZeroGPU:
|
| 9 |
+
MODEL_ID = "unsloth/Kimi-K2.6-GGUF/UD-Q4_K_XL/Kimi-K2.6-UD-Q4_K_XL-00001-of-00014.gguf" # O un checkpoint cuantizado compatible
|
| 10 |
|
| 11 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
MODEL_ID,
|
| 14 |
torch_dtype=torch.bfloat16,
|
| 15 |
+
device_map="auto",
|
| 16 |
+
trust_remote_code=True
|
| 17 |
)
|
| 18 |
|
|
|
|
|
|
|
| 19 |
@spaces.GPU(duration=120)
|
| 20 |
+
def generate_response(message, history, system_prompt=""):
|
| 21 |
+
messages = []
|
| 22 |
+
if system_prompt:
|
| 23 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 24 |
|
| 25 |
+
for user_msg, bot_msg in history:
|
| 26 |
+
messages.append({"role": "user", "content": user_msg})
|
| 27 |
+
if bot_msg:
|
| 28 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 29 |
+
|
| 30 |
+
messages.append({"role": "user", "content": message})
|
| 31 |
|
| 32 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 33 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 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 new_token in streamer:
|
| 50 |
+
partial_text += new_token
|
| 51 |
+
yield partial_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Interfaz Gradio
|
| 54 |
+
demo = gr.ChatInterface(
|
| 55 |
+
fn=generate_response,
|
| 56 |
+
title="Kimi-K2.6 ZeroGPU Service",
|
| 57 |
+
additional_inputs=[
|
| 58 |
+
gr.Textbox("Eres un asistente experto en programaci贸n y resoluci贸n de tareas.", label="System Prompt")
|
| 59 |
+
]
|
| 60 |
+
)
|
| 61 |
|
| 62 |
+
if __name__ == "__main__":
|
| 63 |
+
demo.queue().launch()
|