Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,84 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
"
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
"scatter_color",
|
| 86 |
-
None,
|
| 87 |
-
["none", "sex", "smoker", "day", "time"],
|
| 88 |
-
inline=True,
|
| 89 |
-
)
|
| 90 |
-
|
| 91 |
-
@render_plotly
|
| 92 |
-
def scatterplot():
|
| 93 |
-
color = input.scatter_color()
|
| 94 |
-
return px.scatter(
|
| 95 |
-
tips_data(),
|
| 96 |
-
x="total_bill",
|
| 97 |
-
y="tip",
|
| 98 |
-
color=None if color == "none" else color,
|
| 99 |
-
trendline="lowess",
|
| 100 |
-
)
|
| 101 |
-
|
| 102 |
-
with ui.card(full_screen=True):
|
| 103 |
-
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
| 104 |
-
"Tip percentages"
|
| 105 |
-
with ui.popover(title="Add a color variable"):
|
| 106 |
-
ICONS["ellipsis"]
|
| 107 |
-
ui.input_radio_buttons(
|
| 108 |
-
"tip_perc_y",
|
| 109 |
-
"Split by:",
|
| 110 |
-
["sex", "smoker", "day", "time"],
|
| 111 |
-
selected="day",
|
| 112 |
-
inline=True,
|
| 113 |
-
)
|
| 114 |
-
|
| 115 |
-
@render_plotly
|
| 116 |
-
def tip_perc():
|
| 117 |
-
from ridgeplot import ridgeplot
|
| 118 |
-
|
| 119 |
-
dat = tips_data()
|
| 120 |
-
dat["percent"] = dat.tip / dat.total_bill
|
| 121 |
-
yvar = input.tip_perc_y()
|
| 122 |
-
uvals = dat[yvar].unique()
|
| 123 |
-
|
| 124 |
-
samples = [[dat.percent[dat[yvar] == val]] for val in uvals]
|
| 125 |
-
|
| 126 |
-
plt = ridgeplot(
|
| 127 |
-
samples=samples,
|
| 128 |
-
labels=uvals,
|
| 129 |
-
bandwidth=0.01,
|
| 130 |
-
colorscale="viridis",
|
| 131 |
-
colormode="row-index",
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
plt.update_layout(
|
| 135 |
-
legend=dict(
|
| 136 |
-
orientation="h", yanchor="bottom", y=1.02, xanchor="center", x=0.5
|
| 137 |
-
)
|
| 138 |
-
)
|
| 139 |
-
|
| 140 |
-
return plt
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
ui.include_css(app_dir / "styles.css")
|
| 144 |
-
|
| 145 |
-
# --------------------------------------------------------
|
| 146 |
-
# Reactive calculations and effects
|
| 147 |
-
# --------------------------------------------------------
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
@reactive.calc
|
| 151 |
-
def tips_data():
|
| 152 |
-
bill = input.total_bill()
|
| 153 |
-
idx1 = tips.total_bill.between(bill[0], bill[1])
|
| 154 |
-
idx2 = tips.time.isin(input.time())
|
| 155 |
-
return tips[idx1 & idx2]
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
@reactive.effect
|
| 159 |
-
@reactive.event(input.reset)
|
| 160 |
-
def _():
|
| 161 |
-
ui.update_slider("total_bill", value=bill_rng)
|
| 162 |
-
ui.update_checkbox_group("time", selected=["Lunch", "Dinner"])
|
|
|
|
| 1 |
+
import os, logging, requests
|
| 2 |
+
from typing import List, Dict, Any
|
| 3 |
+
from fastapi import FastAPI
|
| 4 |
+
|
| 5 |
+
# Logging
|
| 6 |
+
logging.basicConfig(level=logging.INFO, format="%(levelname)s %(message)s")
|
| 7 |
+
log = logging.getLogger("replicate-catalog")
|
| 8 |
+
|
| 9 |
+
# Secrets
|
| 10 |
+
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN") or os.getenv("REPLICATE_TOKEN")
|
| 11 |
+
if not REPLICATE_API_TOKEN:
|
| 12 |
+
raise RuntimeError("Missing token: set Space secret REPLICATE_API_TOKEN (or REPLICATE_TOKEN).")
|
| 13 |
+
|
| 14 |
+
# HTTP setup
|
| 15 |
+
BASE = "https://api.replicate.com/v1"
|
| 16 |
+
HEADERS = {"Authorization": f"Token {REPLICATE_API_TOKEN}", "Accept": "application/json"}
|
| 17 |
+
TIMEOUT = 30
|
| 18 |
+
|
| 19 |
+
# State
|
| 20 |
+
last_summary: Dict[str, Any] = {"total": 0, "preview": []}
|
| 21 |
+
|
| 22 |
+
def fetch_full_catalog() -> List[Dict[str, Any]]:
|
| 23 |
+
"""Page through /models to collect the full public catalog."""
|
| 24 |
+
url = f"{BASE}/models"
|
| 25 |
+
items: List[Dict[str, Any]] = []
|
| 26 |
+
while url:
|
| 27 |
+
r = requests.get(url, headers=HEADERS, timeout=TIMEOUT)
|
| 28 |
+
if r.status_code != 200:
|
| 29 |
+
raise RuntimeError(f"Replicate API error {r.status_code}: {r.text[:500]}")
|
| 30 |
+
payload = r.json()
|
| 31 |
+
items.extend(payload.get("results", []))
|
| 32 |
+
url = payload.get("next") # absolute URL or None
|
| 33 |
+
return items
|
| 34 |
+
|
| 35 |
+
def summarize(items: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 36 |
+
"""Build a concise summary and preview for logs/endpoint."""
|
| 37 |
+
# Stable ID: "owner/name"
|
| 38 |
+
def mid(x: Dict[str, Any]) -> str:
|
| 39 |
+
owner = x.get("owner") or x.get("username") or ""
|
| 40 |
+
name = x.get("name") or ""
|
| 41 |
+
return f"{owner}/{name}".strip("/")
|
| 42 |
+
|
| 43 |
+
# Preview fields for UI sanity
|
| 44 |
+
preview = []
|
| 45 |
+
for x in items[:50]:
|
| 46 |
+
preview.append({
|
| 47 |
+
"id": mid(x),
|
| 48 |
+
"description": (x.get("description") or "")[:140],
|
| 49 |
+
"thumbnail": x.get("cover_image_url") or x.get("cover_image") or None,
|
| 50 |
+
"visibility": x.get("visibility"),
|
| 51 |
+
"tags": x.get("tags") or x.get("categories") or [],
|
| 52 |
+
})
|
| 53 |
+
|
| 54 |
+
return {"total": len(items), "preview": preview}
|
| 55 |
+
|
| 56 |
+
def refresh_catalog() -> Dict[str, Any]:
|
| 57 |
+
models = fetch_full_catalog()
|
| 58 |
+
summary = summarize(models)
|
| 59 |
+
# Logs for sanity
|
| 60 |
+
ids_first50 = [p["id"] for p in summary["preview"] if p.get("id")]
|
| 61 |
+
log.info("Replicate catalog total models: %d", summary["total"])
|
| 62 |
+
log.info("First 50 IDs: %s", ids_first50)
|
| 63 |
+
return summary
|
| 64 |
+
|
| 65 |
+
app = FastAPI()
|
| 66 |
+
|
| 67 |
+
@app.on_event("startup")
|
| 68 |
+
def _startup():
|
| 69 |
+
global last_summary
|
| 70 |
+
try:
|
| 71 |
+
last_summary = refresh_catalog()
|
| 72 |
+
except Exception as e:
|
| 73 |
+
log.error("Initial fetch failed: %s", e)
|
| 74 |
+
|
| 75 |
+
@app.get("/")
|
| 76 |
+
def root():
|
| 77 |
+
# Health + current summary
|
| 78 |
+
return {"ok": True, **last_summary}
|
| 79 |
+
|
| 80 |
+
@app.post("/refresh")
|
| 81 |
+
def refresh():
|
| 82 |
+
global last_summary
|
| 83 |
+
last_summary = refresh_catalog()
|
| 84 |
+
return {"ok": True, **last_summary}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|