Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,72 @@
|
|
| 1 |
-
import
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
response = ""
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
response += token
|
| 40 |
-
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi import FastAPI, Request, HTTPException
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
import sqlite3, os, datetime
|
| 6 |
+
from jinja2 import Template
|
| 7 |
|
| 8 |
|
| 9 |
+
AUTH_TOKEN = os.environ.get("AUTH_TOKEN", "CHANGE_ME_SECRET")
|
| 10 |
+
DB_PATH = "data.db"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
|
|
|
| 12 |
|
| 13 |
+
app = FastAPI()
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"],
|
| 17 |
+
allow_credentials=False,
|
| 18 |
+
allow_methods=["*"],
|
| 19 |
+
allow_headers=["*"]
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
|
| 23 |
+
# ----- DB -----
|
| 24 |
+
conn = sqlite3.connect(DB_PATH, check_same_thread=False)
|
| 25 |
+
cur = conn.cursor()
|
| 26 |
+
cur.execute(
|
| 27 |
+
"""
|
| 28 |
+
CREATE TABLE IF NOT EXISTS events (
|
| 29 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 30 |
+
ts TEXT,
|
| 31 |
+
email TEXT,
|
| 32 |
+
gaia_id TEXT,
|
| 33 |
+
device_id TEXT,
|
| 34 |
+
os TEXT,
|
| 35 |
+
chrome_version TEXT,
|
| 36 |
+
event TEXT
|
| 37 |
+
)
|
| 38 |
+
"""
|
| 39 |
+
)
|
| 40 |
+
conn.commit()
|
| 41 |
|
|
|
|
| 42 |
|
| 43 |
+
class Event(BaseModel):
|
| 44 |
+
timestamp: str
|
| 45 |
+
email: str | None = None
|
| 46 |
+
gaia_id: str | None = None
|
| 47 |
+
device_id: str
|
| 48 |
+
os: str | None = None
|
| 49 |
+
chrome_version: str | None = None
|
| 50 |
+
event: str | None = None
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# ----- Ingest endpoint -----
|
| 54 |
+
@app.post("/ingest")
|
| 55 |
+
async def ingest(req: Request, ev: Event):
|
| 56 |
+
auth = req.headers.get("authorization", "")
|
| 57 |
+
if not auth.startswith("Bearer ") or auth.split(" ", 1)[1] != AUTH_TOKEN:
|
| 58 |
+
raise HTTPException(status_code=401, detail="Unauthorized")
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
# Validate timestamp is ISO-ish; if bad, replace with server time
|
| 62 |
+
try:
|
| 63 |
+
datetime.datetime.fromisoformat(ev.timestamp.replace("Z", "+00:00"))
|
| 64 |
+
except Exception:
|
| 65 |
+
ev.timestamp = datetime.datetime.utcnow().isoformat() + "Z"
|
| 66 |
|
| 67 |
|
| 68 |
+
cur.execute(
|
| 69 |
+
"INSERT INTO events (ts, email, gaia_id, device_id, os, chrome_version, event) VALUES (?,?,?,?,?,?,?)",
|
| 70 |
+
(ev.timestamp, ev.email, ev.gaia_id, ev.device_id, ev.os, ev.chrome_version, ev.event)
|
| 71 |
+
)
|
| 72 |
+
now=datetime.datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S") + "Z"
|