Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,192 +1,123 @@
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
-
import os
|
5 |
|
6 |
-
# Assuming these custom modules exist in your project directory or Python path
|
7 |
from Data_Fetching_and_Rendering import fetch_and_render_dashboard
|
8 |
from analytics_fetch_and_rendering import fetch_and_render_analytics
|
9 |
from mentions_dashboard import generate_mentions_dashboard
|
10 |
-
|
11 |
-
# Import the function from your utils file
|
12 |
from gradio_utils import get_url_user_token
|
13 |
-
# Import the Bubble API call function (ensure filename matches: Bubble_API_Calls.py)
|
14 |
from Bubble_API_Calls import fetch_linkedin_token_from_bubble
|
15 |
|
16 |
-
|
17 |
-
def check_token_status(
|
18 |
-
""
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
linkedin_client_id_from_env = os.environ.get("Linkedin_client_id")
|
42 |
-
|
43 |
-
if not linkedin_client_id_from_env:
|
44 |
-
bubble_api_status_msg = "β CRITICAL ERROR: 'Linkedin_client_id' environment variable not set."
|
45 |
-
print(bubble_api_status_msg)
|
46 |
-
new_token_state["client_id"] = "ENV VAR MISSING" # Indicate error in state
|
47 |
-
# Return values: bubble_api_status, overall_token_status, new_token_state
|
48 |
-
return check_token_status(new_token_state), new_token_state
|
49 |
-
|
50 |
-
# Store client_id from env in the state, regardless of token outcome (if env var exists)
|
51 |
-
new_token_state["client_id"] = linkedin_client_id_from_env
|
52 |
-
|
53 |
-
if not url_user_token_str or "not found" in url_user_token_str or "Could not access" in url_user_token_str:
|
54 |
-
bubble_api_status_msg = f"βΉοΈ No valid user token from URL to query Bubble. ({url_user_token_str})"
|
55 |
-
# Client ID is known (if env var was found), but no token to fetch
|
56 |
-
return check_token_status(new_token_state), new_token_state
|
57 |
-
|
58 |
-
print(f"Attempting to fetch token from Bubble with user token: {url_user_token_str}") # Changed "state" to "user token" for clarity
|
59 |
-
parsed_token_dict = fetch_linkedin_token_from_bubble(url_user_token_str)
|
60 |
-
|
61 |
-
if parsed_token_dict and isinstance(parsed_token_dict, dict) and "access_token" in parsed_token_dict:
|
62 |
-
new_token_state["token"] = parsed_token_dict # Store the actual token
|
63 |
-
bubble_api_status_msg = f"β
Token successfully fetched from Bubble for user token: {url_user_token_str}. Client ID loaded."
|
64 |
-
print(bubble_api_status_msg)
|
65 |
else:
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
|
70 |
-
# Return values: bubble_api_status, overall_token_status, new_token_state
|
71 |
-
return check_token_status(new_token_state), new_token_state
|
72 |
|
73 |
-
|
74 |
-
|
75 |
-
# Check only for the presence of the token
|
76 |
-
if not (current_token_state and current_token_state.get("token")):
|
77 |
return "<p style='color:red; text-align:center;'>β Access denied. No token available.</p>"
|
78 |
-
|
79 |
-
|
80 |
-
current_token_state.get("token")
|
81 |
-
)
|
82 |
-
return html
|
83 |
-
|
84 |
-
def guarded_fetch_analytics(current_token_state):
|
85 |
-
# Check only for the presence of the token
|
86 |
-
if not (current_token_state and current_token_state.get("token")):
|
87 |
-
return (
|
88 |
-
"<p style='color:red; text-align:center;'>β Access denied. No token available.</p>",
|
89 |
-
None, None, None, None, None, None, None
|
90 |
-
)
|
91 |
-
client_id = current_token_state.get("client_id")
|
92 |
-
token_data = current_token_state.get("token")
|
93 |
-
count_md, plot, growth_plot, avg_post_eng_rate, interaction_metrics, eb_metrics, mentions_vol_metrics, mentions_sentiment_metrics = fetch_and_render_analytics(
|
94 |
-
client_id,
|
95 |
-
token_data
|
96 |
-
)
|
97 |
-
return count_md, plot, growth_plot, avg_post_eng_rate, interaction_metrics, eb_metrics, mentions_vol_metrics, mentions_sentiment_metrics
|
98 |
|
99 |
-
def
|
100 |
-
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
return ("<p style='color:red; text-align:center;'>β Access denied. No token available.</p>", None)
|
103 |
-
|
104 |
-
|
105 |
-
current_token_state.get("token")
|
106 |
-
)
|
107 |
-
return html, fig
|
108 |
|
109 |
-
# --- Build the Gradio UI ---
|
110 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
|
111 |
title="LinkedIn Post Viewer & Analytics") as app:
|
112 |
|
113 |
-
|
114 |
-
token_state = gr.State(value={"token": None, "client_id": None})
|
115 |
|
116 |
gr.Markdown("# π LinkedIn Organization Post Viewer & Analytics")
|
117 |
gr.Markdown("Token is supplied via URL parameter for Bubble.io lookup. Then explore dashboard and analytics.")
|
118 |
|
119 |
-
url_user_token_display = gr.Textbox(
|
120 |
-
|
121 |
-
|
122 |
-
placeholder="Attempting to load from URL...",
|
123 |
-
visible=False # Kept hidden as per original logic
|
124 |
-
)
|
125 |
-
|
126 |
-
status_box = gr.Textbox(label="Overall Token Status", interactive=False, placeholder="Waiting for token check...") # Added placeholder
|
127 |
|
128 |
-
app.load(
|
129 |
-
fn=get_url_user_token,
|
130 |
-
inputs=None,
|
131 |
-
outputs=[url_user_token_display]
|
132 |
-
)
|
133 |
|
134 |
-
# Modified outputs for process_and_store_bubble_token
|
135 |
url_user_token_display.change(
|
136 |
fn=process_and_store_bubble_token,
|
137 |
-
inputs=[url_user_token_display, token_state],
|
138 |
-
outputs=[status_box, token_state]
|
139 |
)
|
140 |
|
141 |
-
# app.load to initialize status_box based on initial token_state
|
142 |
app.load(fn=check_token_status, inputs=[token_state], outputs=status_box)
|
143 |
-
|
144 |
-
|
145 |
-
timer = gr.Timer(5.0)
|
146 |
-
timer.tick(fn=check_token_status, inputs=[token_state], outputs=status_box)
|
147 |
-
# Removed timer.tick for get_active_client_id and client_display
|
148 |
|
149 |
with gr.Tabs():
|
150 |
with gr.TabItem("1οΈβ£ Dashboard"):
|
151 |
gr.Markdown("View your organization's recent posts and their engagement statistics.")
|
152 |
fetch_dashboard_btn = gr.Button("π Fetch Posts & Stats", variant="primary")
|
153 |
-
dashboard_html = gr.HTML(
|
154 |
-
fetch_dashboard_btn.click(
|
155 |
-
fn=guarded_fetch_dashboard,
|
156 |
-
inputs=[token_state],
|
157 |
-
outputs=[dashboard_html]
|
158 |
-
)
|
159 |
|
160 |
with gr.TabItem("2οΈβ£ Analytics"):
|
161 |
gr.Markdown("View follower count and monthly gains for your organization.")
|
162 |
fetch_analytics_btn = gr.Button("π Fetch Follower Analytics", variant="primary")
|
163 |
-
|
164 |
follower_count = gr.Markdown("<p style='text-align: center; color: #555;'>Waiting for token...</p>")
|
165 |
|
166 |
with gr.Row():
|
167 |
-
follower_plot = gr.Plot(
|
168 |
-
growth_rate_plot = gr.Plot(visible=True)
|
169 |
with gr.Row():
|
170 |
-
|
171 |
with gr.Row():
|
172 |
-
|
173 |
with gr.Row():
|
174 |
-
|
175 |
with gr.Row():
|
176 |
-
|
177 |
-
mentions_sentiment_data = gr.Plot(visible=True)
|
178 |
|
179 |
fetch_analytics_btn.click(
|
180 |
fn=guarded_fetch_analytics,
|
181 |
inputs=[token_state],
|
182 |
-
outputs=[follower_count, follower_plot,
|
|
|
183 |
)
|
184 |
|
185 |
with gr.TabItem("3οΈβ£ Mentions"):
|
186 |
gr.Markdown("Analyze sentiment of recent posts that mention your organization.")
|
187 |
fetch_mentions_btn = gr.Button("π§ Fetch Mentions & Sentiment", variant="primary")
|
188 |
-
mentions_html = gr.HTML(
|
189 |
-
mentions_plot = gr.Plot(
|
190 |
fetch_mentions_btn.click(
|
191 |
fn=run_mentions_and_load,
|
192 |
inputs=[token_state],
|
@@ -194,8 +125,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
|
|
194 |
)
|
195 |
|
196 |
if __name__ == "__main__":
|
197 |
-
# Ensure the Linkedin_client_id environment variable is set before launching.
|
198 |
-
# You might want to add a check here and print a warning if it's not set.
|
199 |
if not os.environ.get("Linkedin_client_id"):
|
200 |
print("WARNING: The 'Linkedin_client_id' environment variable is not set. The application may not function correctly.")
|
201 |
-
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
1 |
# -*- coding: utf-8 -*-
|
2 |
import gradio as gr
|
3 |
import json
|
4 |
+
import os
|
5 |
|
|
|
6 |
from Data_Fetching_and_Rendering import fetch_and_render_dashboard
|
7 |
from analytics_fetch_and_rendering import fetch_and_render_analytics
|
8 |
from mentions_dashboard import generate_mentions_dashboard
|
|
|
|
|
9 |
from gradio_utils import get_url_user_token
|
|
|
10 |
from Bubble_API_Calls import fetch_linkedin_token_from_bubble
|
11 |
|
12 |
+
|
13 |
+
def check_token_status(token_state):
|
14 |
+
return "β
Token available" if token_state and token_state.get("token") else "β Token not available"
|
15 |
+
|
16 |
+
|
17 |
+
def process_and_store_bubble_token(url_user_token, org_urn, token_state):
|
18 |
+
new_state = token_state.copy() if token_state else {"token": None, "client_id": None, "org_urn": None}
|
19 |
+
new_state.update({"token": None, "org_urn": org_urn})
|
20 |
+
|
21 |
+
client_id = os.environ.get("Linkedin_client_id")
|
22 |
+
if not client_id:
|
23 |
+
print("β CRITICAL ERROR: 'Linkedin_client_id' environment variable not set.")
|
24 |
+
new_state["client_id"] = "ENV VAR MISSING"
|
25 |
+
return check_token_status(new_state), new_state
|
26 |
+
|
27 |
+
new_state["client_id"] = client_id
|
28 |
+
if not url_user_token or "not found" in url_user_token or "Could not access" in url_user_token:
|
29 |
+
return check_token_status(new_state), new_state
|
30 |
+
|
31 |
+
print(f"Attempting to fetch token from Bubble with user token: {url_user_token}")
|
32 |
+
parsed = fetch_linkedin_token_from_bubble(url_user_token)
|
33 |
+
|
34 |
+
if isinstance(parsed, dict) and "access_token" in parsed:
|
35 |
+
new_state["token"] = parsed
|
36 |
+
print("β
Token successfully fetched from Bubble.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
else:
|
38 |
+
print("β Failed to fetch a valid token from Bubble.")
|
39 |
+
|
40 |
+
return check_token_status(new_state), new_state
|
41 |
|
|
|
|
|
42 |
|
43 |
+
def guarded_fetch_dashboard(token_state):
|
44 |
+
if not token_state or not token_state.get("token"):
|
|
|
|
|
45 |
return "<p style='color:red; text-align:center;'>β Access denied. No token available.</p>"
|
46 |
+
return fetch_and_render_dashboard(token_state.get("client_id"), token_state.get("token"))
|
47 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
def guarded_fetch_analytics(token_state):
|
50 |
+
if not token_state or not token_state.get("token"):
|
51 |
+
return ("<p style='color:red; text-align:center;'>β Access denied. No token available.</p>",
|
52 |
+
None, None, None, None, None, None, None)
|
53 |
+
|
54 |
+
return fetch_and_render_analytics(token_state.get("client_id"), token_state.get("token"))
|
55 |
+
|
56 |
+
|
57 |
+
def run_mentions_and_load(token_state):
|
58 |
+
if not token_state or not token_state.get("token"):
|
59 |
return ("<p style='color:red; text-align:center;'>β Access denied. No token available.</p>", None)
|
60 |
+
return generate_mentions_dashboard(token_state.get("client_id"), token_state.get("token"))
|
61 |
+
|
|
|
|
|
|
|
62 |
|
|
|
63 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue", secondary_hue="sky"),
|
64 |
title="LinkedIn Post Viewer & Analytics") as app:
|
65 |
|
66 |
+
token_state = gr.State(value={"token": None, "client_id": None, "org_urn": None})
|
|
|
67 |
|
68 |
gr.Markdown("# π LinkedIn Organization Post Viewer & Analytics")
|
69 |
gr.Markdown("Token is supplied via URL parameter for Bubble.io lookup. Then explore dashboard and analytics.")
|
70 |
|
71 |
+
url_user_token_display = gr.Textbox(label="User Token (from URL - Hidden)", interactive=False, visible=False)
|
72 |
+
status_box = gr.Textbox(label="Overall Token Status", interactive=False)
|
73 |
+
org_urn = gr.Textbox(visible=False) # Needed for input, was missing from initial script
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
app.load(fn=get_url_user_token, inputs=None, outputs=[url_user_token_display, org_urn])
|
|
|
|
|
|
|
|
|
76 |
|
|
|
77 |
url_user_token_display.change(
|
78 |
fn=process_and_store_bubble_token,
|
79 |
+
inputs=[url_user_token_display, org_urn, token_state],
|
80 |
+
outputs=[status_box, token_state]
|
81 |
)
|
82 |
|
|
|
83 |
app.load(fn=check_token_status, inputs=[token_state], outputs=status_box)
|
84 |
+
gr.Timer(5.0).tick(fn=check_token_status, inputs=[token_state], outputs=status_box)
|
|
|
|
|
|
|
|
|
85 |
|
86 |
with gr.Tabs():
|
87 |
with gr.TabItem("1οΈβ£ Dashboard"):
|
88 |
gr.Markdown("View your organization's recent posts and their engagement statistics.")
|
89 |
fetch_dashboard_btn = gr.Button("π Fetch Posts & Stats", variant="primary")
|
90 |
+
dashboard_html = gr.HTML("<p style='text-align: center; color: #555;'>Waiting for token...</p>")
|
91 |
+
fetch_dashboard_btn.click(fn=guarded_fetch_dashboard, inputs=[token_state], outputs=[dashboard_html])
|
|
|
|
|
|
|
|
|
92 |
|
93 |
with gr.TabItem("2οΈβ£ Analytics"):
|
94 |
gr.Markdown("View follower count and monthly gains for your organization.")
|
95 |
fetch_analytics_btn = gr.Button("π Fetch Follower Analytics", variant="primary")
|
|
|
96 |
follower_count = gr.Markdown("<p style='text-align: center; color: #555;'>Waiting for token...</p>")
|
97 |
|
98 |
with gr.Row():
|
99 |
+
follower_plot, growth_plot = gr.Plot(), gr.Plot()
|
|
|
100 |
with gr.Row():
|
101 |
+
eng_rate_plot = gr.Plot()
|
102 |
with gr.Row():
|
103 |
+
interaction_plot = gr.Plot()
|
104 |
with gr.Row():
|
105 |
+
eb_plot = gr.Plot()
|
106 |
with gr.Row():
|
107 |
+
mentions_vol_plot, mentions_sentiment_plot = gr.Plot(), gr.Plot()
|
|
|
108 |
|
109 |
fetch_analytics_btn.click(
|
110 |
fn=guarded_fetch_analytics,
|
111 |
inputs=[token_state],
|
112 |
+
outputs=[follower_count, follower_plot, growth_plot, eng_rate_plot,
|
113 |
+
interaction_plot, eb_plot, mentions_vol_plot, mentions_sentiment_plot]
|
114 |
)
|
115 |
|
116 |
with gr.TabItem("3οΈβ£ Mentions"):
|
117 |
gr.Markdown("Analyze sentiment of recent posts that mention your organization.")
|
118 |
fetch_mentions_btn = gr.Button("π§ Fetch Mentions & Sentiment", variant="primary")
|
119 |
+
mentions_html = gr.HTML("<p style='text-align: center; color: #555;'>Waiting for token...</p>")
|
120 |
+
mentions_plot = gr.Plot()
|
121 |
fetch_mentions_btn.click(
|
122 |
fn=run_mentions_and_load,
|
123 |
inputs=[token_state],
|
|
|
125 |
)
|
126 |
|
127 |
if __name__ == "__main__":
|
|
|
|
|
128 |
if not os.environ.get("Linkedin_client_id"):
|
129 |
print("WARNING: The 'Linkedin_client_id' environment variable is not set. The application may not function correctly.")
|
130 |
+
app.launch(server_name="0.0.0.0", server_port=7860, share=True)
|