Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -192,9 +192,8 @@ def batch_process_keywords(keywords, batch_size=8):
|
|
| 192 |
category_embeddings = compute_category_embeddings()
|
| 193 |
|
| 194 |
for i in range(0, len(keywords), batch_size):
|
| 195 |
-
logger.info(f"Processing {len(keywords)}
|
| 196 |
batch = keywords[i:i+batch_size]
|
| 197 |
-
logger.info(f"Processing batch {i//batch_size + 1}")
|
| 198 |
batch_embeddings = sentence_model.encode(batch, batch_size=batch_size, show_progress_bar=False)
|
| 199 |
|
| 200 |
intents = [sort_by_keyword_feature(kw) for kw in batch]
|
|
@@ -219,11 +218,10 @@ def batch_process_keywords(keywords, batch_size=8):
|
|
| 219 |
processed_data['NER Entities'].extend(processed_entities)
|
| 220 |
processed_data['Google Content Topics'].extend(Google_Content_Topics)
|
| 221 |
|
| 222 |
-
# Force garbage collection
|
| 223 |
gc.collect()
|
| 224 |
logger.info("Keyword processing completed successfully")
|
| 225 |
except Exception as e:
|
| 226 |
-
logger.exception("An error occurred in batch_process_keywords")
|
| 227 |
|
| 228 |
return processed_data
|
| 229 |
|
|
@@ -414,7 +412,7 @@ def combined_callback(loaded, n_clicks, keyword_input):
|
|
| 414 |
else:
|
| 415 |
return loaded, False, False, "", "success", None, '', False, ''
|
| 416 |
except Exception as e:
|
| 417 |
-
logger.exception("An error occurred in combined_callback")
|
| 418 |
return loaded, False, True, f"An error occurred: {str(e)}", "danger", None, '', False, ''
|
| 419 |
|
| 420 |
def handle_model_loading(loaded):
|
|
@@ -442,14 +440,14 @@ def handle_keyword_processing(n_clicks, keyword_input):
|
|
| 442 |
)
|
| 443 |
def update_bar_chart(processed_data):
|
| 444 |
logger.info("Updating bar chart")
|
| 445 |
-
if processed_data is None:
|
| 446 |
logger.info("No processed data available")
|
| 447 |
return {
|
| 448 |
'data': [],
|
| 449 |
'layout': {
|
| 450 |
'height': 0,
|
| 451 |
'annotations': [{
|
| 452 |
-
'text': '',
|
| 453 |
'xref': 'paper',
|
| 454 |
'yref': 'paper',
|
| 455 |
'showarrow': False,
|
|
@@ -458,30 +456,46 @@ def update_bar_chart(processed_data):
|
|
| 458 |
}
|
| 459 |
}
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
|
|
|
|
|
|
| 481 |
)
|
| 482 |
-
)
|
| 483 |
|
| 484 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 485 |
|
| 486 |
# Callback for updating the dropdown and download button
|
| 487 |
@app.callback(
|
|
|
|
| 192 |
category_embeddings = compute_category_embeddings()
|
| 193 |
|
| 194 |
for i in range(0, len(keywords), batch_size):
|
| 195 |
+
logger.info(f"Processing batch {i//batch_size + 1} of {len(keywords)//batch_size + 1}")
|
| 196 |
batch = keywords[i:i+batch_size]
|
|
|
|
| 197 |
batch_embeddings = sentence_model.encode(batch, batch_size=batch_size, show_progress_bar=False)
|
| 198 |
|
| 199 |
intents = [sort_by_keyword_feature(kw) for kw in batch]
|
|
|
|
| 218 |
processed_data['NER Entities'].extend(processed_entities)
|
| 219 |
processed_data['Google Content Topics'].extend(Google_Content_Topics)
|
| 220 |
|
|
|
|
| 221 |
gc.collect()
|
| 222 |
logger.info("Keyword processing completed successfully")
|
| 223 |
except Exception as e:
|
| 224 |
+
logger.exception(f"An error occurred in batch_process_keywords: {str(e)}")
|
| 225 |
|
| 226 |
return processed_data
|
| 227 |
|
|
|
|
| 412 |
else:
|
| 413 |
return loaded, False, False, "", "success", None, '', False, ''
|
| 414 |
except Exception as e:
|
| 415 |
+
logger.exception(f"An error occurred in combined_callback: {str(e)}")
|
| 416 |
return loaded, False, True, f"An error occurred: {str(e)}", "danger", None, '', False, ''
|
| 417 |
|
| 418 |
def handle_model_loading(loaded):
|
|
|
|
| 440 |
)
|
| 441 |
def update_bar_chart(processed_data):
|
| 442 |
logger.info("Updating bar chart")
|
| 443 |
+
if processed_data is None or not processed_data:
|
| 444 |
logger.info("No processed data available")
|
| 445 |
return {
|
| 446 |
'data': [],
|
| 447 |
'layout': {
|
| 448 |
'height': 0,
|
| 449 |
'annotations': [{
|
| 450 |
+
'text': 'No data available',
|
| 451 |
'xref': 'paper',
|
| 452 |
'yref': 'paper',
|
| 453 |
'showarrow': False,
|
|
|
|
| 456 |
}
|
| 457 |
}
|
| 458 |
|
| 459 |
+
try:
|
| 460 |
+
df = pd.DataFrame(processed_data)
|
| 461 |
+
logger.info(f"Data shape: {df.shape}")
|
| 462 |
+
intent_counts = df['Intent'].value_counts().reset_index()
|
| 463 |
+
intent_counts.columns = ['Intent', 'Count']
|
| 464 |
+
|
| 465 |
+
fig = px.bar(intent_counts, x='Intent', y='Count', color='Intent',
|
| 466 |
+
title='Keyword Intent Distribution',
|
| 467 |
+
color_discrete_sequence=px.colors.qualitative.Dark2)
|
| 468 |
+
|
| 469 |
+
fig.update_layout(
|
| 470 |
+
plot_bgcolor='#222222',
|
| 471 |
+
paper_bgcolor='#222222',
|
| 472 |
+
font_color='white',
|
| 473 |
+
height=400,
|
| 474 |
+
legend=dict(
|
| 475 |
+
orientation="h",
|
| 476 |
+
yanchor="bottom",
|
| 477 |
+
y=1.02,
|
| 478 |
+
xanchor="right",
|
| 479 |
+
x=1
|
| 480 |
+
)
|
| 481 |
)
|
|
|
|
| 482 |
|
| 483 |
+
return fig
|
| 484 |
+
except Exception as e:
|
| 485 |
+
logger.exception(f"Error in update_bar_chart: {str(e)}")
|
| 486 |
+
return {
|
| 487 |
+
'data': [],
|
| 488 |
+
'layout': {
|
| 489 |
+
'height': 0,
|
| 490 |
+
'annotations': [{
|
| 491 |
+
'text': f'Error: {str(e)}',
|
| 492 |
+
'xref': 'paper',
|
| 493 |
+
'yref': 'paper',
|
| 494 |
+
'showarrow': False,
|
| 495 |
+
'font': {'size': 28}
|
| 496 |
+
}]
|
| 497 |
+
}
|
| 498 |
+
}
|
| 499 |
|
| 500 |
# Callback for updating the dropdown and download button
|
| 501 |
@app.callback(
|