Spaces:
Sleeping
Sleeping
feat:hehe
Browse files- Dockerfile +9 -4
- requirements.txt +1 -0
- start.sh +29 -0
- ui.py +123 -0
Dockerfile
CHANGED
|
@@ -15,13 +15,18 @@ RUN rm -rf /var/lib/apt/lists/* \
|
|
| 15 |
git git-lfs ca-certificates \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
|
| 22 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 23 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
| 15 |
git git-lfs ca-certificates \
|
| 16 |
&& rm -rf /var/lib/apt/lists/*
|
| 17 |
|
| 18 |
+
RUN apt-get update && apt-get install -y --no-install-recommends tini && rm -rf /var/lib/apt/lists/*
|
| 19 |
|
| 20 |
+
WORKDIR /app
|
| 21 |
|
| 22 |
COPY --chown=user ./requirements.txt requirements.txt
|
| 23 |
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 24 |
|
| 25 |
+
# Bring over your existing FastAPI + util, plus Streamlit UI and launcher
|
| 26 |
+
COPY util.py app.py ui.py start.sh /app/
|
| 27 |
+
|
| 28 |
+
ENV PORT=7860
|
| 29 |
+
EXPOSE 7860
|
| 30 |
|
| 31 |
+
ENTRYPOINT ["/usr/bin/tini","--"]
|
| 32 |
+
CMD ["bash","/app/start.sh"]
|
requirements.txt
CHANGED
|
@@ -18,6 +18,7 @@ icecream
|
|
| 18 |
einops
|
| 19 |
Pillow
|
| 20 |
gradio
|
|
|
|
| 21 |
xformers==0.0.27.post2
|
| 22 |
spconv-cu120==2.3.6
|
| 23 |
transformers==4.46.3
|
|
|
|
| 18 |
einops
|
| 19 |
Pillow
|
| 20 |
gradio
|
| 21 |
+
streamlit
|
| 22 |
xformers==0.0.27.post2
|
| 23 |
spconv-cu120==2.3.6
|
| 24 |
transformers==4.46.3
|
start.sh
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env bash
|
| 2 |
+
set -euo pipefail
|
| 3 |
+
|
| 4 |
+
# ---- Make all caches writable in Spaces (avoid /.cache) ----
|
| 5 |
+
export HF_CACHE_DIR="${HF_CACHE_DIR:-/tmp/hf-cache}"
|
| 6 |
+
mkdir -p "$HF_CACHE_DIR" /tmp/pycache
|
| 7 |
+
export HOME=/tmp
|
| 8 |
+
export XDG_CACHE_HOME="$HF_CACHE_DIR"
|
| 9 |
+
export HF_HOME="$HF_CACHE_DIR"
|
| 10 |
+
export HUGGINGFACE_HUB_CACHE="$HF_CACHE_DIR"
|
| 11 |
+
export TRANSFORMERS_CACHE="$HF_CACHE_DIR"
|
| 12 |
+
export HF_DATASETS_CACHE="$HF_CACHE_DIR/datasets"
|
| 13 |
+
export TORCH_HOME="$HF_CACHE_DIR"
|
| 14 |
+
export PYTHONPYCACHEPREFIX=/tmp/pycache
|
| 15 |
+
|
| 16 |
+
# ---- Disable FlashAttention by default (safe on T4/L4) ----
|
| 17 |
+
export SMOLVLM_ATTN="${SMOLVLM_ATTN:-sdpa}"
|
| 18 |
+
|
| 19 |
+
# ---- Start FastAPI (internal) ----
|
| 20 |
+
uvicorn app:app --host 0.0.0.0 --port 8000 --no-server-header --forwarded-allow-ips="*" &
|
| 21 |
+
|
| 22 |
+
# ---- Start Streamlit (public) ----
|
| 23 |
+
exec streamlit run ui.py \
|
| 24 |
+
--server.address=0.0.0.0 \
|
| 25 |
+
--server.port="${PORT:-7860}" \
|
| 26 |
+
--server.headless=true \
|
| 27 |
+
--server.enableCORS=false \
|
| 28 |
+
--server.enableXsrfProtection=false
|
| 29 |
+
|
ui.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# ui.py
|
| 2 |
+
import os
|
| 3 |
+
import io
|
| 4 |
+
import json
|
| 5 |
+
import requests
|
| 6 |
+
import streamlit as st
|
| 7 |
+
from PIL import Image
|
| 8 |
+
|
| 9 |
+
st.set_page_config(page_title="SmolVLM UI", layout="wide")
|
| 10 |
+
st.title("SmolVLM")
|
| 11 |
+
|
| 12 |
+
API_BASE = os.getenv("API_BASE", "http://127.0.0.1:8000")
|
| 13 |
+
|
| 14 |
+
with st.sidebar:
|
| 15 |
+
st.subheader("Generation settings")
|
| 16 |
+
max_new_tokens = st.slider("max_new_tokens", 1, 1024, 300, step=1)
|
| 17 |
+
temperature_on = st.toggle("Set temperature?", value=False)
|
| 18 |
+
temperature = st.slider("temperature", 0.0, 2.0, 0.2, step=0.05) if temperature_on else None
|
| 19 |
+
topp_on = st.toggle("Set top_p?", value=False)
|
| 20 |
+
top_p = st.slider("top_p", 0.05, 1.0, 0.95, step=0.05) if topp_on else None
|
| 21 |
+
st.caption("API base: " + API_BASE)
|
| 22 |
+
|
| 23 |
+
tabs = st.tabs(["Upload images", "Image URLs"])
|
| 24 |
+
prompt = st.text_area("Prompt", "Can you describe the image(s)?", height=80)
|
| 25 |
+
|
| 26 |
+
def show_metrics(metrics: dict):
|
| 27 |
+
if not metrics:
|
| 28 |
+
return
|
| 29 |
+
info = metrics
|
| 30 |
+
cols = st.columns(4)
|
| 31 |
+
tt = info.get("timings_ms", {}).get("total")
|
| 32 |
+
it = info.get("timings_ms", {}).get("inference")
|
| 33 |
+
tps = info.get("throughput", {}).get("tokens_per_sec_inference")
|
| 34 |
+
vram = info.get("gpu_memory_mb", {}).get("max_reserved")
|
| 35 |
+
cols[0].metric("Total (ms)", f"{tt:.0f}" if tt is not None else "—")
|
| 36 |
+
cols[1].metric("Inference (ms)", f"{it:.0f}" if it is not None else "—")
|
| 37 |
+
cols[2].metric("Tok/s (infer)", f"{tps:.1f}" if tps is not None else "—")
|
| 38 |
+
cols[3].metric("GPU reserved (MB)", f"{vram:.0f}" if vram is not None else "—")
|
| 39 |
+
st.expander("All metrics").json(info)
|
| 40 |
+
|
| 41 |
+
with tabs[0]:
|
| 42 |
+
st.subheader("Upload one or more images")
|
| 43 |
+
files = st.file_uploader("Images", type=["png", "jpg", "jpeg", "webp"], accept_multiple_files=True)
|
| 44 |
+
run = st.button("Generate from uploads", type="primary", use_container_width=True, key="run_files")
|
| 45 |
+
|
| 46 |
+
if run:
|
| 47 |
+
if not files or not prompt.strip():
|
| 48 |
+
st.error("Please add at least one image and a prompt.")
|
| 49 |
+
else:
|
| 50 |
+
with st.spinner("Calling FastAPI…"):
|
| 51 |
+
data = {
|
| 52 |
+
"prompt": prompt,
|
| 53 |
+
"max_new_tokens": str(max_new_tokens), # form fields are strings
|
| 54 |
+
}
|
| 55 |
+
if temperature is not None:
|
| 56 |
+
data["temperature"] = str(temperature)
|
| 57 |
+
if top_p is not None:
|
| 58 |
+
data["top_p"] = str(top_p)
|
| 59 |
+
|
| 60 |
+
multipart = []
|
| 61 |
+
previews = []
|
| 62 |
+
for f in files:
|
| 63 |
+
content = f.read()
|
| 64 |
+
multipart.append(("images", (f.name, content, f.type or "application/octet-stream")))
|
| 65 |
+
try:
|
| 66 |
+
previews.append(Image.open(io.BytesIO(content)))
|
| 67 |
+
except Exception:
|
| 68 |
+
pass
|
| 69 |
+
|
| 70 |
+
try:
|
| 71 |
+
r = requests.post(f"{API_BASE}/generate", data=data, files=multipart, timeout=300)
|
| 72 |
+
r.raise_for_status()
|
| 73 |
+
out = r.json()
|
| 74 |
+
st.success("Done!")
|
| 75 |
+
if previews:
|
| 76 |
+
st.image(previews, caption=[f.name for f in files], use_column_width=True)
|
| 77 |
+
st.subheader("Answer")
|
| 78 |
+
st.write(out.get("text", ""))
|
| 79 |
+
show_metrics(out.get("metrics", {}))
|
| 80 |
+
except requests.RequestException as e:
|
| 81 |
+
st.error(f"Request failed: {e}")
|
| 82 |
+
if hasattr(e, "response") and e.response is not None:
|
| 83 |
+
try:
|
| 84 |
+
st.code(e.response.text, language="json")
|
| 85 |
+
except Exception:
|
| 86 |
+
st.write(e.response.text)
|
| 87 |
+
|
| 88 |
+
with tabs[1]:
|
| 89 |
+
st.subheader("Use remote image URLs")
|
| 90 |
+
urls_raw = st.text_area("One URL per line", "", height=120, placeholder="https://example.com/a.jpg\nhttps://example.com/b.png")
|
| 91 |
+
run2 = st.button("Generate from URLs", type="primary", use_container_width=True, key="run_urls")
|
| 92 |
+
|
| 93 |
+
if run2:
|
| 94 |
+
urls = [u.strip() for u in urls_raw.splitlines() if u.strip()]
|
| 95 |
+
if not urls or not prompt.strip():
|
| 96 |
+
st.error("Please add at least one URL and a prompt.")
|
| 97 |
+
else:
|
| 98 |
+
with st.spinner("Calling FastAPI…"):
|
| 99 |
+
body = {
|
| 100 |
+
"prompt": prompt,
|
| 101 |
+
"image_urls": urls,
|
| 102 |
+
"max_new_tokens": max_new_tokens,
|
| 103 |
+
"temperature": temperature, # FastAPI model allows null
|
| 104 |
+
"top_p": top_p,
|
| 105 |
+
}
|
| 106 |
+
try:
|
| 107 |
+
r = requests.post(f"{API_BASE}/generate_urls", json=body, timeout=300)
|
| 108 |
+
r.raise_for_status()
|
| 109 |
+
out = r.json()
|
| 110 |
+
st.success("Done!")
|
| 111 |
+
st.subheader("Answer")
|
| 112 |
+
st.write(out.get("text", ""))
|
| 113 |
+
show_metrics(out.get("metrics", {}))
|
| 114 |
+
st.caption("Fetched URLs:")
|
| 115 |
+
st.code(json.dumps(urls, indent=2))
|
| 116 |
+
except requests.RequestException as e:
|
| 117 |
+
st.error(f"Request failed: {e}")
|
| 118 |
+
if hasattr(e, "response") and e.response is not None:
|
| 119 |
+
try:
|
| 120 |
+
st.code(e.response.text, language="json")
|
| 121 |
+
except Exception:
|
| 122 |
+
st.write(e.response.text)
|
| 123 |
+
|