Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,31 +1,21 @@
|
|
| 1 |
-
import os
|
| 2 |
import spaces
|
| 3 |
import gradio as gr
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 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)
|
| 28 |
-
def
|
| 29 |
messages = []
|
| 30 |
if system_prompt:
|
| 31 |
messages.append({"role": "system", "content": system_prompt})
|
|
@@ -36,27 +26,36 @@ def generate_response(message, history, system_prompt=""):
|
|
| 36 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 37 |
|
| 38 |
messages.append({"role": "user", "content": message})
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
messages
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
)
|
| 47 |
|
|
|
|
|
|
|
|
|
|
| 48 |
partial_text = ""
|
| 49 |
-
for
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
partial_text += delta["content"]
|
| 53 |
-
yield partial_text
|
| 54 |
|
| 55 |
demo = gr.ChatInterface(
|
| 56 |
-
fn=
|
| 57 |
-
title="
|
| 58 |
additional_inputs=[
|
| 59 |
-
gr.Textbox("Eres un asistente experto en programaci贸n.", label="System Prompt")
|
| 60 |
]
|
| 61 |
)
|
| 62 |
|
|
|
|
|
|
|
| 1 |
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 5 |
+
from threading import Thread
|
| 6 |
+
|
| 7 |
+
MODEL_ID = "deepseek-ai/DeepSeek-Coder-V2-Lite-Instruct"
|
| 8 |
+
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
MODEL_ID,
|
| 12 |
+
torch_dtype=torch.bfloat16,
|
| 13 |
+
device_map="auto",
|
| 14 |
+
trust_remote_code=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
)
|
| 16 |
|
| 17 |
@spaces.GPU(duration=120)
|
| 18 |
+
def generate(message, history, system_prompt=""):
|
| 19 |
messages = []
|
| 20 |
if system_prompt:
|
| 21 |
messages.append({"role": "system", "content": system_prompt})
|
|
|
|
| 26 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 27 |
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
+
|
| 30 |
+
inputs = tokenizer.apply_chat_template(
|
| 31 |
+
messages,
|
| 32 |
+
tokenize=True,
|
| 33 |
+
add_generation_prompt=True,
|
| 34 |
+
return_tensors="pt"
|
| 35 |
+
).to(model.device)
|
| 36 |
+
|
| 37 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 38 |
+
generate_kwargs = dict(
|
| 39 |
+
inputs=inputs,
|
| 40 |
+
streamer=streamer,
|
| 41 |
+
max_new_tokens=2048,
|
| 42 |
+
temperature=0.3,
|
| 43 |
+
top_p=0.95
|
| 44 |
)
|
| 45 |
|
| 46 |
+
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 47 |
+
thread.start()
|
| 48 |
+
|
| 49 |
partial_text = ""
|
| 50 |
+
for new_token in streamer:
|
| 51 |
+
partial_text += new_token
|
| 52 |
+
yield partial_text
|
|
|
|
|
|
|
| 53 |
|
| 54 |
demo = gr.ChatInterface(
|
| 55 |
+
fn=generate,
|
| 56 |
+
title="DeepSeek Coder V2 Lite (ZeroGPU)",
|
| 57 |
additional_inputs=[
|
| 58 |
+
gr.Textbox("Eres un asistente experto en programaci贸n y resoluci贸n de problemas.", label="System Prompt")
|
| 59 |
]
|
| 60 |
)
|
| 61 |
|