Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -201,129 +201,66 @@ def plot_historical_data(historical_data):
|
|
201 |
return plt
|
202 |
|
203 |
def create_evolution_chart(data, forecast_months=6, growth_scenario="Moderate"):
|
204 |
-
"""Create
|
205 |
try:
|
206 |
import plotly.graph_objects as go
|
207 |
-
from plotly.subplots import make_subplots
|
208 |
|
209 |
-
# Create figure
|
210 |
-
fig =
|
211 |
|
212 |
-
# Add
|
213 |
fig.add_trace(
|
214 |
go.Scatter(
|
215 |
x=[item["month"] for item in data],
|
216 |
y=[item["searchVolume"] for item in data],
|
217 |
name="Search Volume",
|
218 |
line=dict(color="#8884d8", width=3),
|
219 |
-
|
220 |
)
|
221 |
)
|
222 |
|
|
|
|
|
|
|
|
|
|
|
223 |
fig.add_trace(
|
224 |
go.Scatter(
|
225 |
x=[item["month"] for item in data],
|
226 |
-
y=[item["competitionScore"] for item in data],
|
227 |
name="Competition Score",
|
228 |
-
line=dict(color="#82ca9d", width=
|
229 |
-
|
230 |
-
)
|
231 |
-
secondary_y=True
|
232 |
)
|
233 |
|
|
|
234 |
fig.add_trace(
|
235 |
go.Scatter(
|
236 |
x=[item["month"] for item in data],
|
237 |
-
y=[item["intentClarity"] for item in data],
|
238 |
name="Intent Clarity",
|
239 |
-
line=dict(color="#ffc658", width=
|
240 |
-
|
241 |
-
),
|
242 |
-
secondary_y=True
|
243 |
-
)
|
244 |
-
|
245 |
-
# Add trend line
|
246 |
-
x_values = list(range(len(data)))
|
247 |
-
y_values = [item["searchVolume"] for item in data]
|
248 |
-
|
249 |
-
# Simple linear regression
|
250 |
-
slope, intercept = np.polyfit(x_values, y_values, 1)
|
251 |
-
trend_y = [slope * x + intercept for x in x_values]
|
252 |
-
|
253 |
-
fig.add_trace(
|
254 |
-
go.Scatter(
|
255 |
-
x=[item["month"] for item in data],
|
256 |
-
y=trend_y,
|
257 |
-
name="Trend",
|
258 |
-
line=dict(color="rgba(255, 0, 0, 0.5)", width=2, dash="dot"),
|
259 |
-
hoverinfo="skip"
|
260 |
)
|
261 |
)
|
262 |
|
263 |
-
#
|
264 |
fig.update_layout(
|
265 |
title=f"Keyword Evolution Forecast ({growth_scenario} Growth)",
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
titlefont=dict(size=14),
|
271 |
-
showgrid=True,
|
272 |
-
gridcolor="rgba(0,0,0,0.1)"
|
273 |
-
),
|
274 |
-
yaxis=dict(
|
275 |
-
title="Search Volume",
|
276 |
-
titlefont=dict(size=14),
|
277 |
-
showgrid=True,
|
278 |
-
gridcolor="rgba(0,0,0,0.1)"
|
279 |
-
),
|
280 |
-
yaxis2=dict(
|
281 |
-
title="Score (0-100)",
|
282 |
-
titlefont=dict(size=14),
|
283 |
-
range=[0, 100]
|
284 |
-
),
|
285 |
-
legend=dict(
|
286 |
-
orientation="h",
|
287 |
-
yanchor="bottom",
|
288 |
-
y=1.02,
|
289 |
-
xanchor="center",
|
290 |
-
x=0.5
|
291 |
-
),
|
292 |
-
margin=dict(l=10, r=10, t=80, b=10),
|
293 |
-
height=500,
|
294 |
-
template="plotly_white"
|
295 |
)
|
296 |
|
297 |
-
# Add annotations for key insights
|
298 |
-
max_month_index = y_values.index(max(y_values))
|
299 |
-
fig.add_annotation(
|
300 |
-
x=data[max_month_index]["month"],
|
301 |
-
y=max(y_values),
|
302 |
-
text="Peak Volume",
|
303 |
-
showarrow=True,
|
304 |
-
arrowhead=1,
|
305 |
-
ax=0,
|
306 |
-
ay=-40
|
307 |
-
)
|
308 |
-
|
309 |
-
# Return the figure
|
310 |
return fig
|
311 |
-
|
312 |
except Exception as e:
|
313 |
-
print(f"Error in
|
314 |
-
#
|
315 |
-
|
316 |
-
fig
|
317 |
-
fig.add_annotation(
|
318 |
-
x=0.5, y=0.5,
|
319 |
-
text=f"Error creating chart: {str(e)}",
|
320 |
-
showarrow=False,
|
321 |
-
font=dict(size=14, color="red")
|
322 |
-
)
|
323 |
-
fig.update_layout(
|
324 |
-
xaxis=dict(showticklabels=False),
|
325 |
-
yaxis=dict(showticklabels=False)
|
326 |
-
)
|
327 |
return fig
|
328 |
|
329 |
def analyze_keyword(keyword, forecast_months=6, growth_scenario="Moderate", progress=gr.Progress()):
|
|
|
201 |
return plt
|
202 |
|
203 |
def create_evolution_chart(data, forecast_months=6, growth_scenario="Moderate"):
|
204 |
+
"""Create a simpler chart that's more compatible with Gradio"""
|
205 |
try:
|
206 |
import plotly.graph_objects as go
|
|
|
207 |
|
208 |
+
# Create a basic figure without subplots
|
209 |
+
fig = go.Figure()
|
210 |
|
211 |
+
# Add main trace for search volume
|
212 |
fig.add_trace(
|
213 |
go.Scatter(
|
214 |
x=[item["month"] for item in data],
|
215 |
y=[item["searchVolume"] for item in data],
|
216 |
name="Search Volume",
|
217 |
line=dict(color="#8884d8", width=3),
|
218 |
+
mode="lines+markers"
|
219 |
)
|
220 |
)
|
221 |
|
222 |
+
# Scale the other metrics to be visible on the same chart
|
223 |
+
max_volume = max([item["searchVolume"] for item in data])
|
224 |
+
scale_factor = max_volume / 100
|
225 |
+
|
226 |
+
# Add competition score (scaled)
|
227 |
fig.add_trace(
|
228 |
go.Scatter(
|
229 |
x=[item["month"] for item in data],
|
230 |
+
y=[item["competitionScore"] * scale_factor for item in data],
|
231 |
name="Competition Score",
|
232 |
+
line=dict(color="#82ca9d", width=2, dash="dot"),
|
233 |
+
mode="lines+markers"
|
234 |
+
)
|
|
|
235 |
)
|
236 |
|
237 |
+
# Add intent clarity (scaled)
|
238 |
fig.add_trace(
|
239 |
go.Scatter(
|
240 |
x=[item["month"] for item in data],
|
241 |
+
y=[item["intentClarity"] * scale_factor for item in data],
|
242 |
name="Intent Clarity",
|
243 |
+
line=dict(color="#ffc658", width=2, dash="dash"),
|
244 |
+
mode="lines+markers"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
245 |
)
|
246 |
)
|
247 |
|
248 |
+
# Simple layout
|
249 |
fig.update_layout(
|
250 |
title=f"Keyword Evolution Forecast ({growth_scenario} Growth)",
|
251 |
+
xaxis_title="Month",
|
252 |
+
yaxis_title="Value",
|
253 |
+
legend=dict(orientation="h", y=1.1),
|
254 |
+
height=500
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
255 |
)
|
256 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
257 |
return fig
|
258 |
+
|
259 |
except Exception as e:
|
260 |
+
print(f"Error in chart creation: {str(e)}")
|
261 |
+
# Fallback to an even simpler chart
|
262 |
+
fig = go.Figure(data=go.Scatter(x=[1, 2, 3], y=[4, 1, 2]))
|
263 |
+
fig.update_layout(title="Fallback Chart (Error occurred)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
264 |
return fig
|
265 |
|
266 |
def analyze_keyword(keyword, forecast_months=6, growth_scenario="Moderate", progress=gr.Progress()):
|