Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -23,20 +23,8 @@ model = HfApiModel(
|
|
23 |
|
24 |
@tool
|
25 |
def save_scraped_data_as_markdown(scraped_data: dict, filename: str = None) -> str:
|
26 |
-
"""Convierte el contenido scrapeado en un archivo Markdown mejor estructurado.
|
27 |
|
28 |
-
Mejoras:
|
29 |
-
- Resumen automático del contenido con NLP.
|
30 |
-
- Uso de encabezados, listas y negritas en Markdown.
|
31 |
-
- Guardado con timestamp para evitar sobrescribir archivos.
|
32 |
-
|
33 |
-
Args:
|
34 |
-
scraped_data: Diccionario con la URL y los datos extraídos.
|
35 |
-
filename: Nombre del archivo de salida (si no se da, se genera con timestamp).
|
36 |
-
|
37 |
-
Returns:
|
38 |
-
Mensaje de confirmación o error.
|
39 |
-
"""
|
40 |
try:
|
41 |
url = scraped_data.get("url", "Desconocido")
|
42 |
content_list = scraped_data.get("scraped_data", [])
|
@@ -44,37 +32,25 @@ def save_scraped_data_as_markdown(scraped_data: dict, filename: str = None) -> s
|
|
44 |
if not content_list:
|
45 |
return "No hay datos para guardar en Markdown."
|
46 |
|
47 |
-
|
48 |
-
tokenized_sentences = [sent_tokenize(text) for text in content_list]
|
49 |
-
formatted_content = "\n\n".join([" ".join(sentences) for sentences in tokenized_sentences])
|
50 |
|
51 |
-
#
|
52 |
if len(formatted_content.split()) > 100:
|
53 |
-
summarized_text =
|
54 |
-
prompt=f"Resume el siguiente texto:\n\n{formatted_content[:1024]}",
|
55 |
-
max_length=150,
|
56 |
-
min_length=50,
|
57 |
-
)
|
58 |
else:
|
59 |
summarized_text = formatted_content
|
60 |
|
61 |
-
#
|
62 |
markdown_content = f"# Contenido extraído de {url}\n\n"
|
63 |
markdown_content += f"## Resumen\n\n> {summarized_text}\n\n"
|
64 |
markdown_content += "## Contenido Completo\n\n"
|
|
|
65 |
|
66 |
-
|
67 |
-
if len(paragraph.split()) > 10: # Si el párrafo es largo, lo tratamos como sección
|
68 |
-
markdown_content += f"### {paragraph[:50]}...\n\n{paragraph}\n\n"
|
69 |
-
else:
|
70 |
-
markdown_content += f"- **{paragraph}**\n\n"
|
71 |
-
|
72 |
-
# Generar nombre con timestamp si no se proporciona
|
73 |
if not filename:
|
74 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
75 |
filename = f"scraped_{timestamp}.md"
|
76 |
|
77 |
-
# Guardar en un archivo Markdown
|
78 |
with open(filename, "w", encoding="utf-8") as file:
|
79 |
file.write(markdown_content)
|
80 |
|
@@ -83,6 +59,24 @@ def save_scraped_data_as_markdown(scraped_data: dict, filename: str = None) -> s
|
|
83 |
except Exception as e:
|
84 |
return f"Error al generar el archivo Markdown: {str(e)}"
|
85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
@tool
|
88 |
def scrape_webpage(url: str, tag: str = "p", class_name: str = None) -> dict:
|
|
|
23 |
|
24 |
@tool
|
25 |
def save_scraped_data_as_markdown(scraped_data: dict, filename: str = None) -> str:
|
26 |
+
"""Convierte el contenido scrapeado en un archivo Markdown mejor estructurado."""
|
27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
try:
|
29 |
url = scraped_data.get("url", "Desconocido")
|
30 |
content_list = scraped_data.get("scraped_data", [])
|
|
|
32 |
if not content_list:
|
33 |
return "No hay datos para guardar en Markdown."
|
34 |
|
35 |
+
formatted_content = "\n\n".join(content_list)
|
|
|
|
|
36 |
|
37 |
+
# Resumir el contenido si es muy largo
|
38 |
if len(formatted_content.split()) > 100:
|
39 |
+
summarized_text = summarize_text(formatted_content[:1024])
|
|
|
|
|
|
|
|
|
40 |
else:
|
41 |
summarized_text = formatted_content
|
42 |
|
43 |
+
# Estructurar en Markdown
|
44 |
markdown_content = f"# Contenido extraído de {url}\n\n"
|
45 |
markdown_content += f"## Resumen\n\n> {summarized_text}\n\n"
|
46 |
markdown_content += "## Contenido Completo\n\n"
|
47 |
+
markdown_content += formatted_content
|
48 |
|
49 |
+
# Guardar el archivo con timestamp
|
|
|
|
|
|
|
|
|
|
|
|
|
50 |
if not filename:
|
51 |
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
|
52 |
filename = f"scraped_{timestamp}.md"
|
53 |
|
|
|
54 |
with open(filename, "w", encoding="utf-8") as file:
|
55 |
file.write(markdown_content)
|
56 |
|
|
|
59 |
except Exception as e:
|
60 |
return f"Error al generar el archivo Markdown: {str(e)}"
|
61 |
|
62 |
+
@tool
|
63 |
+
|
64 |
+
def summarize_text(text):
|
65 |
+
"""Usa una API de Hugging Face para resumir texto."""
|
66 |
+
API_URL = "https://api-inference.huggingface.co/models/facebook/bart-large-cnn"
|
67 |
+
headers = {"Authorization": f"Bearer TU_HUGGINGFACE_API_KEY"}
|
68 |
+
|
69 |
+
payload = {
|
70 |
+
"inputs": text,
|
71 |
+
"parameters": {"max_length": 150, "min_length": 50, "do_sample": False},
|
72 |
+
}
|
73 |
+
|
74 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
75 |
+
|
76 |
+
if response.status_code == 200:
|
77 |
+
return response.json()[0]["summary_text"]
|
78 |
+
else:
|
79 |
+
return "Error en el resumen"
|
80 |
|
81 |
@tool
|
82 |
def scrape_webpage(url: str, tag: str = "p", class_name: str = None) -> dict:
|