Spaces:
Running
on
T4
Running
on
T4
Update app.py
Browse files
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
|
51 |
-
if url:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
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/
|