Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,61 @@
|
|
|
|
|
|
|
|
| 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 = "
|
| 8 |
|
|
|
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
|
|
|
|
|
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
MODEL_ID,
|
| 12 |
-
torch_dtype=torch.
|
| 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})
|
| 22 |
-
|
| 23 |
for user_msg, bot_msg in history:
|
| 24 |
messages.append({"role": "user", "content": user_msg})
|
| 25 |
if bot_msg:
|
| 26 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 27 |
-
|
| 28 |
messages.append({"role": "user", "content": message})
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
messages,
|
| 32 |
-
tokenize=
|
| 33 |
-
add_generation_prompt=True
|
| 34 |
-
|
| 35 |
-
).to(model.device)
|
| 36 |
-
|
| 37 |
-
streamer = TextIteratorStreamer(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
generate_kwargs = dict(
|
| 39 |
-
|
| 40 |
streamer=streamer,
|
| 41 |
-
max_new_tokens=
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
| 44 |
)
|
| 45 |
|
|
|
|
| 46 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 47 |
thread.start()
|
| 48 |
|
|
@@ -51,13 +64,42 @@ def generate(message, history, system_prompt=""):
|
|
| 51 |
partial_text += new_token
|
| 52 |
yield partial_text
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
demo.queue().launch()
|
|
|
|
| 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 |
+
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct-AWQ"
|
| 9 |
|
| 10 |
+
# Cargar tokenizer
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 12 |
+
|
| 13 |
+
# Cargar modelo cuantizado en AWQ (ocupa ~18-20GB VRAM)
|
| 14 |
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
MODEL_ID,
|
| 16 |
+
torch_dtype=torch.float16,
|
| 17 |
device_map="auto",
|
| 18 |
trust_remote_code=True
|
| 19 |
)
|
| 20 |
|
| 21 |
@spaces.GPU(duration=120)
|
| 22 |
+
def generate(message, history, system_prompt, temperature, top_p, max_tokens):
|
| 23 |
messages = []
|
| 24 |
if system_prompt:
|
| 25 |
messages.append({"role": "system", "content": system_prompt})
|
| 26 |
+
|
| 27 |
for user_msg, bot_msg in history:
|
| 28 |
messages.append({"role": "user", "content": user_msg})
|
| 29 |
if bot_msg:
|
| 30 |
messages.append({"role": "assistant", "content": bot_msg})
|
| 31 |
+
|
| 32 |
messages.append({"role": "user", "content": message})
|
| 33 |
+
|
| 34 |
+
text = tokenizer.apply_chat_template(
|
| 35 |
+
messages,
|
| 36 |
+
tokenize=False,
|
| 37 |
+
add_generation_prompt=True
|
| 38 |
+
)
|
| 39 |
+
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
|
| 40 |
+
|
| 41 |
+
streamer = TextIteratorStreamer(
|
| 42 |
+
tokenizer,
|
| 43 |
+
timeout=60.0,
|
| 44 |
+
skip_prompt=True,
|
| 45 |
+
skip_special_tokens=True
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
generate_kwargs = dict(
|
| 49 |
+
model_inputs,
|
| 50 |
streamer=streamer,
|
| 51 |
+
max_new_tokens=int(max_tokens),
|
| 52 |
+
do_sample=temperature > 0.0,
|
| 53 |
+
temperature=max(temperature, 1e-2) if temperature > 0.0 else None,
|
| 54 |
+
top_p=top_p if temperature > 0.0 else None,
|
| 55 |
+
repetition_penalty=1.05
|
| 56 |
)
|
| 57 |
|
| 58 |
+
# Iniciar generaci贸n en un hilo secundario para streaming fluido
|
| 59 |
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 60 |
thread.start()
|
| 61 |
|
|
|
|
| 64 |
partial_text += new_token
|
| 65 |
yield partial_text
|
| 66 |
|
| 67 |
+
# Interfaz de Gradio
|
| 68 |
+
with gr.Blocks(title="Qwen2.5-Coder-32B-Instruct AWQ (ZeroGPU)", theme=gr.themes.Soft()) as demo:
|
| 69 |
+
gr.Markdown("# 馃殌 Qwen2.5-Coder-32B-Instruct (AWQ en ZeroGPU)")
|
| 70 |
+
gr.Markdown("Servicio de inferencia optimizado para c贸digo y compatible con clientes v铆a API / Gradio.")
|
| 71 |
+
|
| 72 |
+
chat_interface = gr.ChatInterface(
|
| 73 |
+
fn=generate,
|
| 74 |
+
additional_inputs=[
|
| 75 |
+
gr.Textbox(
|
| 76 |
+
value="Eres un asistente de programaci贸n experto, preciso y conciso.",
|
| 77 |
+
label="System Prompt",
|
| 78 |
+
lines=2
|
| 79 |
+
),
|
| 80 |
+
gr.Slider(
|
| 81 |
+
minimum=0.0,
|
| 82 |
+
maximum=1.0,
|
| 83 |
+
value=0.2,
|
| 84 |
+
step=0.05,
|
| 85 |
+
label="Temperature"
|
| 86 |
+
),
|
| 87 |
+
gr.Slider(
|
| 88 |
+
minimum=0.1,
|
| 89 |
+
maximum=1.0,
|
| 90 |
+
value=0.9,
|
| 91 |
+
step=0.05,
|
| 92 |
+
label="Top-P"
|
| 93 |
+
),
|
| 94 |
+
gr.Slider(
|
| 95 |
+
minimum=256,
|
| 96 |
+
maximum=4096,
|
| 97 |
+
value=2048,
|
| 98 |
+
step=256,
|
| 99 |
+
label="Max New Tokens"
|
| 100 |
+
)
|
| 101 |
+
]
|
| 102 |
+
)
|
| 103 |
|
| 104 |
if __name__ == "__main__":
|
| 105 |
demo.queue().launch()
|