Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,9 @@ import yaml
|
|
| 6 |
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from duckduckgo_search import DDGS
|
| 9 |
-
|
|
|
|
|
|
|
| 10 |
|
| 11 |
from Gradio_UI import GradioUI
|
| 12 |
|
|
@@ -23,6 +25,36 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 23 |
|
| 24 |
#from datasets import load_dataset
|
| 25 |
#ds = load_dataset("nfliu/decontextualization")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
@tool
|
| 27 |
def answer_nfl_question_tool(question: str) -> str:
|
| 28 |
"""
|
|
@@ -55,6 +87,14 @@ def answer_nfl_question_tool(question: str) -> str:
|
|
| 55 |
detailed_results.append("-" * 30 + "\n")
|
| 56 |
detailed_results.append(f"Title: {result.get('title', 'No title available')}\n")
|
| 57 |
detailed_results.append(f"Description: {result.get('snippet', 'No description available')}\n")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
return "\n".join(detailed_results)
|
| 60 |
|
|
|
|
| 6 |
|
| 7 |
from tools.final_answer import FinalAnswerTool
|
| 8 |
from duckduckgo_search import DDGS
|
| 9 |
+
import requests
|
| 10 |
+
from bs4 import BeautifulSoup
|
| 11 |
+
import time
|
| 12 |
|
| 13 |
from Gradio_UI import GradioUI
|
| 14 |
|
|
|
|
| 25 |
|
| 26 |
#from datasets import load_dataset
|
| 27 |
#ds = load_dataset("nfliu/decontextualization")
|
| 28 |
+
def get_page_content(url):
|
| 29 |
+
"""
|
| 30 |
+
Obtiene el contenido de una página web
|
| 31 |
+
"""
|
| 32 |
+
try:
|
| 33 |
+
# Añadir headers para evitar bloqueos
|
| 34 |
+
headers = {
|
| 35 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 36 |
+
}
|
| 37 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
|
| 40 |
+
# Parsear el contenido HTML
|
| 41 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 42 |
+
|
| 43 |
+
# Eliminar scripts y estilos
|
| 44 |
+
for script in soup(["script", "style"]):
|
| 45 |
+
script.decompose()
|
| 46 |
+
|
| 47 |
+
# Obtener el texto
|
| 48 |
+
text = soup.get_text()
|
| 49 |
+
|
| 50 |
+
# Limpiar el texto
|
| 51 |
+
lines = (line.strip() for line in text.splitlines())
|
| 52 |
+
text = ' '.join(chunk for chunk in lines if chunk)
|
| 53 |
+
|
| 54 |
+
return text[:1000] # Devolver los primeros 1000 caracteres
|
| 55 |
+
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return f"Error al acceder a la página: {str(e)}"
|
| 58 |
@tool
|
| 59 |
def answer_nfl_question_tool(question: str) -> str:
|
| 60 |
"""
|
|
|
|
| 87 |
detailed_results.append("-" * 30 + "\n")
|
| 88 |
detailed_results.append(f"Title: {result.get('title', 'No title available')}\n")
|
| 89 |
detailed_results.append(f"Description: {result.get('snippet', 'No description available')}\n")
|
| 90 |
+
|
| 91 |
+
# Obtener y añadir el contenido de la página
|
| 92 |
+
print(f"📄 Accessing page {i}...")
|
| 93 |
+
page_content = get_page_content(url)
|
| 94 |
+
detailed_results.append(f"Content: {page_content}\n")
|
| 95 |
+
|
| 96 |
+
# Añadir un pequeño delay entre peticiones
|
| 97 |
+
time.sleep(1)
|
| 98 |
|
| 99 |
return "\n".join(detailed_results)
|
| 100 |
|