Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,89 +1,119 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import json
|
| 3 |
from Data_Fetching_and_Rendering import fetch_and_render_dashboard
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
token_received = {"status": False, "token":
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
def receive_token(accessToken:
|
|
|
|
|
|
|
|
|
|
| 10 |
try:
|
| 11 |
-
token_dict = json.loads(accessToken.replace("'", '"'))
|
| 12 |
except json.JSONDecodeError as e:
|
| 13 |
-
print("Failed to parse accessToken:", e)
|
| 14 |
return {
|
| 15 |
"status": "β Invalid token format",
|
| 16 |
"token": "",
|
| 17 |
"client_id": client_id
|
| 18 |
}
|
| 19 |
-
|
| 20 |
token_received["status"] = True
|
| 21 |
token_received["token"] = token_dict
|
| 22 |
token_received["client_id"] = client_id
|
| 23 |
return {
|
| 24 |
-
"status": "β
|
| 25 |
"token": token_dict.get("access_token", ""),
|
| 26 |
"client_id": client_id
|
| 27 |
}
|
| 28 |
|
| 29 |
-
# 2) poll for status
|
| 30 |
def check_status():
|
| 31 |
-
return "β
|
| 32 |
|
| 33 |
def show_token():
|
| 34 |
-
return token_received["token"] if token_received["status"] else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
def show_client_id():
|
| 37 |
-
return token_received["client_id"] if token_received["status"] else ""
|
| 38 |
|
| 39 |
-
|
| 40 |
-
def fetch_and_render():
|
| 41 |
if not token_received["status"]:
|
| 42 |
-
return
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
# Wire hidden POST handler
|
| 66 |
hidden_btn.click(
|
| 67 |
fn=receive_token,
|
| 68 |
-
inputs=[hidden_token,
|
| 69 |
outputs=[status_box, token_display, client_display]
|
| 70 |
)
|
| 71 |
|
| 72 |
-
|
| 73 |
-
gr.Button("Refresh Status").click(fn=check_status, outputs=status_box)
|
| 74 |
-
gr.Button("Reset").click(
|
| 75 |
-
fn=reset_status,
|
| 76 |
-
outputs=[status_box, token_display, client_display, urn_display, name_display]
|
| 77 |
-
)
|
| 78 |
-
|
| 79 |
-
# Timer triggers polling and fetch
|
| 80 |
-
# Timer triggers polling and fetch
|
| 81 |
timer = gr.Timer(1.0)
|
| 82 |
timer.tick(fn=check_status, outputs=status_box)
|
| 83 |
-
timer.tick(fn=show_token, outputs=token_display)
|
| 84 |
-
timer.tick(fn=
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
| 1 |
+
# -*- coding: utf-8 -*-
|
| 2 |
import gradio as gr
|
| 3 |
import json
|
| 4 |
from Data_Fetching_and_Rendering import fetch_and_render_dashboard
|
| 5 |
+
from analytics_fetch_and_rendering import fetch_and_render_analytics
|
| 6 |
|
| 7 |
+
# Shared state
|
| 8 |
+
token_received = {"status": False, "token": None, "client_id": None}
|
| 9 |
|
| 10 |
+
# --- Handlers for token reception and status ---
|
| 11 |
+
def receive_token(accessToken: str, client_id: str):
|
| 12 |
+
"""
|
| 13 |
+
Called by a hidden POST mechanism to supply the OAuth code/token and client ID.
|
| 14 |
+
"""
|
| 15 |
try:
|
| 16 |
+
token_dict = json.loads(accessToken.replace("'", '"'))
|
| 17 |
except json.JSONDecodeError as e:
|
|
|
|
| 18 |
return {
|
| 19 |
"status": "β Invalid token format",
|
| 20 |
"token": "",
|
| 21 |
"client_id": client_id
|
| 22 |
}
|
| 23 |
+
|
| 24 |
token_received["status"] = True
|
| 25 |
token_received["token"] = token_dict
|
| 26 |
token_received["client_id"] = client_id
|
| 27 |
return {
|
| 28 |
+
"status": "β
Token received",
|
| 29 |
"token": token_dict.get("access_token", ""),
|
| 30 |
"client_id": client_id
|
| 31 |
}
|
| 32 |
|
|
|
|
| 33 |
def check_status():
|
| 34 |
+
return "β
Token received" if token_received["status"] else "β Waiting for tokenβ¦"
|
| 35 |
|
| 36 |
def show_token():
|
| 37 |
+
return token_received["token"].get("access_token", "") if token_received["status"] else ""
|
| 38 |
+
|
| 39 |
+
def show_client():
|
| 40 |
+
return token_received["client_id"] or "" if token_received["status"] else ""
|
| 41 |
+
|
| 42 |
+
# --- Guarded fetch functions ---
|
| 43 |
+
def guarded_fetch_dashboard():
|
| 44 |
+
if not token_received["status"]:
|
| 45 |
+
return "<p style='color:red; text-align:center;'>β Access denied. No token available. Please send token first.</p>"
|
| 46 |
+
# token_received["client_id"] and token_received["token"] required by fetch function
|
| 47 |
+
html = fetch_and_render_dashboard(
|
| 48 |
+
token_received["client_id"],
|
| 49 |
+
token_received["token"]
|
| 50 |
+
)
|
| 51 |
+
return html
|
| 52 |
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
def guarded_fetch_analytics():
|
|
|
|
| 55 |
if not token_received["status"]:
|
| 56 |
+
# return placeholder for both markdown and plot
|
| 57 |
+
return ("<p style='color:red; text-align:center;'>β Access denied. No token available.</p>", None)
|
| 58 |
+
count_md, plot = fetch_and_render_analytics(
|
| 59 |
+
token_received["client_id"],
|
| 60 |
+
token_received["token"]
|
| 61 |
+
)
|
| 62 |
+
return count_md, plot
|
| 63 |
+
|
| 64 |
+
# --- Build the Gradio UI ---
|
| 65 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
|
| 66 |
+
title="LinkedIn Post Viewer & Analytics") as app:
|
| 67 |
+
gr.Markdown("# π LinkedIn Organization Post Viewer & Analytics")
|
| 68 |
+
gr.Markdown("Send your OAuth token via API call, then explore dashboard and analytics.")
|
| 69 |
+
|
| 70 |
+
# Hidden elements: simulate POST endpoint
|
| 71 |
+
hidden_token = gr.Textbox(visible=False, elem_id="hidden_token")
|
| 72 |
+
hidden_client = gr.Textbox(visible=False, elem_id="hidden_client_id")
|
| 73 |
+
hidden_btn = gr.Button(visible=False, elem_id="hidden_btn")
|
| 74 |
+
|
| 75 |
+
status_box = gr.Textbox(value=check_status(), label="Status", interactive=False)
|
| 76 |
+
token_display = gr.Textbox(value=show_token(), label="Access Token", interactive=False)
|
| 77 |
+
client_display = gr.Textbox(value=show_client(), label="Client ID", interactive=False)
|
| 78 |
|
| 79 |
# Wire hidden POST handler
|
| 80 |
hidden_btn.click(
|
| 81 |
fn=receive_token,
|
| 82 |
+
inputs=[hidden_token, hidden_client],
|
| 83 |
outputs=[status_box, token_display, client_display]
|
| 84 |
)
|
| 85 |
|
| 86 |
+
# Polling timer to update status and displays
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
timer = gr.Timer(1.0)
|
| 88 |
timer.tick(fn=check_status, outputs=status_box)
|
| 89 |
+
timer.tick(fn=show_token, outputs=token_display)
|
| 90 |
+
timer.tick(fn=show_client, outputs=client_display)
|
| 91 |
+
|
| 92 |
+
# Tabs for functionality
|
| 93 |
+
with gr.Tabs():
|
| 94 |
+
with gr.TabItem("1οΈβ£ Dashboard"):
|
| 95 |
+
gr.Markdown("View your organization's recent posts and their engagement statistics.")
|
| 96 |
+
fetch_dashboard_btn = gr.Button("π Fetch Posts & Stats", variant="primary")
|
| 97 |
+
dashboard_html = gr.HTML(value="<p style='text-align: center; color: #555;'>Waiting for token...</p>")
|
| 98 |
+
fetch_dashboard_btn.click(
|
| 99 |
+
fn=guarded_fetch_dashboard,
|
| 100 |
+
inputs=[],
|
| 101 |
+
outputs=[dashboard_html]
|
| 102 |
+
)
|
| 103 |
|
| 104 |
+
with gr.TabItem("2οΈβ£ Analytics"):
|
| 105 |
+
gr.Markdown("View follower count and monthly gains for your organization.")
|
| 106 |
+
fetch_analytics_btn = gr.Button("π Fetch Follower Analytics", variant="primary")
|
| 107 |
+
follower_count = gr.Markdown(
|
| 108 |
+
"<p style='text-align: center; color: #555;'>Waiting for token...</p>"
|
| 109 |
+
)
|
| 110 |
+
follower_plot = gr.Plot(visible=False)
|
| 111 |
+
fetch_analytics_btn.click(
|
| 112 |
+
fn=guarded_fetch_analytics,
|
| 113 |
+
inputs=[],
|
| 114 |
+
outputs=[follower_count, follower_plot]
|
| 115 |
+
)
|
| 116 |
|
| 117 |
+
# Launch the app
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|