Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import cohere
|
|
|
4 |
from crewai import Agent, Task, Crew, Process
|
5 |
|
6 |
from langchain_groq import ChatGroq
|
@@ -8,6 +9,7 @@ from langchain_cohere import ChatCohere
|
|
8 |
|
9 |
from langchain_community.tools import DuckDuckGoSearchRun, DuckDuckGoSearchResults
|
10 |
from crewai_tools import tool, SeleniumScrapingTool, ScrapeWebsiteTool
|
|
|
11 |
|
12 |
# Ensure essential environment variables are set
|
13 |
cohere_api_key = os.getenv('COHERE_API_KEY')
|
@@ -20,12 +22,19 @@ if not groq_api_key:
|
|
20 |
# Initialize API clients
|
21 |
co = cohere.Client(cohere_api_key)
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
-
# def search(search_query: str):
|
26 |
-
# """Search the web for information on a given topic."""
|
27 |
-
# return DuckDuckGoSearchRun().run(search_query)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
29 |
|
30 |
# Define the DuckDuckGoSearch tool
|
31 |
@tool('DuckDuckGoSearchResults')
|
@@ -38,10 +47,9 @@ def search_results(search_query: str) -> dict:
|
|
38 |
Returns:
|
39 |
- list: A list of dictionaries, where each dictionary represents a search result. Each dictionary includes 'snippet' of the page and the 'link' with the url linking to it.
|
40 |
"""
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
return results
|
45 |
|
46 |
@tool('WebScrapper')
|
47 |
def web_scrapper(url: str, topic: str) -> str:
|
@@ -57,8 +65,7 @@ def web_scrapper(url: str, topic: str) -> str:
|
|
57 |
- summary (str): summary of the url on the topic
|
58 |
"""
|
59 |
# Scrape content from the specified URL
|
60 |
-
|
61 |
-
content = scraper.run()
|
62 |
|
63 |
# Prepare the prompt for generating the summary
|
64 |
prompt = f"Generate a summary of the following content on the topic ## {topic} ### \n\nCONTENT:\n\n" + content
|
@@ -195,4 +202,4 @@ def main():
|
|
195 |
demo.queue(api_open=False, max_size=3).launch()
|
196 |
|
197 |
if __name__ == "__main__":
|
198 |
-
main()
|
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
import cohere
|
4 |
+
import requests
|
5 |
from crewai import Agent, Task, Crew, Process
|
6 |
|
7 |
from langchain_groq import ChatGroq
|
|
|
9 |
|
10 |
from langchain_community.tools import DuckDuckGoSearchRun, DuckDuckGoSearchResults
|
11 |
from crewai_tools import tool, SeleniumScrapingTool, ScrapeWebsiteTool
|
12 |
+
from duckduckgo_search import DDGS # Asegúrate de tener instalado el paquete duckduckgo_search
|
13 |
|
14 |
# Ensure essential environment variables are set
|
15 |
cohere_api_key = os.getenv('COHERE_API_KEY')
|
|
|
22 |
# Initialize API clients
|
23 |
co = cohere.Client(cohere_api_key)
|
24 |
|
25 |
+
# Jina.ai API key
|
26 |
+
jina_api_key = os.getenv('JINA_API_KEY', 'jina_e62a3177429a4ab199bbbce5d8a3efb2sZB9jmZTZme67gGtEeldrlBYO2KP')
|
|
|
|
|
|
|
27 |
|
28 |
+
def fetch_content(url):
|
29 |
+
try:
|
30 |
+
response = requests.get(
|
31 |
+
f"https://r.jina.ai/{url}",
|
32 |
+
headers={"Authorization": f"Bearer {jina_api_key}"}
|
33 |
+
)
|
34 |
+
response.raise_for_status()
|
35 |
+
return response.text
|
36 |
+
except requests.exceptions.RequestException as e:
|
37 |
+
return f"Error fetching content: {e}"
|
38 |
|
39 |
# Define the DuckDuckGoSearch tool
|
40 |
@tool('DuckDuckGoSearchResults')
|
|
|
47 |
Returns:
|
48 |
- list: A list of dictionaries, where each dictionary represents a search result. Each dictionary includes 'snippet' of the page and the 'link' with the url linking to it.
|
49 |
"""
|
50 |
+
results = DDGS().text(search_query, max_results=5, timelimit='m')
|
51 |
+
results_list = [{"title": result['title'], "snippet": result['body'], "link": result['href']} for result in results]
|
52 |
+
return results_list
|
|
|
53 |
|
54 |
@tool('WebScrapper')
|
55 |
def web_scrapper(url: str, topic: str) -> str:
|
|
|
65 |
- summary (str): summary of the url on the topic
|
66 |
"""
|
67 |
# Scrape content from the specified URL
|
68 |
+
content = fetch_content(url)
|
|
|
69 |
|
70 |
# Prepare the prompt for generating the summary
|
71 |
prompt = f"Generate a summary of the following content on the topic ## {topic} ### \n\nCONTENT:\n\n" + content
|
|
|
202 |
demo.queue(api_open=False, max_size=3).launch()
|
203 |
|
204 |
if __name__ == "__main__":
|
205 |
+
main()
|