Kuautli commited on
Commit
3d817f4
verified
1 Parent(s): c6d0116

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -70
app.py CHANGED
@@ -36,96 +36,84 @@ def convert_graph_to_html(graph, full_html=False):
36
  @app.route("/", methods=["GET", "POST"])
37
  def index():
38
  video_details = None
39
- k_distance_graph = None
40
- scores_graph = None
41
  sankey_graph = None
 
42
  image_path = None
43
  sentiment_daily_graph = None
44
  sentiment_count = None
 
45
 
46
  current_directory = os.getcwd()
47
  log_message("Iniciando procesamiento...")
48
 
49
  if request.method == "POST":
50
- url = request.form["url"]
51
- if url:
52
- log_message("Obteniendo datos de Youtube")
53
- video_details = clustering.get_youtube_video_details(url, api_key)
54
- comments_df = clustering.get_youtube_comments(api_key, url)
55
- log_message("Generando embeddings")
56
- comments_df = clustering.add_normalized_embeddings_to_dataframe(
57
- comments_df, "comment"
58
- )
59
- log_message("Procesamiento de los datos")
60
- comments_df["published_at"] = pd.to_datetime(
61
- comments_df["published_at"]
62
- ).dt.date
63
- log_message("Clasificaci贸n de los sentimientos")
64
- comments_df = clustering.classify_sentiment_df(comments_df)
65
- comments_df.to_pickle(
66
- "./data/Comentarios-Youtube/comments_df.pkl"
67
- )
68
- comments_df = pd.read_pickle(
69
- "./data/Comentarios-Youtube/comments_df.pkl"
70
- )
71
- sentiment_count = comments_df["sentimiento"].value_counts().to_dict()
72
- sentiment_daily_graph = clustering.plot_sentiment_daily(comments_df)
73
-
74
- sentiment_daily_graph = convert_graph_to_html(sentiment_daily_graph)
75
-
76
- umap_df, min_eps, max_eps = clustering.transform_embeddings(
77
- comments_df, embeddings_col="embeddings"
78
- )
79
- log_message("Generaci贸n de wordcloud")
80
- image_path = os.path.join("static", "wordcloud.png")
81
- clustering.plot_wordcloud(comments_df, text_column="comment", output_filename=image_path)
82
-
83
- total = comments_df.shape[0]
84
-
85
- min_items_by_cluster = clustering.determine_min_items_by_cluster(total)
86
- log_message("Modelado y generaci贸n de m茅tricas")
87
- (
88
- cluster_assignments,
89
- cluster_counts,
90
- calinski_harabasz_scores,
91
- silhouette_scores,
92
- most_similar_comments,
93
- umap_df,
94
- ) = clustering.perform_clustering(
95
- umap_df, min_eps, max_eps, n=10,
96
- embeddings_col="embeddings"
97
- )
98
- log_message(f"Clusters assignments {cluster_assignments}")
99
- log_message("Creaci贸n de gr谩fico de Sankey")
100
- labels, source, target, values, comments = clustering.build_sankey_data(
101
- cluster_assignments,
102
- cluster_counts,
103
- most_similar_comments,
104
- min_items_by_cluster=min_items_by_cluster,
105
- )
106
-
107
- sankey_graph = clustering.plot_sankey(
108
- labels, source, target, values, comments, height=1000, width=1200
109
- )
110
- sankey_graph = convert_graph_to_html(sankey_graph)
111
-
112
- scores_graph, _ = clustering.plot_clustering_metric(
113
- silhouette_scores, calinski_harabasz_scores
114
- )
115
- scores_graph = convert_graph_to_html(scores_graph)
116
 
117
  return render_template(
118
  "index.html",
119
  video_details=video_details,
120
- k_distance_graph=k_distance_graph,
121
  sankey_graph=sankey_graph,
122
  scores_graph=scores_graph,
123
  wordcloud_path=image_path,
124
  sentiment_daily_graph=sentiment_daily_graph,
125
  sentiment_count=sentiment_count,
 
126
  )
127
 
128
-
129
  # gunicorn -b 0.0.0.0:5000 app_clustering.app:app
130
  # http://172.20.0.2:5000/
131
  # http://0.0.0.0:5000/
 
36
  @app.route("/", methods=["GET", "POST"])
37
  def index():
38
  video_details = None
 
 
39
  sankey_graph = None
40
+ scores_graph = None
41
  image_path = None
42
  sentiment_daily_graph = None
43
  sentiment_count = None
44
+ error_message = None
45
 
46
  current_directory = os.getcwd()
47
  log_message("Iniciando procesamiento...")
48
 
49
  if request.method == "POST":
50
+ url = request.form.get("url") # Utiliza get para evitar KeyError
51
+ if not url:
52
+ error_message = "La URL es requerida."
53
+ return render_template("index.html", error_message=error_message)
54
+
55
+ log_message("Obteniendo datos de Youtube")
56
+ video_details = clustering.get_youtube_video_details(url, api_key)
57
+ if "error" in video_details: # Manejo de error al obtener detalles del video
58
+ error_message = video_details["error"]
59
+ return render_template("index.html", error_message=error_message)
60
+
61
+ comments_df = clustering.get_youtube_comments(api_key, url)
62
+ if comments_df is None: # Verifica si no hay comentarios
63
+ error_message = "No se pudieron obtener comentarios."
64
+ return render_template("index.html", error_message=error_message)
65
+
66
+ log_message("Generando embeddings")
67
+ comments_df = clustering.add_normalized_embeddings_to_dataframe(comments_df, "comment")
68
+
69
+ log_message("Procesamiento de los datos")
70
+ comments_df["published_at"] = pd.to_datetime(comments_df["published_at"]).dt.date
71
+
72
+ log_message("Clasificaci贸n de los sentimientos")
73
+ comments_df = clustering.classify_sentiment_df(comments_df)
74
+ comments_df.to_pickle("./data/Comentarios-Youtube/comments_df.pkl")
75
+
76
+ sentiment_count = comments_df["sentimiento"].value_counts().to_dict()
77
+ sentiment_daily_graph = clustering.plot_sentiment_daily(comments_df)
78
+ sentiment_daily_graph = convert_graph_to_html(sentiment_daily_graph)
79
+
80
+ umap_df, min_eps, max_eps = clustering.transform_embeddings(comments_df, embeddings_col="embeddings")
81
+
82
+ log_message("Generaci贸n de wordcloud")
83
+ image_path = os.path.join("static", "wordcloud.png")
84
+ clustering.plot_wordcloud(comments_df, text_column="comment", output_filename=image_path)
85
+
86
+ total = comments_df.shape[0]
87
+ min_items_by_cluster = clustering.determine_min_items_by_cluster(total)
88
+
89
+ log_message("Modelado y generaci贸n de m茅tricas")
90
+ (cluster_assignments, cluster_counts, calinski_harabasz_scores, silhouette_scores, most_similar_comments, umap_df) = clustering.perform_clustering(
91
+ umap_df, min_eps, max_eps, n=10, embeddings_col="embeddings"
92
+ )
93
+
94
+ log_message(f"Clusters assignments {cluster_assignments}")
95
+ log_message("Creaci贸n de gr谩fico de Sankey")
96
+ labels, source, target, values, comments = clustering.build_sankey_data(
97
+ cluster_assignments, cluster_counts, most_similar_comments, min_items_by_cluster=min_items_by_cluster
98
+ )
99
+
100
+ sankey_graph = clustering.plot_sankey(labels, source, target, values, comments, height=1000, width=1200)
101
+ sankey_graph = convert_graph_to_html(sankey_graph)
102
+
103
+ scores_graph, _ = clustering.plot_clustering_metric(silhouette_scores, calinski_harabasz_scores)
104
+ scores_graph = convert_graph_to_html(scores_graph)
 
 
 
 
 
 
 
 
 
 
 
105
 
106
  return render_template(
107
  "index.html",
108
  video_details=video_details,
 
109
  sankey_graph=sankey_graph,
110
  scores_graph=scores_graph,
111
  wordcloud_path=image_path,
112
  sentiment_daily_graph=sentiment_daily_graph,
113
  sentiment_count=sentiment_count,
114
+ error_message=error_message, # Incluye el mensaje de error si existe
115
  )
116
 
 
117
  # gunicorn -b 0.0.0.0:5000 app_clustering.app:app
118
  # http://172.20.0.2:5000/
119
  # http://0.0.0.0:5000/