Skip to content
This repository has been archived by the owner on Oct 11, 2024. It is now read-only.

Commit

Permalink
feat(demo): add analytics to demo
Browse files Browse the repository at this point in the history
  • Loading branch information
frgfm committed Mar 29, 2024
1 parent c7fea64 commit 406bf60
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions demo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}]},
Expand Down
2 changes: 1 addition & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 406bf60

Please sign in to comment.