Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,162 +1,71 @@
|
|
| 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 |
-
with ui.card(full_screen=True):
|
| 73 |
-
ui.card_header("Tips data")
|
| 74 |
-
|
| 75 |
-
@render.data_frame
|
| 76 |
-
def table():
|
| 77 |
-
return render.DataGrid(tips_data())
|
| 78 |
-
|
| 79 |
-
with ui.card(full_screen=True):
|
| 80 |
-
with ui.card_header(class_="d-flex justify-content-between align-items-center"):
|
| 81 |
-
"Total bill vs tip"
|
| 82 |
-
with ui.popover(title="Add a color variable", placement="top"):
|
| 83 |
-
ICONS["ellipsis"]
|
| 84 |
-
ui.input_radio_buttons(
|
| 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, asyncio, httpx, websockets
|
| 2 |
+
from fastapi import FastAPI, Request, WebSocket, WebSocketDisconnect
|
| 3 |
+
from fastapi.responses import StreamingResponse, PlainTextResponse
|
| 4 |
+
|
| 5 |
+
UPSTREAM = os.getenv("UPSTREAM", "https://cjerzak-policyview.hf.space").rstrip("/")
|
| 6 |
+
TOKEN = os.getenv("HF_TOKEN")
|
| 7 |
+
if not TOKEN:
|
| 8 |
+
raise RuntimeError("Set HF_TOKEN as a Secret in the Space settings.")
|
| 9 |
+
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
@app.get("/healthz")
|
| 13 |
+
def healthz():
|
| 14 |
+
return PlainTextResponse("ok")
|
| 15 |
+
|
| 16 |
+
async def stream_bytes(resp):
|
| 17 |
+
async for chunk in resp.aiter_bytes():
|
| 18 |
+
yield chunk
|
| 19 |
+
|
| 20 |
+
@app.api_route("/{path:path}", methods=["GET","POST","PUT","PATCH","DELETE","OPTIONS"])
|
| 21 |
+
async def proxy(request: Request, path: str):
|
| 22 |
+
url = f"{UPSTREAM}/{path}"
|
| 23 |
+
if request.url.query:
|
| 24 |
+
url += f"?{request.url.query}"
|
| 25 |
+
headers = {k: v for k, v in request.headers.items()
|
| 26 |
+
if k.lower() not in ("host", "content-length", "authorization")}
|
| 27 |
+
headers["Authorization"] = f"Bearer {TOKEN}"
|
| 28 |
+
headers["x-forwarded-host"] = request.headers.get("host", "")
|
| 29 |
+
headers["x-forwarded-proto"] = request.url.scheme
|
| 30 |
+
async with httpx.AsyncClient(follow_redirects=False, timeout=httpx.Timeout(60.0, connect=60.0)) as client:
|
| 31 |
+
upstream = await client.request(
|
| 32 |
+
request.method, url, headers=headers, content=await request.body(), stream=True
|
| 33 |
+
)
|
| 34 |
+
# Strip hop-by-hop headers and rewrite redirects back through this proxy
|
| 35 |
+
drop = {"content-length","transfer-encoding","connection","keep-alive",
|
| 36 |
+
"proxy-authenticate","proxy-authorization","te","trailers","upgrade","set-cookie"}
|
| 37 |
+
out_headers = {k: v for k, v in upstream.headers.items() if k.lower() not in drop}
|
| 38 |
+
loc = upstream.headers.get("location")
|
| 39 |
+
if loc and loc.startswith(UPSTREAM):
|
| 40 |
+
out_headers["location"] = loc.replace(UPSTREAM, "")
|
| 41 |
+
return StreamingResponse(stream_bytes(upstream), status_code=upstream.status_code, headers=out_headers)
|
| 42 |
+
|
| 43 |
+
@app.websocket("/{path:path}")
|
| 44 |
+
async def ws_proxy(ws: WebSocket, path: str):
|
| 45 |
+
await ws.accept()
|
| 46 |
+
target = f"{UPSTREAM}/{path}"
|
| 47 |
+
if ws.url.query:
|
| 48 |
+
target += f"?{ws.url.query}"
|
| 49 |
+
ws_headers = [("Authorization", f"Bearer {TOKEN}")]
|
| 50 |
+
# Upgrade to wss for https upstream
|
| 51 |
+
target = target.replace("https://", "wss://").replace("http://", "ws://")
|
| 52 |
+
try:
|
| 53 |
+
async with websockets.connect(target, extra_headers=ws_headers, origin=UPSTREAM) as ups:
|
| 54 |
+
async def client_to_up():
|
| 55 |
+
while True:
|
| 56 |
+
msg = await ws.receive()
|
| 57 |
+
data = msg.get("text") if "text" in msg else msg.get("bytes")
|
| 58 |
+
if data is None: break
|
| 59 |
+
await ups.send(data)
|
| 60 |
+
async def up_to_client():
|
| 61 |
+
while True:
|
| 62 |
+
data = await ups.recv()
|
| 63 |
+
if isinstance(data, (bytes, bytearray)):
|
| 64 |
+
await ws.send_bytes(data)
|
| 65 |
+
else:
|
| 66 |
+
await ws.send_text(data)
|
| 67 |
+
await asyncio.gather(client_to_up(), up_to_client())
|
| 68 |
+
except WebSocketDisconnect:
|
| 69 |
+
pass
|
| 70 |
+
except Exception:
|
| 71 |
+
await ws.close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|