Spaces:
Running
Running
Initial demo: streaming DNA continuation with logprob coloring
Browse files- Dockerfile +13 -0
- README.md +21 -5
- app.py +87 -0
- index.html +636 -0
- requirements.txt +4 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY app.py index.html ./
|
| 9 |
+
|
| 10 |
+
ENV PORT=7860
|
| 11 |
+
EXPOSE 7860
|
| 12 |
+
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
README.md
CHANGED
|
@@ -1,10 +1,26 @@
|
|
| 1 |
---
|
| 2 |
-
title: Carbon
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
|
|
|
| 7 |
pinned: false
|
| 8 |
---
|
| 9 |
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Carbon DNA Continuation
|
| 3 |
+
emoji: 🧬
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
| 11 |
+
# Carbon · DNA Continuation
|
| 12 |
+
|
| 13 |
+
A streaming demo for the `hf-carbon/carbon-3B-hybrid-loss-1T-mix2-v1` model. Enter a DNA sequence prefix and watch the model continue it.
|
| 14 |
+
|
| 15 |
+
## Local
|
| 16 |
+
|
| 17 |
+
```bash
|
| 18 |
+
pip install -r requirements.txt
|
| 19 |
+
uvicorn app:app --reload --port 7860
|
| 20 |
+
```
|
| 21 |
+
|
| 22 |
+
Auth uses the locally cached HF token (`huggingface-cli login`). Override the endpoint with `ENDPOINT_URL` and the model with `MODEL_NAME`.
|
| 23 |
+
|
| 24 |
+
## Spaces
|
| 25 |
+
|
| 26 |
+
Deploy as a Docker Space. Set `HF_TOKEN` as a secret (the inference endpoint requires a token authorized for it).
|
app.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
from fastapi import FastAPI, Request
|
| 5 |
+
from fastapi.responses import FileResponse, StreamingResponse
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
|
| 8 |
+
ENDPOINT_URL = os.environ.get(
|
| 9 |
+
"ENDPOINT_URL",
|
| 10 |
+
"https://z7nx05gfuixijytx.us-east-1.aws.endpoints.huggingface.cloud/v1/",
|
| 11 |
+
)
|
| 12 |
+
MODEL_NAME = os.environ.get(
|
| 13 |
+
"MODEL_NAME",
|
| 14 |
+
"hf-carbon/carbon-3B-hybrid-loss-1T-mix2-v1",
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
HERE = os.path.dirname(os.path.abspath(__file__))
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def get_api_key():
|
| 21 |
+
key = os.environ.get("HF_TOKEN")
|
| 22 |
+
if key:
|
| 23 |
+
return key
|
| 24 |
+
try:
|
| 25 |
+
from huggingface_hub import get_token
|
| 26 |
+
return get_token()
|
| 27 |
+
except Exception:
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
app = FastAPI()
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
@app.get("/")
|
| 35 |
+
def root():
|
| 36 |
+
return FileResponse(os.path.join(HERE, "index.html"))
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@app.get("/config")
|
| 40 |
+
def config():
|
| 41 |
+
return {"model": MODEL_NAME, "endpoint": ENDPOINT_URL}
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
@app.post("/generate")
|
| 45 |
+
async def generate(request: Request):
|
| 46 |
+
body = await request.json()
|
| 47 |
+
prompt = (body.get("prompt") or "").strip().upper()
|
| 48 |
+
max_tokens = int(body.get("max_tokens", 128))
|
| 49 |
+
temperature = float(body.get("temperature", 1.0))
|
| 50 |
+
top_p = float(body.get("top_p", 1.0))
|
| 51 |
+
|
| 52 |
+
api_key = get_api_key()
|
| 53 |
+
if not api_key:
|
| 54 |
+
return {"error": "no HF token available — set HF_TOKEN env var or run `huggingface-cli login`"}
|
| 55 |
+
|
| 56 |
+
full_prompt = "<dna>" + prompt
|
| 57 |
+
|
| 58 |
+
def stream():
|
| 59 |
+
try:
|
| 60 |
+
client = OpenAI(base_url=ENDPOINT_URL, api_key=api_key)
|
| 61 |
+
completion = client.completions.create(
|
| 62 |
+
model=MODEL_NAME,
|
| 63 |
+
prompt=full_prompt,
|
| 64 |
+
stream=True,
|
| 65 |
+
max_tokens=max_tokens,
|
| 66 |
+
temperature=temperature,
|
| 67 |
+
top_p=top_p,
|
| 68 |
+
logprobs=5,
|
| 69 |
+
)
|
| 70 |
+
for chunk in completion:
|
| 71 |
+
ch = chunk.choices[0]
|
| 72 |
+
payload = {}
|
| 73 |
+
if ch.text:
|
| 74 |
+
payload["text"] = ch.text
|
| 75 |
+
if ch.logprobs and ch.logprobs.tokens:
|
| 76 |
+
payload["logprobs"] = {
|
| 77 |
+
"tokens": ch.logprobs.tokens,
|
| 78 |
+
"token_logprobs": ch.logprobs.token_logprobs,
|
| 79 |
+
"top_logprobs": ch.logprobs.top_logprobs,
|
| 80 |
+
}
|
| 81 |
+
if payload:
|
| 82 |
+
yield f"data: {json.dumps(payload)}\n\n"
|
| 83 |
+
yield f"data: {json.dumps({'done': True})}\n\n"
|
| 84 |
+
except Exception as e:
|
| 85 |
+
yield f"data: {json.dumps({'error': str(e)})}\n\n"
|
| 86 |
+
|
| 87 |
+
return StreamingResponse(stream(), media_type="text/event-stream")
|
index.html
ADDED
|
@@ -0,0 +1,636 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.0">
|
| 6 |
+
<title>Carbon · DNA Continuation</title>
|
| 7 |
+
<style>
|
| 8 |
+
@import url('https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;600&family=Inter:wght@300;400;500;600&display=swap');
|
| 9 |
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
| 10 |
+
body {
|
| 11 |
+
font-family: "Inter", "Helvetica Neue", sans-serif;
|
| 12 |
+
font-size: 12px; font-weight: 300; line-height: 1.6;
|
| 13 |
+
color: #1a1a1a; background: #fafafa;
|
| 14 |
+
padding: 24px 32px;
|
| 15 |
+
max-width: 1280px; margin: 0 auto;
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
h1 {
|
| 19 |
+
font-family: "JetBrains Mono", monospace;
|
| 20 |
+
font-size: 16px; font-weight: 400; letter-spacing: 2px;
|
| 21 |
+
}
|
| 22 |
+
.meta {
|
| 23 |
+
font-family: "JetBrains Mono", monospace;
|
| 24 |
+
color: #888; font-size: 10px; font-weight: 300;
|
| 25 |
+
letter-spacing: 0.5px; margin-top: 4px;
|
| 26 |
+
}
|
| 27 |
+
.header-row {
|
| 28 |
+
margin-bottom: 20px; padding-bottom: 12px; border-bottom: 1px solid #ddd;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
.section-title {
|
| 32 |
+
font-family: "JetBrains Mono", monospace;
|
| 33 |
+
font-size: 11px; font-weight: 400;
|
| 34 |
+
text-transform: uppercase; letter-spacing: 2px; color: #444;
|
| 35 |
+
margin-top: 24px; margin-bottom: 8px;
|
| 36 |
+
border-bottom: 1px solid #ccc; padding-bottom: 4px;
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
/* --- Examples --- */
|
| 40 |
+
.examples {
|
| 41 |
+
display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px;
|
| 42 |
+
align-items: center;
|
| 43 |
+
}
|
| 44 |
+
.examples-label {
|
| 45 |
+
font-family: "JetBrains Mono", monospace;
|
| 46 |
+
font-size: 10px; color: #999; text-transform: uppercase; letter-spacing: 1px;
|
| 47 |
+
margin-right: 4px;
|
| 48 |
+
}
|
| 49 |
+
.ex-btn {
|
| 50 |
+
font-family: "JetBrains Mono", monospace;
|
| 51 |
+
font-size: 10px; padding: 3px 8px;
|
| 52 |
+
border: 1px solid #ddd; border-radius: 3px;
|
| 53 |
+
background: #fff; color: #666; cursor: pointer;
|
| 54 |
+
transition: all 0.15s;
|
| 55 |
+
}
|
| 56 |
+
.ex-btn:hover { border-color: #888; color: #1a1a1a; }
|
| 57 |
+
.ex-btn .ex-label {
|
| 58 |
+
color: #aaa; font-size: 9px; margin-left: 6px; text-transform: uppercase;
|
| 59 |
+
letter-spacing: 0.5px;
|
| 60 |
+
}
|
| 61 |
+
|
| 62 |
+
/* --- Inputs --- */
|
| 63 |
+
textarea, input[type=number] {
|
| 64 |
+
font-family: "JetBrains Mono", monospace;
|
| 65 |
+
font-size: 12px; font-weight: 300; color: #1a1a1a;
|
| 66 |
+
background: #fff; border: 1px solid #ddd; border-radius: 3px;
|
| 67 |
+
padding: 8px 12px; outline: none; transition: border 0.15s;
|
| 68 |
+
}
|
| 69 |
+
textarea:focus, input[type=number]:focus { border-color: #1a1a1a; }
|
| 70 |
+
textarea {
|
| 71 |
+
width: 100%; resize: none; overflow: hidden;
|
| 72 |
+
letter-spacing: 1px; line-height: 1.7;
|
| 73 |
+
min-height: 36px;
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
.controls {
|
| 77 |
+
display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
|
| 78 |
+
margin-top: 10px;
|
| 79 |
+
}
|
| 80 |
+
.control {
|
| 81 |
+
display: flex; align-items: center; gap: 6px;
|
| 82 |
+
font-family: "JetBrains Mono", monospace; font-size: 10px; color: #666;
|
| 83 |
+
text-transform: uppercase; letter-spacing: 1px;
|
| 84 |
+
}
|
| 85 |
+
.control input[type=number] {
|
| 86 |
+
width: 64px; padding: 4px 6px; font-size: 11px; text-align: right;
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/* Color mode toggle */
|
| 90 |
+
.mode-group {
|
| 91 |
+
display: flex; align-items: center; gap: 6px;
|
| 92 |
+
font-family: "JetBrains Mono", monospace; font-size: 10px; color: #666;
|
| 93 |
+
text-transform: uppercase; letter-spacing: 1px;
|
| 94 |
+
}
|
| 95 |
+
.mode-btns { display: flex; }
|
| 96 |
+
.mode-btn {
|
| 97 |
+
font-family: "JetBrains Mono", monospace;
|
| 98 |
+
font-size: 10px; padding: 4px 9px;
|
| 99 |
+
border: 1px solid #ccc; border-right: none;
|
| 100 |
+
background: #fff; color: #666; cursor: pointer;
|
| 101 |
+
text-transform: uppercase; letter-spacing: 1px;
|
| 102 |
+
transition: all 0.15s;
|
| 103 |
+
}
|
| 104 |
+
.mode-btn:first-child { border-radius: 3px 0 0 3px; }
|
| 105 |
+
.mode-btn:last-child { border-right: 1px solid #ccc; border-radius: 0 3px 3px 0; }
|
| 106 |
+
.mode-btn:hover { color: #1a1a1a; }
|
| 107 |
+
.mode-btn.active { background: #1a1a1a; color: #fff; border-color: #1a1a1a; }
|
| 108 |
+
|
| 109 |
+
button.action {
|
| 110 |
+
font-family: "JetBrains Mono", monospace;
|
| 111 |
+
font-size: 10px; font-weight: 400;
|
| 112 |
+
padding: 5px 12px; border: 1px solid #ccc; border-radius: 3px;
|
| 113 |
+
background: #fff; color: #555; cursor: pointer;
|
| 114 |
+
text-transform: uppercase; letter-spacing: 1.5px;
|
| 115 |
+
transition: all 0.15s;
|
| 116 |
+
}
|
| 117 |
+
button.action:hover { border-color: #888; color: #1a1a1a; }
|
| 118 |
+
button.action.primary { background: #1a1a1a; color: #fff; border-color: #1a1a1a; }
|
| 119 |
+
button.action.primary:hover { background: #000; }
|
| 120 |
+
button.action:disabled { opacity: 0.4; cursor: not-allowed; }
|
| 121 |
+
button.action.primary:disabled { background: #888; border-color: #888; }
|
| 122 |
+
|
| 123 |
+
.button-row { margin-left: auto; display: flex; gap: 6px; }
|
| 124 |
+
|
| 125 |
+
/* --- Status --- */
|
| 126 |
+
.status {
|
| 127 |
+
font-family: "JetBrains Mono", monospace;
|
| 128 |
+
font-size: 10px; color: #666;
|
| 129 |
+
text-transform: uppercase; letter-spacing: 1.5px;
|
| 130 |
+
margin-top: 10px; min-height: 14px;
|
| 131 |
+
}
|
| 132 |
+
.status.error { color: #b00020; text-transform: none; letter-spacing: 0.3px; }
|
| 133 |
+
.status .dot {
|
| 134 |
+
display: inline-block; width: 6px; height: 6px; border-radius: 50%;
|
| 135 |
+
background: #888; margin-right: 6px; vertical-align: middle;
|
| 136 |
+
}
|
| 137 |
+
.status.streaming .dot { background: #1a8a3a; animation: pulse 1.2s ease-in-out infinite; }
|
| 138 |
+
@keyframes pulse { 50% { opacity: 0.3; } }
|
| 139 |
+
|
| 140 |
+
/* --- Output layout: sequence + sticky stats sidebar --- */
|
| 141 |
+
.output-row {
|
| 142 |
+
display: grid;
|
| 143 |
+
grid-template-columns: minmax(0, 1fr) 200px;
|
| 144 |
+
gap: 16px;
|
| 145 |
+
align-items: start;
|
| 146 |
+
}
|
| 147 |
+
|
| 148 |
+
.seq-block {
|
| 149 |
+
font-family: "JetBrains Mono", monospace;
|
| 150 |
+
background: #f4f4f4; border: 1px solid #ddd;
|
| 151 |
+
padding: 16px 20px; overflow-x: auto;
|
| 152 |
+
white-space: pre; font-size: 12px; font-weight: 400;
|
| 153 |
+
line-height: 1.85; letter-spacing: 1.5px;
|
| 154 |
+
min-height: 80px;
|
| 155 |
+
}
|
| 156 |
+
.seq-block.empty { color: #aaa; font-weight: 300; letter-spacing: normal; }
|
| 157 |
+
.seq-line { white-space: pre; }
|
| 158 |
+
.pos { color: #bbb; user-select: none; font-weight: 300; }
|
| 159 |
+
.cursor {
|
| 160 |
+
display: inline-block; width: 7px; height: 14px;
|
| 161 |
+
background: #1a1a1a; vertical-align: text-bottom;
|
| 162 |
+
margin-left: 2px;
|
| 163 |
+
animation: blink 1s step-end infinite;
|
| 164 |
+
}
|
| 165 |
+
@keyframes blink { 50% { opacity: 0; } }
|
| 166 |
+
|
| 167 |
+
/* Token-block hover (for logprob/bases mode tooltips later) */
|
| 168 |
+
.tok { transition: background 0.1s; }
|
| 169 |
+
.tok:hover { background: #e8e8e8; }
|
| 170 |
+
|
| 171 |
+
/* --- Stats sidebar --- */
|
| 172 |
+
.stats {
|
| 173 |
+
position: sticky; top: 16px;
|
| 174 |
+
border: 1px solid #ddd; background: #fff;
|
| 175 |
+
}
|
| 176 |
+
.stat {
|
| 177 |
+
display: flex; justify-content: space-between; align-items: baseline;
|
| 178 |
+
padding: 8px 12px;
|
| 179 |
+
border-bottom: 1px solid #eee;
|
| 180 |
+
font-family: "JetBrains Mono", monospace;
|
| 181 |
+
}
|
| 182 |
+
.stat:last-child { border-bottom: none; }
|
| 183 |
+
.stat-label {
|
| 184 |
+
font-size: 9px; color: #999;
|
| 185 |
+
text-transform: uppercase; letter-spacing: 1.2px; font-weight: 300;
|
| 186 |
+
}
|
| 187 |
+
.stat-value {
|
| 188 |
+
font-size: 12px; font-weight: 400; color: #1a1a1a;
|
| 189 |
+
font-variant-numeric: tabular-nums;
|
| 190 |
+
}
|
| 191 |
+
.stat-value .unit { font-size: 9px; color: #999; margin-left: 3px; font-weight: 300; }
|
| 192 |
+
|
| 193 |
+
/* Logprob legend (only shown in logprob mode) */
|
| 194 |
+
.legend {
|
| 195 |
+
margin-top: 8px;
|
| 196 |
+
padding: 8px 12px;
|
| 197 |
+
background: #fff; border: 1px solid #ddd;
|
| 198 |
+
font-family: "JetBrains Mono", monospace;
|
| 199 |
+
font-size: 9px; color: #888;
|
| 200 |
+
text-transform: uppercase; letter-spacing: 1px;
|
| 201 |
+
display: none;
|
| 202 |
+
}
|
| 203 |
+
.legend.show { display: block; }
|
| 204 |
+
.legend-bar {
|
| 205 |
+
height: 6px; margin: 4px 0 3px;
|
| 206 |
+
background: linear-gradient(to right, #d83a2a, #888, #1a1a1a);
|
| 207 |
+
border-radius: 1px;
|
| 208 |
+
}
|
| 209 |
+
.legend-row { display: flex; justify-content: space-between; }
|
| 210 |
+
|
| 211 |
+
::-webkit-scrollbar { width: 8px; height: 8px; }
|
| 212 |
+
::-webkit-scrollbar-thumb { background: #ccc; border-radius: 4px; }
|
| 213 |
+
::-webkit-scrollbar-track { background: transparent; }
|
| 214 |
+
</style>
|
| 215 |
+
</head>
|
| 216 |
+
<body>
|
| 217 |
+
|
| 218 |
+
<div class="header-row">
|
| 219 |
+
<h1>CARBON · DNA CONTINUATION</h1>
|
| 220 |
+
<div class="meta" id="meta">loading…</div>
|
| 221 |
+
</div>
|
| 222 |
+
|
| 223 |
+
<div class="section-title">Prompt</div>
|
| 224 |
+
|
| 225 |
+
<div class="examples">
|
| 226 |
+
<span class="examples-label">examples</span>
|
| 227 |
+
<button class="ex-btn" data-ex="">empty<span class="ex-label">unconditional</span></button>
|
| 228 |
+
<button class="ex-btn" data-ex="ATG">ATG<span class="ex-label">start codon</span></button>
|
| 229 |
+
<button class="ex-btn" data-ex="TATAAA">TATAAA<span class="ex-label">TATA box</span></button>
|
| 230 |
+
<button class="ex-btn" data-ex="CGCGCGCGCG">CGCG…<span class="ex-label">CpG island</span></button>
|
| 231 |
+
<button class="ex-btn" data-ex="ATGGCCAAGCTGACCAGCGAGCTGCTG">ATGGCC…<span class="ex-label">ORF start</span></button>
|
| 232 |
+
<button class="ex-btn" data-ex="AAAAAAAAAAAAAAAA">A·16<span class="ex-label">poly-A</span></button>
|
| 233 |
+
</div>
|
| 234 |
+
|
| 235 |
+
<textarea id="prompt" rows="1" spellcheck="false" autocapitalize="characters">AGT</textarea>
|
| 236 |
+
|
| 237 |
+
<div class="controls">
|
| 238 |
+
<label class="control">max tokens
|
| 239 |
+
<input type="number" id="max_tokens" value="128" min="1" max="2048" step="1">
|
| 240 |
+
</label>
|
| 241 |
+
<label class="control">temperature
|
| 242 |
+
<input type="number" id="temperature" value="1.0" min="0" max="2" step="0.1">
|
| 243 |
+
</label>
|
| 244 |
+
<label class="control">top-p
|
| 245 |
+
<input type="number" id="top_p" value="1.0" min="0" max="1" step="0.05">
|
| 246 |
+
</label>
|
| 247 |
+
|
| 248 |
+
<div class="mode-group">color
|
| 249 |
+
<div class="mode-btns" id="mode-btns">
|
| 250 |
+
<button class="mode-btn active" data-mode="none">none</button>
|
| 251 |
+
<button class="mode-btn" data-mode="bases">bases</button>
|
| 252 |
+
<button class="mode-btn" data-mode="logprob">logprob</button>
|
| 253 |
+
</div>
|
| 254 |
+
</div>
|
| 255 |
+
|
| 256 |
+
<div class="button-row">
|
| 257 |
+
<button id="clear-btn" class="action">clear</button>
|
| 258 |
+
<button id="stop-btn" class="action" disabled>stop</button>
|
| 259 |
+
<button id="generate-btn" class="action primary">generate</button>
|
| 260 |
+
</div>
|
| 261 |
+
</div>
|
| 262 |
+
|
| 263 |
+
<div class="status" id="status"><span class="dot"></span><span id="status-text">idle</span></div>
|
| 264 |
+
|
| 265 |
+
<div class="section-title">Sequence</div>
|
| 266 |
+
|
| 267 |
+
<div class="output-row">
|
| 268 |
+
<div>
|
| 269 |
+
<div class="seq-block empty" id="seq">prompt + generated bases will stream here</div>
|
| 270 |
+
</div>
|
| 271 |
+
|
| 272 |
+
<div>
|
| 273 |
+
<div class="stats" id="stats">
|
| 274 |
+
<div class="stat"><span class="stat-label">prompt</span><span class="stat-value" id="stat-prompt">0<span class="unit">bp</span></span></div>
|
| 275 |
+
<div class="stat"><span class="stat-label">generated</span><span class="stat-value" id="stat-gen">0<span class="unit">bp</span></span></div>
|
| 276 |
+
<div class="stat"><span class="stat-label">tokens</span><span class="stat-value" id="stat-tok">0</span></div>
|
| 277 |
+
<div class="stat"><span class="stat-label">elapsed</span><span class="stat-value" id="stat-time">0.0<span class="unit">s</span></span></div>
|
| 278 |
+
<div class="stat"><span class="stat-label">throughput</span><span class="stat-value" id="stat-rate">0<span class="unit">bp/s</span></span></div>
|
| 279 |
+
<div class="stat"><span class="stat-label">GC content</span><span class="stat-value" id="stat-gc">—</span></div>
|
| 280 |
+
<div class="stat"><span class="stat-label">mean logprob</span><span class="stat-value" id="stat-lp">—</span></div>
|
| 281 |
+
<div class="stat"><span class="stat-label">perplexity</span><span class="stat-value" id="stat-ppl">—</span></div>
|
| 282 |
+
</div>
|
| 283 |
+
<div class="legend" id="legend">
|
| 284 |
+
<div>token logprob</div>
|
| 285 |
+
<div class="legend-bar" id="legend-bar"></div>
|
| 286 |
+
<div class="legend-row"><span id="lp-min">—</span><span id="lp-mid">—</span><span id="lp-max">—</span></div>
|
| 287 |
+
</div>
|
| 288 |
+
</div>
|
| 289 |
+
</div>
|
| 290 |
+
|
| 291 |
+
<script>
|
| 292 |
+
const els = {
|
| 293 |
+
prompt: document.getElementById("prompt"),
|
| 294 |
+
maxTokens: document.getElementById("max_tokens"),
|
| 295 |
+
temperature: document.getElementById("temperature"),
|
| 296 |
+
topP: document.getElementById("top_p"),
|
| 297 |
+
generate: document.getElementById("generate-btn"),
|
| 298 |
+
stop: document.getElementById("stop-btn"),
|
| 299 |
+
clear: document.getElementById("clear-btn"),
|
| 300 |
+
modeBtns: document.getElementById("mode-btns"),
|
| 301 |
+
seq: document.getElementById("seq"),
|
| 302 |
+
meta: document.getElementById("meta"),
|
| 303 |
+
status: document.getElementById("status"),
|
| 304 |
+
statusText: document.getElementById("status-text"),
|
| 305 |
+
legend: document.getElementById("legend"),
|
| 306 |
+
statPrompt: document.getElementById("stat-prompt"),
|
| 307 |
+
statGen: document.getElementById("stat-gen"),
|
| 308 |
+
statTok: document.getElementById("stat-tok"),
|
| 309 |
+
statTime: document.getElementById("stat-time"),
|
| 310 |
+
statRate: document.getElementById("stat-rate"),
|
| 311 |
+
statGc: document.getElementById("stat-gc"),
|
| 312 |
+
statLp: document.getElementById("stat-lp"),
|
| 313 |
+
statPpl: document.getElementById("stat-ppl"),
|
| 314 |
+
};
|
| 315 |
+
|
| 316 |
+
let promptBases = "";
|
| 317 |
+
let genText = "";
|
| 318 |
+
let genTokens = []; // [{text, logprob, top}]
|
| 319 |
+
let genTokenAtBase = []; // genTokenAtBase[i] = token index for generated base i
|
| 320 |
+
let abortCtrl = null;
|
| 321 |
+
let startTime = 0;
|
| 322 |
+
let timer = null;
|
| 323 |
+
let colorMode = "none";
|
| 324 |
+
|
| 325 |
+
// --- Color schemes (RGB tuples, used for both fg color and faint bg tint) ---
|
| 326 |
+
const BASE_RGB = {
|
| 327 |
+
A: [58, 138, 62],
|
| 328 |
+
C: [46, 107, 184],
|
| 329 |
+
G: [181, 137, 30],
|
| 330 |
+
T: [181, 58, 58],
|
| 331 |
+
N: [136, 136, 136],
|
| 332 |
+
};
|
| 333 |
+
const PROMPT_RGB = [170, 170, 170];
|
| 334 |
+
const DARK_RGB = [26, 26, 26];
|
| 335 |
+
const MID_RGB = [136, 136, 136];
|
| 336 |
+
const RED_RGB = [216, 58, 42];
|
| 337 |
+
const BG_ALPHA = 0.12;
|
| 338 |
+
|
| 339 |
+
// Logprob gradient: dynamically scaled to observed min/mean/max.
|
| 340 |
+
// Most confident (max) → DARK_RGB, mean → MID_RGB, most uncertain (min) → RED_RGB
|
| 341 |
+
let lpRange = null; // {min, mid, max} or null
|
| 342 |
+
function lerp(a, b, t) { return Math.round(a + (b - a) * t); }
|
| 343 |
+
function lerpRgb(c1, c2, t) {
|
| 344 |
+
return [lerp(c1[0], c2[0], t), lerp(c1[1], c2[1], t), lerp(c1[2], c2[2], t)];
|
| 345 |
+
}
|
| 346 |
+
function recomputeLpRange() {
|
| 347 |
+
if (!genTokens.length) { lpRange = null; updateLegend(); return; }
|
| 348 |
+
let min = Infinity, max = -Infinity, sum = 0, n = 0;
|
| 349 |
+
for (const t of genTokens) {
|
| 350 |
+
if (t.logprob == null || isNaN(t.logprob)) continue;
|
| 351 |
+
if (t.logprob < min) min = t.logprob;
|
| 352 |
+
if (t.logprob > max) max = t.logprob;
|
| 353 |
+
sum += t.logprob; n++;
|
| 354 |
+
}
|
| 355 |
+
lpRange = n ? { min, mid: sum / n, max } : null;
|
| 356 |
+
updateLegend();
|
| 357 |
+
}
|
| 358 |
+
function updateLegend() {
|
| 359 |
+
const minEl = document.getElementById("lp-min");
|
| 360 |
+
const midEl = document.getElementById("lp-mid");
|
| 361 |
+
const maxEl = document.getElementById("lp-max");
|
| 362 |
+
const bar = document.getElementById("legend-bar");
|
| 363 |
+
if (!lpRange) {
|
| 364 |
+
minEl.textContent = midEl.textContent = maxEl.textContent = "—";
|
| 365 |
+
bar.style.background = "linear-gradient(to right, #d83a2a, #888, #1a1a1a)";
|
| 366 |
+
return;
|
| 367 |
+
}
|
| 368 |
+
const { min, mid, max } = lpRange;
|
| 369 |
+
minEl.textContent = min.toFixed(1);
|
| 370 |
+
midEl.textContent = mid.toFixed(1);
|
| 371 |
+
maxEl.textContent = max.toFixed(1);
|
| 372 |
+
const midPct = max > min ? ((mid - min) / (max - min)) * 100 : 50;
|
| 373 |
+
bar.style.background = `linear-gradient(to right, #d83a2a 0%, #888 ${midPct.toFixed(1)}%, #1a1a1a 100%)`;
|
| 374 |
+
}
|
| 375 |
+
function logprobRgb(lp) {
|
| 376 |
+
if (lp == null || isNaN(lp) || !lpRange) return DARK_RGB;
|
| 377 |
+
const { min, mid, max } = lpRange;
|
| 378 |
+
if (max === min) return MID_RGB;
|
| 379 |
+
if (lp >= mid) {
|
| 380 |
+
const denom = max - mid;
|
| 381 |
+
const t = denom > 0 ? Math.min(1, Math.max(0, (max - lp) / denom)) : 0;
|
| 382 |
+
return lerpRgb(DARK_RGB, MID_RGB, t);
|
| 383 |
+
}
|
| 384 |
+
const denom = mid - min;
|
| 385 |
+
const t = denom > 0 ? Math.min(1, Math.max(0, (mid - lp) / denom)) : 0;
|
| 386 |
+
return lerpRgb(MID_RGB, RED_RGB, t);
|
| 387 |
+
}
|
| 388 |
+
|
| 389 |
+
// --- Init ---
|
| 390 |
+
async function init() {
|
| 391 |
+
try {
|
| 392 |
+
const resp = await fetch("/config");
|
| 393 |
+
const cfg = await resp.json();
|
| 394 |
+
els.meta.textContent = `${cfg.model} · ${cfg.endpoint}`;
|
| 395 |
+
} catch {
|
| 396 |
+
els.meta.textContent = "config unavailable";
|
| 397 |
+
}
|
| 398 |
+
}
|
| 399 |
+
|
| 400 |
+
// --- Auto-grow textarea ---
|
| 401 |
+
function autoGrow() {
|
| 402 |
+
els.prompt.style.height = "auto";
|
| 403 |
+
els.prompt.style.height = els.prompt.scrollHeight + "px";
|
| 404 |
+
}
|
| 405 |
+
els.prompt.addEventListener("input", () => {
|
| 406 |
+
const cleaned = cleanPrompt(els.prompt.value);
|
| 407 |
+
if (cleaned !== els.prompt.value) {
|
| 408 |
+
const pos = els.prompt.selectionStart;
|
| 409 |
+
els.prompt.value = cleaned;
|
| 410 |
+
els.prompt.setSelectionRange(pos, pos);
|
| 411 |
+
}
|
| 412 |
+
autoGrow();
|
| 413 |
+
});
|
| 414 |
+
window.addEventListener("load", autoGrow);
|
| 415 |
+
|
| 416 |
+
// --- Prompt validation ---
|
| 417 |
+
function cleanPrompt(s) {
|
| 418 |
+
return s.toUpperCase().replace(/[^ACGTN]/g, "");
|
| 419 |
+
}
|
| 420 |
+
|
| 421 |
+
// --- Sequence rendering ---
|
| 422 |
+
function rgbForBase(absIdx, base) {
|
| 423 |
+
// absIdx: 0-indexed position in prompt+gen string
|
| 424 |
+
if (absIdx < promptBases.length) return PROMPT_RGB;
|
| 425 |
+
if (colorMode === "bases") return BASE_RGB[base] || DARK_RGB;
|
| 426 |
+
if (colorMode === "logprob") {
|
| 427 |
+
const genIdx = absIdx - promptBases.length;
|
| 428 |
+
const tok = genTokens[genTokenAtBase[genIdx]];
|
| 429 |
+
return tok ? logprobRgb(tok.logprob) : DARK_RGB;
|
| 430 |
+
}
|
| 431 |
+
return DARK_RGB;
|
| 432 |
+
}
|
| 433 |
+
|
| 434 |
+
function renderSequence() {
|
| 435 |
+
if (colorMode === "logprob") recomputeLpRange();
|
| 436 |
+
const total = promptBases + genText;
|
| 437 |
+
if (!total) {
|
| 438 |
+
els.seq.classList.add("empty");
|
| 439 |
+
els.seq.textContent = "prompt + generated bases will stream here";
|
| 440 |
+
return;
|
| 441 |
+
}
|
| 442 |
+
els.seq.classList.remove("empty");
|
| 443 |
+
|
| 444 |
+
const lines = [];
|
| 445 |
+
for (let i = 0; i < total.length; i += 60) {
|
| 446 |
+
const lineBases = total.slice(i, i + 60);
|
| 447 |
+
const pos = String(i + 1).padStart(5, " ");
|
| 448 |
+
let html = `<span class="pos">${pos}</span> `;
|
| 449 |
+
for (let j = 0; j < lineBases.length; j++) {
|
| 450 |
+
if (j > 0 && j % 10 === 0) html += " ";
|
| 451 |
+
const absIdx = i + j;
|
| 452 |
+
const base = lineBases[j];
|
| 453 |
+
const [r, g, b] = rgbForBase(absIdx, base);
|
| 454 |
+
const tinted = colorMode !== "none" && absIdx >= promptBases.length;
|
| 455 |
+
const bg = tinted ? `;background:rgba(${r},${g},${b},${BG_ALPHA})` : "";
|
| 456 |
+
html += `<span style="color:rgb(${r},${g},${b})${bg}">${base}</span>`;
|
| 457 |
+
}
|
| 458 |
+
if (abortCtrl && i + lineBases.length === total.length) {
|
| 459 |
+
html += `<span class="cursor"></span>`;
|
| 460 |
+
}
|
| 461 |
+
lines.push(`<div class="seq-line">${html}</div>`);
|
| 462 |
+
}
|
| 463 |
+
els.seq.innerHTML = lines.join("");
|
| 464 |
+
}
|
| 465 |
+
|
| 466 |
+
// --- Stats ---
|
| 467 |
+
function gcContent(s) {
|
| 468 |
+
if (!s) return null;
|
| 469 |
+
let gc = 0;
|
| 470 |
+
for (const c of s) if (c === "G" || c === "C") gc++;
|
| 471 |
+
return (gc / s.length) * 100;
|
| 472 |
+
}
|
| 473 |
+
|
| 474 |
+
function meanLogprob() {
|
| 475 |
+
if (!genTokens.length) return null;
|
| 476 |
+
let sum = 0, n = 0;
|
| 477 |
+
for (const t of genTokens) {
|
| 478 |
+
if (t.logprob != null && !isNaN(t.logprob)) { sum += t.logprob; n++; }
|
| 479 |
+
}
|
| 480 |
+
return n ? sum / n : null;
|
| 481 |
+
}
|
| 482 |
+
|
| 483 |
+
function updateStats() {
|
| 484 |
+
els.statPrompt.innerHTML = `${promptBases.length}<span class="unit">bp</span>`;
|
| 485 |
+
els.statGen.innerHTML = `${genText.length}<span class="unit">bp</span>`;
|
| 486 |
+
els.statTok.textContent = genTokens.length;
|
| 487 |
+
const elapsed = startTime ? (performance.now() - startTime) / 1000 : 0;
|
| 488 |
+
els.statTime.innerHTML = `${elapsed.toFixed(1)}<span class="unit">s</span>`;
|
| 489 |
+
const rate = elapsed > 0 ? Math.round(genText.length / elapsed) : 0;
|
| 490 |
+
els.statRate.innerHTML = `${rate}<span class="unit">bp/s</span>`;
|
| 491 |
+
const gc = gcContent(genText);
|
| 492 |
+
els.statGc.textContent = gc == null ? "—" : `${gc.toFixed(1)}%`;
|
| 493 |
+
const mlp = meanLogprob();
|
| 494 |
+
els.statLp.textContent = mlp == null ? "—" : mlp.toFixed(2);
|
| 495 |
+
els.statPpl.textContent = mlp == null ? "—" : Math.exp(-mlp).toFixed(1);
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
function setStatus(text, mode = "") {
|
| 499 |
+
els.statusText.textContent = text;
|
| 500 |
+
els.status.className = "status" + (mode ? " " + mode : "");
|
| 501 |
+
}
|
| 502 |
+
|
| 503 |
+
// --- Color mode toggle ---
|
| 504 |
+
els.modeBtns.querySelectorAll(".mode-btn").forEach(b => {
|
| 505 |
+
b.addEventListener("click", () => {
|
| 506 |
+
colorMode = b.dataset.mode;
|
| 507 |
+
els.modeBtns.querySelectorAll(".mode-btn").forEach(x => x.classList.toggle("active", x === b));
|
| 508 |
+
els.legend.classList.toggle("show", colorMode === "logprob");
|
| 509 |
+
renderSequence();
|
| 510 |
+
});
|
| 511 |
+
});
|
| 512 |
+
|
| 513 |
+
// --- Generation ---
|
| 514 |
+
async function generate() {
|
| 515 |
+
if (abortCtrl) return;
|
| 516 |
+
|
| 517 |
+
promptBases = cleanPrompt(els.prompt.value);
|
| 518 |
+
genText = "";
|
| 519 |
+
genTokens = [];
|
| 520 |
+
genTokenAtBase = [];
|
| 521 |
+
startTime = performance.now();
|
| 522 |
+
abortCtrl = new AbortController();
|
| 523 |
+
|
| 524 |
+
els.generate.disabled = true;
|
| 525 |
+
els.stop.disabled = false;
|
| 526 |
+
setStatus("connecting…", "streaming");
|
| 527 |
+
renderSequence();
|
| 528 |
+
updateStats();
|
| 529 |
+
timer = setInterval(updateStats, 100);
|
| 530 |
+
|
| 531 |
+
const body = {
|
| 532 |
+
prompt: promptBases,
|
| 533 |
+
max_tokens: parseInt(els.maxTokens.value),
|
| 534 |
+
temperature: parseFloat(els.temperature.value),
|
| 535 |
+
top_p: parseFloat(els.topP.value),
|
| 536 |
+
};
|
| 537 |
+
|
| 538 |
+
try {
|
| 539 |
+
const resp = await fetch("/generate", {
|
| 540 |
+
method: "POST",
|
| 541 |
+
headers: { "Content-Type": "application/json" },
|
| 542 |
+
body: JSON.stringify(body),
|
| 543 |
+
signal: abortCtrl.signal,
|
| 544 |
+
});
|
| 545 |
+
|
| 546 |
+
if (!resp.ok) {
|
| 547 |
+
throw new Error(`HTTP ${resp.status}: ${await resp.text()}`);
|
| 548 |
+
}
|
| 549 |
+
|
| 550 |
+
setStatus("streaming", "streaming");
|
| 551 |
+
const reader = resp.body.getReader();
|
| 552 |
+
const decoder = new TextDecoder();
|
| 553 |
+
let buffer = "";
|
| 554 |
+
|
| 555 |
+
while (true) {
|
| 556 |
+
const { done, value } = await reader.read();
|
| 557 |
+
if (done) break;
|
| 558 |
+
buffer += decoder.decode(value, { stream: true });
|
| 559 |
+
const events = buffer.split("\n\n");
|
| 560 |
+
buffer = events.pop();
|
| 561 |
+
for (const ev of events) {
|
| 562 |
+
const line = ev.trim();
|
| 563 |
+
if (!line.startsWith("data:")) continue;
|
| 564 |
+
const data = JSON.parse(line.slice(5).trim());
|
| 565 |
+
if (data.error) throw new Error(data.error);
|
| 566 |
+
if (data.done) continue;
|
| 567 |
+
if (data.logprobs) {
|
| 568 |
+
const lp = data.logprobs;
|
| 569 |
+
for (let i = 0; i < lp.tokens.length; i++) {
|
| 570 |
+
const tokIdx = genTokens.length;
|
| 571 |
+
genTokens.push({
|
| 572 |
+
text: lp.tokens[i],
|
| 573 |
+
logprob: lp.token_logprobs[i],
|
| 574 |
+
top: lp.top_logprobs[i],
|
| 575 |
+
});
|
| 576 |
+
for (let j = 0; j < lp.tokens[i].length; j++) genTokenAtBase.push(tokIdx);
|
| 577 |
+
}
|
| 578 |
+
}
|
| 579 |
+
if (data.text) {
|
| 580 |
+
genText += cleanPrompt(data.text);
|
| 581 |
+
renderSequence();
|
| 582 |
+
}
|
| 583 |
+
}
|
| 584 |
+
}
|
| 585 |
+
setStatus("done");
|
| 586 |
+
} catch (e) {
|
| 587 |
+
if (e.name === "AbortError") setStatus("stopped");
|
| 588 |
+
else setStatus(e.message, "error");
|
| 589 |
+
} finally {
|
| 590 |
+
abortCtrl = null;
|
| 591 |
+
clearInterval(timer);
|
| 592 |
+
updateStats();
|
| 593 |
+
renderSequence();
|
| 594 |
+
els.generate.disabled = false;
|
| 595 |
+
els.stop.disabled = true;
|
| 596 |
+
}
|
| 597 |
+
}
|
| 598 |
+
|
| 599 |
+
function stop() { if (abortCtrl) abortCtrl.abort(); }
|
| 600 |
+
|
| 601 |
+
function clearAll() {
|
| 602 |
+
if (abortCtrl) return;
|
| 603 |
+
promptBases = "";
|
| 604 |
+
genText = "";
|
| 605 |
+
genTokens = [];
|
| 606 |
+
genTokenAtBase = [];
|
| 607 |
+
startTime = 0;
|
| 608 |
+
renderSequence();
|
| 609 |
+
updateStats();
|
| 610 |
+
setStatus("idle");
|
| 611 |
+
}
|
| 612 |
+
|
| 613 |
+
els.generate.addEventListener("click", generate);
|
| 614 |
+
els.stop.addEventListener("click", stop);
|
| 615 |
+
els.clear.addEventListener("click", clearAll);
|
| 616 |
+
|
| 617 |
+
els.prompt.addEventListener("keydown", e => {
|
| 618 |
+
if ((e.metaKey || e.ctrlKey) && e.key === "Enter") {
|
| 619 |
+
e.preventDefault();
|
| 620 |
+
generate();
|
| 621 |
+
}
|
| 622 |
+
});
|
| 623 |
+
|
| 624 |
+
document.querySelectorAll(".ex-btn").forEach(btn => {
|
| 625 |
+
btn.addEventListener("click", () => {
|
| 626 |
+
els.prompt.value = btn.dataset.ex;
|
| 627 |
+
autoGrow();
|
| 628 |
+
els.prompt.focus();
|
| 629 |
+
});
|
| 630 |
+
});
|
| 631 |
+
|
| 632 |
+
init();
|
| 633 |
+
updateStats();
|
| 634 |
+
</script>
|
| 635 |
+
</body>
|
| 636 |
+
</html>
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi>=0.110
|
| 2 |
+
uvicorn[standard]>=0.27
|
| 3 |
+
openai>=1.40
|
| 4 |
+
huggingface_hub>=0.24
|