Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import re
|
|
| 5 |
import xmltodict
|
| 6 |
from collections import Counter
|
| 7 |
import unicodedata
|
|
|
|
| 8 |
|
| 9 |
# Función para normalizar palabras clave (elimina tildes y convierte a minúsculas)
|
| 10 |
def normalize_keyword(keyword):
|
|
@@ -165,7 +166,7 @@ def get_top_suggestions(suggestions, top_n=10):
|
|
| 165 |
suggestion_counter = Counter(suggestions)
|
| 166 |
return suggestion_counter.most_common(top_n)
|
| 167 |
|
| 168 |
-
# Función principal
|
| 169 |
def main(keyword):
|
| 170 |
expanded_keywords = expand_keyword(keyword)
|
| 171 |
all_suggestions = {}
|
|
@@ -174,8 +175,8 @@ def main(keyword):
|
|
| 174 |
youtube_suggestions_all = []
|
| 175 |
bing_suggestions_all = []
|
| 176 |
amazon_suggestions_all = []
|
| 177 |
-
qwant_suggestions_all = []
|
| 178 |
-
brave_suggestions_all = []
|
| 179 |
|
| 180 |
# Obtener sugerencias de DuckDuckGo
|
| 181 |
for exp_keyword in expanded_keywords:
|
|
@@ -238,55 +239,34 @@ def main(keyword):
|
|
| 238 |
brave_top_10 = get_top_suggestions(brave_suggestions_all, top_n=10)
|
| 239 |
|
| 240 |
# Ordenar las sugerencias combinadas por frecuencia
|
| 241 |
-
|
| 242 |
-
suggestions_str = ", ".join([f"{sug} ({freq})" for sug, freq in combined_top_10_suggestions])
|
| 243 |
|
| 244 |
-
# Crear
|
| 245 |
-
|
| 246 |
-
for suggestion, freq in combined_top_10_suggestions:
|
| 247 |
-
all_suggestions_str += f"<li>{suggestion} - {freq} repeticiones</li>"
|
| 248 |
-
all_suggestions_str += "</ul>"
|
| 249 |
|
| 250 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
html_output = f"""
|
| 252 |
<div>
|
| 253 |
-
<b>Sugerencias combinadas de Google, DuckDuckGo, YouTube, Bing, Amazon, Qwant y Brave (Top 10 combinadas):</b>
|
|
|
|
| 254 |
<button class="lg secondary svelte-cmf5ev" style="font-size: small; padding: 2px; color: #808080ba; border: none; margin-left: 5px;"
|
| 255 |
-
onclick='navigator.clipboard.writeText(document.getElementById("suggestions_text").innerText).then(() => alert("Texto copiado al portapapeles"))'
|
|
|
|
| 256 |
</div>
|
| 257 |
-
|
| 258 |
-
<h4>Top 10 Sugerencias de Google:</h4><ul>
|
| 259 |
"""
|
| 260 |
-
for suggestion, freq in google_top_10:
|
| 261 |
-
html_output += f"<li>{suggestion} ({freq})</li>"
|
| 262 |
-
html_output += "</ul><h4>Top 10 Sugerencias de DuckDuckGo:</h4><ul>"
|
| 263 |
-
for suggestion, freq in duckduckgo_top_10:
|
| 264 |
-
html_output += f"<li>{suggestion} ({freq})</li>"
|
| 265 |
-
html_output += "</ul><h4>Top 10 Sugerencias de YouTube:</h4><ul>"
|
| 266 |
-
for suggestion, freq in youtube_top_10:
|
| 267 |
-
html_output += f"<li>{suggestion} ({freq})</li>"
|
| 268 |
-
html_output += "</ul><h4>Top 10 Sugerencias de Bing:</h4><ul>"
|
| 269 |
-
for suggestion, freq in bing_top_10:
|
| 270 |
-
html_output += f"<li>{suggestion} ({freq})</li>"
|
| 271 |
-
html_output += "</ul><h4>Top 10 Sugerencias de Amazon:</h4><ul>"
|
| 272 |
-
for suggestion, freq in amazon_top_10:
|
| 273 |
-
html_output += f"<li>{suggestion} ({freq})</li>"
|
| 274 |
-
html_output += "</ul><h4>Top 10 Sugerencias de Qwant:</h4><ul>"
|
| 275 |
-
for suggestion, freq in qwant_top_10:
|
| 276 |
-
html_output += f"<li>{suggestion} ({freq})</li>"
|
| 277 |
-
html_output += "</ul><h4>Top 10 Sugerencias de Brave:</h4><ul>"
|
| 278 |
-
for suggestion, freq in brave_top_10:
|
| 279 |
-
html_output += f"<li>{suggestion} ({freq})</li>"
|
| 280 |
-
html_output += "</ul>"
|
| 281 |
-
|
| 282 |
-
return html_output
|
| 283 |
|
|
|
|
|
|
|
| 284 |
|
| 285 |
-
# Interfaz de Gradio
|
| 286 |
iface = gr.Interface(
|
| 287 |
fn=main,
|
| 288 |
inputs="text",
|
| 289 |
-
outputs="
|
| 290 |
title="<div style='margin:0 auto;text-align:center'><div style='margin:0 auto;text-align:center'><img style='width:100px;display: inline-table;margin-bottom:-10px' src='https://artxeweb.com/media/files/search.jpg'><p>Sugerencias Combinadas de Google, DuckDuckGo, YouTube, Bing y Amazon</p></div>",
|
| 291 |
description="<p style='margin-bottom:10px;text-align:center;background: #ffffff; padding: 8px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb;'>Ingrese una palabra clave para obtener sugerencias de búsqueda relacionadas de Google, DuckDuckGo, YouTube, Bing y Amazon. Se mostrarán las 10 primeras sugerencias combinadas y también las 10 principales de cada plataforma por separado.</p>",
|
| 292 |
article="<div style='margin-top:10px'><p style='text-align: center !important; background: #ffffff; padding: 5px 30px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb; width: fit-content; margin: auto;'>Desarrollada por <a style='text-decoration: none !important; color: #e12a31 !important;' href='https://artxeweb.com'>© Artxe Web</a></p></div>"
|
|
|
|
| 5 |
import xmltodict
|
| 6 |
from collections import Counter
|
| 7 |
import unicodedata
|
| 8 |
+
import pandas as pd # Para manejar CSV
|
| 9 |
|
| 10 |
# Función para normalizar palabras clave (elimina tildes y convierte a minúsculas)
|
| 11 |
def normalize_keyword(keyword):
|
|
|
|
| 166 |
suggestion_counter = Counter(suggestions)
|
| 167 |
return suggestion_counter.most_common(top_n)
|
| 168 |
|
| 169 |
+
# Función principal que ahora también devuelve un archivo CSV
|
| 170 |
def main(keyword):
|
| 171 |
expanded_keywords = expand_keyword(keyword)
|
| 172 |
all_suggestions = {}
|
|
|
|
| 175 |
youtube_suggestions_all = []
|
| 176 |
bing_suggestions_all = []
|
| 177 |
amazon_suggestions_all = []
|
| 178 |
+
qwant_suggestions_all = []
|
| 179 |
+
brave_suggestions_all = []
|
| 180 |
|
| 181 |
# Obtener sugerencias de DuckDuckGo
|
| 182 |
for exp_keyword in expanded_keywords:
|
|
|
|
| 239 |
brave_top_10 = get_top_suggestions(brave_suggestions_all, top_n=10)
|
| 240 |
|
| 241 |
# Ordenar las sugerencias combinadas por frecuencia
|
| 242 |
+
combined_suggestions = sorted(all_suggestions.items(), key=lambda item: item[1], reverse=True)
|
|
|
|
| 243 |
|
| 244 |
+
# Crear un DataFrame de Pandas para las sugerencias y relevancias
|
| 245 |
+
df = pd.DataFrame(combined_suggestions, columns=["keyword", "relevancia"])
|
|
|
|
|
|
|
|
|
|
| 246 |
|
| 247 |
+
# Guardar el DataFrame en un archivo CSV
|
| 248 |
+
csv_file = "/tmp/keywords_relevancia.csv"
|
| 249 |
+
df.to_csv(csv_file, index=False)
|
| 250 |
+
|
| 251 |
+
# Crear el HTML de salida con las sugerencias (ya está hecho en tu código anterior)
|
| 252 |
html_output = f"""
|
| 253 |
<div>
|
| 254 |
+
<b>Sugerencias combinadas de Google, DuckDuckGo, YouTube, Bing, Amazon, Qwant y Brave (Top 10 combinadas):</b>
|
| 255 |
+
<span id='suggestions_text'>{', '.join([f'{sug} ({freq})' for sug, freq in combined_suggestions[:10]])}</span>
|
| 256 |
<button class="lg secondary svelte-cmf5ev" style="font-size: small; padding: 2px; color: #808080ba; border: none; margin-left: 5px;"
|
| 257 |
+
onclick='navigator.clipboard.writeText(document.getElementById("suggestions_text").innerText).then(() => alert("Texto copiado al portapapeles"))'>
|
| 258 |
+
✂ </button>
|
| 259 |
</div>
|
|
|
|
|
|
|
| 260 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 261 |
|
| 262 |
+
# Devolver el HTML y el archivo CSV
|
| 263 |
+
return html_output, csv_file
|
| 264 |
|
| 265 |
+
# Interfaz de Gradio con salida HTML y archivo descargable (CSV)
|
| 266 |
iface = gr.Interface(
|
| 267 |
fn=main,
|
| 268 |
inputs="text",
|
| 269 |
+
outputs=[gr.outputs.HTML(), gr.outputs.File(label="Descargar CSV")],
|
| 270 |
title="<div style='margin:0 auto;text-align:center'><div style='margin:0 auto;text-align:center'><img style='width:100px;display: inline-table;margin-bottom:-10px' src='https://artxeweb.com/media/files/search.jpg'><p>Sugerencias Combinadas de Google, DuckDuckGo, YouTube, Bing y Amazon</p></div>",
|
| 271 |
description="<p style='margin-bottom:10px;text-align:center;background: #ffffff; padding: 8px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb;'>Ingrese una palabra clave para obtener sugerencias de búsqueda relacionadas de Google, DuckDuckGo, YouTube, Bing y Amazon. Se mostrarán las 10 primeras sugerencias combinadas y también las 10 principales de cada plataforma por separado.</p>",
|
| 272 |
article="<div style='margin-top:10px'><p style='text-align: center !important; background: #ffffff; padding: 5px 30px; border-radius: 8px; border-width: 1px; border: solid 1px #e5e7eb; width: fit-content; margin: auto;'>Desarrollada por <a style='text-decoration: none !important; color: #e12a31 !important;' href='https://artxeweb.com'>© Artxe Web</a></p></div>"
|