radames commited on
Commit
dbc6ff9
1 Parent(s): d34f2f6

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ from fastapi import FastAPI, Request
4
+ import uvicorn
5
+ from pathlib import Path
6
+ import os
7
+
8
+ HF_TOKEN = os.environ.get("HF_TOKEN")
9
+ FLAG_DIR = Path("./flagged_data")
10
+ DATASET_NAME = "gradio_clicks_dataset"
11
+
12
+ # setup logger
13
+ # remote logging to HuggingFace
14
+ remote = True
15
+ logger = gr.CSVLogger()
16
+ if remote:
17
+ logger = gr.HuggingFaceDatasetSaver(
18
+ HF_TOKEN, dataset_name=DATASET_NAME, organization=None, private=False)
19
+ logger.setup([gr.Text(label="URL"), gr.Text(label="Host")], FLAG_DIR)
20
+
21
+ # dummy data
22
+ websites = [
23
+ ["https://www.google.com/"],
24
+ ["https://www.youtube.com/"],
25
+ ["https://www.facebook.com/"],
26
+ ["https://www.wikipedia.org/"],
27
+ ["https://www.amazon.com/"],
28
+ ["https://www.yahoo.com/"],
29
+ ["https://www.twitter.com/"],
30
+ ["https://www.instagram.com/"],
31
+ ["https://www.reddit.com/"],
32
+ ["https://www.linkedin.com/"],
33
+ ["https://www.netflix.com/"],
34
+ ["https://www.microsoft.com/"],
35
+ ["https://www.apple.com/"],
36
+ ["https://www.zoom.us/"],
37
+ ["https://www.gmail.com/"],
38
+ ["https://www.dropbox.com/"],
39
+ ["https://www.github.com/"],
40
+ ["https://www.stackoverflow.com/"],
41
+ ["https://www.medium.com/"],
42
+ ["https://www.quora.com/"],
43
+ ]
44
+
45
+ # add simeple get request tracking
46
+ websites = [
47
+ [f"<a href={x[0]} target='_blank' onclick='fetch(\"/track?url={x[0]}\")'>{x[0]}</a>"] for x in websites]
48
+
49
+ df = pd.DataFrame(websites, columns=['img_code'])
50
+
51
+ df_html = df.to_html(escape=False, render_links=False,
52
+ index=False, header=False)
53
+
54
+ # create a FastAPI app
55
+ app = FastAPI()
56
+
57
+ # gradio app
58
+
59
+
60
+ def refresh():
61
+ df = pd.read_csv(FLAG_DIR / DATASET_NAME / "data.csv")
62
+ url_counts = df.groupby('URL').count()['Host']
63
+ normalized_counts = url_counts / url_counts.sum()
64
+ return normalized_counts.to_dict()
65
+
66
+
67
+ with gr.Blocks() as block:
68
+ gr.Markdown("""
69
+ ## Gradio Tracking Clicks + FastAPI + HuggingFace Datasets
70
+ This is a demo of how to track clicks on a Gradio app using FastAPI and HuggingFace Datasets.
71
+ Each click sends a request to the FastAPI server, which logs the click to a HuggingFace dataset.
72
+ """)
73
+ with gr.Row():
74
+ with gr.Column():
75
+ refresh_bt = gr.Button("Refresh")
76
+ gr.HTML(df_html)
77
+ with gr.Column():
78
+ labels = gr.Label()
79
+ refresh_bt.click(fn=refresh, inputs=[], outputs=[labels])
80
+ block.load(fn=refresh, inputs=[], outputs=[labels])
81
+ # custom get request handler to flag clicks
82
+
83
+
84
+ @ app.get("/track")
85
+ async def track(url: str, request: Request):
86
+ # host disable for privacy reasons
87
+ # host = request.headers.get("host")
88
+ logger.flag([url, "ip"])
89
+ return {"message": "ok"}
90
+
91
+ # mount Gradio app to FastAPI app
92
+ app = gr.mount_gradio_app(app, block, path="/")
93
+
94
+ # serve the app
95
+ if __name__ == "__main__":
96
+ uvicorn.run(app, host="0.0.0.0", port=7860)