Commit ·
68385f9
0
Parent(s):
added:
Browse files- README.md +17 -0
- app.py +71 -0
- frontend/app.js +45 -0
- frontend/index.html +22 -0
- frontend/style.css +66 -0
- requirements.txt +4 -0
README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: dllm
|
| 3 |
+
emoji: 🧠
|
| 4 |
+
colorFrom: indigo
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.9.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# dllm
|
| 13 |
+
|
| 14 |
+
Custom FastAPI backend + vanilla JS frontend, running on Hugging Face Spaces'
|
| 15 |
+
free Gradio SDK tier with ZeroGPU. Gradio is mounted at `/gradio` purely as
|
| 16 |
+
the ZeroGPU scheduling harness; the frontend at `/` and any `/api/*` routes
|
| 17 |
+
are plain FastAPI.
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import threading
|
| 3 |
+
|
| 4 |
+
import spaces
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
import uvicorn
|
| 8 |
+
from fastapi import FastAPI
|
| 9 |
+
from fastapi.staticfiles import StaticFiles
|
| 10 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 11 |
+
|
| 12 |
+
MODEL_ID = os.environ.get("MODEL_ID", "Qwen/Qwen2.5-0.5B-Instruct")
|
| 13 |
+
FRONTEND = os.path.join(os.path.dirname(os.path.abspath(__file__)), "frontend")
|
| 14 |
+
|
| 15 |
+
# Loaded on CPU at import. Touching CUDA here would break ZeroGPU allocation.
|
| 16 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 17 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_ID, torch_dtype=torch.float16)
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
@spaces.GPU(duration=60)
|
| 21 |
+
def infer(prompt, max_new_tokens):
|
| 22 |
+
model.to("cuda")
|
| 23 |
+
input_ids = tokenizer.apply_chat_template(
|
| 24 |
+
[{"role": "user", "content": prompt}],
|
| 25 |
+
add_generation_prompt=True,
|
| 26 |
+
return_tensors="pt",
|
| 27 |
+
).to("cuda")
|
| 28 |
+
|
| 29 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
|
| 30 |
+
threading.Thread(
|
| 31 |
+
target=model.generate,
|
| 32 |
+
kwargs=dict(
|
| 33 |
+
input_ids=input_ids,
|
| 34 |
+
max_new_tokens=int(max_new_tokens),
|
| 35 |
+
do_sample=True,
|
| 36 |
+
temperature=0.7,
|
| 37 |
+
streamer=streamer,
|
| 38 |
+
),
|
| 39 |
+
).start()
|
| 40 |
+
|
| 41 |
+
acc = ""
|
| 42 |
+
for chunk in streamer:
|
| 43 |
+
acc += chunk
|
| 44 |
+
yield acc
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
with gr.Blocks() as demo:
|
| 48 |
+
prompt_box = gr.Textbox(label="Prompt")
|
| 49 |
+
token_slider = gr.Slider(16, 512, value=128, step=16, label="Max new tokens")
|
| 50 |
+
output_box = gr.Textbox(label="Output")
|
| 51 |
+
gr.Button("Generate").click(
|
| 52 |
+
infer, [prompt_box, token_slider], output_box, api_name="infer"
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
demo.queue(max_size=32)
|
| 56 |
+
|
| 57 |
+
app = FastAPI()
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
@app.get("/api/health")
|
| 61 |
+
def health():
|
| 62 |
+
return {"status": "ok", "model": MODEL_ID}
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
app = gr.mount_gradio_app(app, demo, path="/gradio")
|
| 66 |
+
|
| 67 |
+
# Catch-all: must stay last so /api and /gradio keep their routes.
|
| 68 |
+
app.mount("/", StaticFiles(directory=FRONTEND, html=True), name="ui")
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
frontend/app.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const promptEl = document.getElementById("prompt");
|
| 2 |
+
const tokensEl = document.getElementById("tokens");
|
| 3 |
+
const outputEl = document.getElementById("output");
|
| 4 |
+
const runBtn = document.getElementById("run");
|
| 5 |
+
|
| 6 |
+
async function generate() {
|
| 7 |
+
const prompt = promptEl.value.trim();
|
| 8 |
+
if (!prompt) return;
|
| 9 |
+
|
| 10 |
+
runBtn.disabled = true;
|
| 11 |
+
outputEl.textContent = "";
|
| 12 |
+
|
| 13 |
+
try {
|
| 14 |
+
const startRes = await fetch("/gradio/call/infer", {
|
| 15 |
+
method: "POST",
|
| 16 |
+
headers: { "Content-Type": "application/json" },
|
| 17 |
+
body: JSON.stringify({ data: [prompt, Number(tokensEl.value)] }),
|
| 18 |
+
});
|
| 19 |
+
if (!startRes.ok) throw new Error(`start failed: ${startRes.status}`);
|
| 20 |
+
const { event_id } = await startRes.json();
|
| 21 |
+
|
| 22 |
+
const es = new EventSource(`/gradio/call/infer/${event_id}`);
|
| 23 |
+
|
| 24 |
+
es.addEventListener("message", (e) => {
|
| 25 |
+
const payload = JSON.parse(e.data);
|
| 26 |
+
outputEl.textContent = payload[0] ?? "";
|
| 27 |
+
});
|
| 28 |
+
|
| 29 |
+
es.addEventListener("error", () => {
|
| 30 |
+
es.close();
|
| 31 |
+
runBtn.disabled = false;
|
| 32 |
+
});
|
| 33 |
+
|
| 34 |
+
// Gradio sends a "complete" typed event when generation finishes.
|
| 35 |
+
es.addEventListener("complete", () => {
|
| 36 |
+
es.close();
|
| 37 |
+
runBtn.disabled = false;
|
| 38 |
+
});
|
| 39 |
+
} catch (err) {
|
| 40 |
+
outputEl.textContent = `Error: ${err.message}`;
|
| 41 |
+
runBtn.disabled = false;
|
| 42 |
+
}
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
runBtn.addEventListener("click", generate);
|
frontend/index.html
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!doctype html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="utf-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
| 6 |
+
<title>dllm</title>
|
| 7 |
+
<link rel="stylesheet" href="/style.css" />
|
| 8 |
+
</head>
|
| 9 |
+
<body>
|
| 10 |
+
<main class="wrap">
|
| 11 |
+
<h1>dllm</h1>
|
| 12 |
+
<textarea id="prompt" placeholder="Type a prompt..."></textarea>
|
| 13 |
+
<div class="row">
|
| 14 |
+
<label for="tokens">Max tokens</label>
|
| 15 |
+
<input id="tokens" type="number" value="128" min="16" max="512" step="16" />
|
| 16 |
+
<button id="run">Generate</button>
|
| 17 |
+
</div>
|
| 18 |
+
<pre id="output"></pre>
|
| 19 |
+
</main>
|
| 20 |
+
<script src="/app.js"></script>
|
| 21 |
+
</body>
|
| 22 |
+
</html>
|
frontend/style.css
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
* { box-sizing: border-box; }
|
| 2 |
+
|
| 3 |
+
body {
|
| 4 |
+
margin: 0;
|
| 5 |
+
font-family: system-ui, sans-serif;
|
| 6 |
+
background: #0f1115;
|
| 7 |
+
color: #e6e6e6;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
.wrap {
|
| 11 |
+
max-width: 640px;
|
| 12 |
+
margin: 3rem auto;
|
| 13 |
+
padding: 0 1rem;
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
textarea {
|
| 17 |
+
width: 100%;
|
| 18 |
+
min-height: 100px;
|
| 19 |
+
padding: 0.75rem;
|
| 20 |
+
background: #1a1d24;
|
| 21 |
+
color: inherit;
|
| 22 |
+
border: 1px solid #2a2e37;
|
| 23 |
+
border-radius: 8px;
|
| 24 |
+
font-size: 1rem;
|
| 25 |
+
resize: vertical;
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
.row {
|
| 29 |
+
display: flex;
|
| 30 |
+
align-items: center;
|
| 31 |
+
gap: 0.5rem;
|
| 32 |
+
margin: 0.75rem 0;
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
.row input {
|
| 36 |
+
width: 5rem;
|
| 37 |
+
padding: 0.4rem;
|
| 38 |
+
background: #1a1d24;
|
| 39 |
+
color: inherit;
|
| 40 |
+
border: 1px solid #2a2e37;
|
| 41 |
+
border-radius: 6px;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
button {
|
| 45 |
+
padding: 0.5rem 1rem;
|
| 46 |
+
background: #6d5ef8;
|
| 47 |
+
color: white;
|
| 48 |
+
border: none;
|
| 49 |
+
border-radius: 6px;
|
| 50 |
+
cursor: pointer;
|
| 51 |
+
font-size: 1rem;
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
button:disabled {
|
| 55 |
+
opacity: 0.5;
|
| 56 |
+
cursor: default;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
pre {
|
| 60 |
+
white-space: pre-wrap;
|
| 61 |
+
background: #1a1d24;
|
| 62 |
+
border: 1px solid #2a2e37;
|
| 63 |
+
border-radius: 8px;
|
| 64 |
+
padding: 0.75rem;
|
| 65 |
+
min-height: 3rem;
|
| 66 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
accelerate
|
| 4 |
+
spaces
|