diff --git a/demo/main.py b/demo/main.py index fb941fd..9501c9c 100644 --- a/demo/main.py +++ b/demo/main.py @@ -12,15 +12,28 @@ import gradio as gr import requests from dotenv import load_dotenv +from posthog import Posthog + +ph_client = None +if isinstance(os.getenv("POSTHOG_KEY"), str) and len(os.environ["POSTHOG_KEY"]) > 0: + ph_client = Posthog( + project_api_key=os.environ["POSTHOG_KEY"], host=os.getenv("POSTHOG_HOST", "https://eu.posthog.com") + ) class SessionManager: def __init__(self) -> None: self._token = "" self._url = "" + self.user_id = None def set_token(self, token: str) -> None: self._token = token + if ph_client is not None: + # Retrieve the user ID + self.user_id = requests.post(f"{self._url}/login/validate", timeout=2, headers=self.auth).json()["user_id"] + # Analytics + ph_client.capture(self.user_id, event="gradio-auth") def set_url(self, url: str) -> None: self._url = url @@ -71,6 +84,8 @@ def chat_response(message: str, history: List[List[str]]) -> str: for hist in history for idx, msg in enumerate(hist) ] + if ph_client is not None: + ph_client.capture(session_manager.user_id, event="gradio-chat") with session.post( session_manager.chat_endpoint, json={"messages": [*_history, {"role": "user", "content": message}]}, diff --git a/poetry.lock b/poetry.lock index 15ea90e..e48c4fa 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2884,4 +2884,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "fb508ef9a08a05ed01b2a9c724ca3ee9ec84c33b2c827d9789124a4d18ea328e" +content-hash = "7426d11a2b74473d1da9124900a727fdca97401d3ce6dfae645e1456fb0dde42" diff --git a/pyproject.toml b/pyproject.toml index 312feec..d691678 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,6 +46,7 @@ optional = true gradio = "^4.21.0" requests = "^2.20.0" python-dotenv = "^1.0.0" +posthog = "^3.0.0" [tool.poetry.group.test]