Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,6 @@ import json
|
|
| 4 |
import uuid
|
| 5 |
import torch
|
| 6 |
import spaces
|
| 7 |
-
import uvicorn
|
| 8 |
from fastapi import FastAPI
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
from pydantic import BaseModel
|
|
@@ -16,19 +15,18 @@ import gradio as gr
|
|
| 16 |
|
| 17 |
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct-AWQ"
|
| 18 |
|
| 19 |
-
#
|
| 20 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
model = AutoAWQForCausalLM.from_quantized(
|
| 24 |
MODEL_ID,
|
| 25 |
-
fuse_layers=
|
| 26 |
trust_remote_code=True,
|
| 27 |
safetensors=True
|
| 28 |
)
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
class ChatMessage(BaseModel):
|
| 33 |
role: str
|
| 34 |
content: str
|
|
@@ -41,6 +39,7 @@ class ChatCompletionRequest(BaseModel):
|
|
| 41 |
max_tokens: Optional[int] = 2048
|
| 42 |
stream: Optional[bool] = False
|
| 43 |
|
|
|
|
| 44 |
@spaces.GPU(duration=120)
|
| 45 |
def generate_stream_tokens(messages_dict: List[Dict[str, str]], temperature: float, top_p: float, max_tokens: int):
|
| 46 |
text = tokenizer.apply_chat_template(
|
|
@@ -73,7 +72,41 @@ def generate_stream_tokens(messages_dict: List[Dict[str, str]], temperature: flo
|
|
| 73 |
for new_token in streamer:
|
| 74 |
yield new_token
|
| 75 |
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
async def list_models():
|
| 78 |
return {
|
| 79 |
"object": "list",
|
|
@@ -87,7 +120,7 @@ async def list_models():
|
|
| 87 |
]
|
| 88 |
}
|
| 89 |
|
| 90 |
-
@
|
| 91 |
async def chat_completions(req: ChatCompletionRequest):
|
| 92 |
req_id = f"chatcmpl-{uuid.uuid4().hex[:12]}"
|
| 93 |
created_time = int(time.time())
|
|
@@ -146,35 +179,5 @@ async def chat_completions(req: ChatCompletionRequest):
|
|
| 146 |
"usage": {"prompt_tokens": -1, "completion_tokens": -1, "total_tokens": -1}
|
| 147 |
}
|
| 148 |
|
| 149 |
-
def gradio_generate(message, history, system_prompt, temperature, top_p, max_tokens):
|
| 150 |
-
messages = []
|
| 151 |
-
if system_prompt:
|
| 152 |
-
messages.append({"role": "system", "content": system_prompt})
|
| 153 |
-
for user_msg, bot_msg in history:
|
| 154 |
-
messages.append({"role": "user", "content": user_msg})
|
| 155 |
-
if bot_msg:
|
| 156 |
-
messages.append({"role": "assistant", "content": bot_msg})
|
| 157 |
-
messages.append({"role": "user", "content": message})
|
| 158 |
-
|
| 159 |
-
partial_text = ""
|
| 160 |
-
for token in generate_stream_tokens(messages, temperature, top_p, int(max_tokens)):
|
| 161 |
-
partial_text += token
|
| 162 |
-
yield partial_text
|
| 163 |
-
|
| 164 |
-
with gr.Blocks(title="Qwen2.5-Coder-32B API & UI", theme=gr.themes.Soft()) as gradio_app:
|
| 165 |
-
gr.Markdown("# 馃殌 Qwen2.5-Coder-32B-Instruct (AWQ en ZeroGPU)")
|
| 166 |
-
gr.Markdown("Endpoint OpenAI: `/v1/chat/completions` | Modelo: `Qwen/Qwen2.5-Coder-32B-Instruct-AWQ`")
|
| 167 |
-
gr.ChatInterface(
|
| 168 |
-
fn=gradio_generate,
|
| 169 |
-
additional_inputs=[
|
| 170 |
-
gr.Textbox("Eres un asistente de programaci贸n experto.", label="System Prompt"),
|
| 171 |
-
gr.Slider(0.0, 1.0, 0.2, step=0.05, label="Temperature"),
|
| 172 |
-
gr.Slider(0.1, 1.0, 0.9, step=0.05, label="Top-P"),
|
| 173 |
-
gr.Slider(256, 4096, 2048, step=256, label="Max New Tokens")
|
| 174 |
-
]
|
| 175 |
-
)
|
| 176 |
-
|
| 177 |
-
app = gr.mount_gradio_app(app, gradio_app, path="/")
|
| 178 |
-
|
| 179 |
if __name__ == "__main__":
|
| 180 |
-
|
|
|
|
| 4 |
import uuid
|
| 5 |
import torch
|
| 6 |
import spaces
|
|
|
|
| 7 |
from fastapi import FastAPI
|
| 8 |
from fastapi.responses import StreamingResponse
|
| 9 |
from pydantic import BaseModel
|
|
|
|
| 15 |
|
| 16 |
MODEL_ID = "Qwen/Qwen2.5-Coder-32B-Instruct-AWQ"
|
| 17 |
|
| 18 |
+
# Cargar Tokenizer
|
| 19 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
|
| 20 |
|
| 21 |
+
# Cargar Modelo AWQ
|
| 22 |
model = AutoAWQForCausalLM.from_quantized(
|
| 23 |
MODEL_ID,
|
| 24 |
+
fuse_layers=False,
|
| 25 |
trust_remote_code=True,
|
| 26 |
safetensors=True
|
| 27 |
)
|
| 28 |
|
| 29 |
+
# Esquemas para la API OpenAI
|
|
|
|
| 30 |
class ChatMessage(BaseModel):
|
| 31 |
role: str
|
| 32 |
content: str
|
|
|
|
| 39 |
max_tokens: Optional[int] = 2048
|
| 40 |
stream: Optional[bool] = False
|
| 41 |
|
| 42 |
+
# Generaci贸n en GPU con ZeroGPU
|
| 43 |
@spaces.GPU(duration=120)
|
| 44 |
def generate_stream_tokens(messages_dict: List[Dict[str, str]], temperature: float, top_p: float, max_tokens: int):
|
| 45 |
text = tokenizer.apply_chat_template(
|
|
|
|
| 72 |
for new_token in streamer:
|
| 73 |
yield new_token
|
| 74 |
|
| 75 |
+
# Funciones de soporte para Gradio UI
|
| 76 |
+
def gradio_generate(message, history, system_prompt, temperature, top_p, max_tokens):
|
| 77 |
+
messages = []
|
| 78 |
+
if system_prompt:
|
| 79 |
+
messages.append({"role": "system", "content": system_prompt})
|
| 80 |
+
for user_msg, bot_msg in history:
|
| 81 |
+
messages.append({"role": "user", "content": user_msg})
|
| 82 |
+
if bot_msg:
|
| 83 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 84 |
+
messages.append({"role": "user", "content": message})
|
| 85 |
+
|
| 86 |
+
partial_text = ""
|
| 87 |
+
for token in generate_stream_tokens(messages, temperature, top_p, int(max_tokens)):
|
| 88 |
+
partial_text += token
|
| 89 |
+
yield partial_text
|
| 90 |
+
|
| 91 |
+
# Definici贸n de la interfaz Gradio
|
| 92 |
+
with gr.Blocks(title="Qwen2.5-Coder-32B API & UI") as demo:
|
| 93 |
+
gr.Markdown("# 馃殌 Qwen2.5-Coder-32B-Instruct (AWQ en ZeroGPU)")
|
| 94 |
+
gr.Markdown("Compatible con OpenAI: `/v1/chat/completions` | Modelo: `Qwen/Qwen2.5-Coder-32B-Instruct-AWQ`")
|
| 95 |
+
|
| 96 |
+
gr.ChatInterface(
|
| 97 |
+
fn=gradio_generate,
|
| 98 |
+
additional_inputs=[
|
| 99 |
+
gr.Textbox("Eres un asistente de programaci贸n experto.", label="System Prompt"),
|
| 100 |
+
gr.Slider(0.0, 1.0, 0.2, step=0.05, label="Temperature"),
|
| 101 |
+
gr.Slider(0.1, 1.0, 0.9, step=0.05, label="Top-P"),
|
| 102 |
+
gr.Slider(256, 4096, 2048, step=256, label="Max New Tokens")
|
| 103 |
+
]
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
# Definici贸n de FastAPI y vinculaci贸n de endpoints a la app subyacente de Gradio
|
| 107 |
+
fastapi_app = demo.app
|
| 108 |
+
|
| 109 |
+
@fastapi_app.get("/v1/models")
|
| 110 |
async def list_models():
|
| 111 |
return {
|
| 112 |
"object": "list",
|
|
|
|
| 120 |
]
|
| 121 |
}
|
| 122 |
|
| 123 |
+
@fastapi_app.post("/v1/chat/completions")
|
| 124 |
async def chat_completions(req: ChatCompletionRequest):
|
| 125 |
req_id = f"chatcmpl-{uuid.uuid4().hex[:12]}"
|
| 126 |
created_time = int(time.time())
|
|
|
|
| 179 |
"usage": {"prompt_tokens": -1, "completion_tokens": -1, "total_tokens": -1}
|
| 180 |
}
|
| 181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
if __name__ == "__main__":
|
| 183 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860)
|