Samagra07 commited on
Commit
f9e5853
·
verified ·
1 Parent(s): 55130d6

Upload 8 files

Browse files
agents/__pycache__/report_generator_agent.cpython-311.pyc ADDED
Binary file (1.75 kB). View file
 
agents/__pycache__/summarizer_agent.cpython-311.pyc ADDED
Binary file (2.54 kB). View file
 
agents/__pycache__/web_crawler_agent.cpython-311.pyc ADDED
Binary file (2.88 kB). View file
 
agents/report_generator_agent.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from crewai import Agent, Task
2
+
3
+ class ReportGeneratorAgent(Agent):
4
+ def __init__(self, llm):
5
+ super().__init__(
6
+ llm=llm,
7
+ role="report generator",
8
+ backstory="I generate a final comprehensive report without a bias",
9
+ goal="Create a detailed report summarizing the research findings."
10
+ )
11
+
12
+ def execute_task(self, task: Task, context: dict = None, tools: list = None):
13
+ """Generate a comprehensive report."""
14
+
15
+ summaries = context
16
+ report = self.generate_report(summaries)
17
+ return report
18
+
19
+ def generate_report(self, summaries):
20
+ """Generate a comprehensive research report."""
21
+ return self.llm.call([
22
+ {"role": "user", "content": summaries}
23
+ ])
agents/summarizer_agent.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from crewai import Agent, Task
2
+
3
+ class SummarizerAgent(Agent):
4
+ def __init__(self, llm):
5
+ super().__init__(
6
+ llm=llm,
7
+ role="Content Summarizer",
8
+ backstory="I summarize research content into concise insights.",
9
+ goal="Summarize research articles or content to provide clear and precise information."
10
+ )
11
+
12
+ def execute_task(self, task: Task, context: dict = None, tools: list = None):
13
+ """Execute the summarization task."""
14
+ if not context:
15
+ raise ValueError("Summarization task requires content in context.")
16
+
17
+ raw_content = context
18
+ if isinstance(raw_content, str):
19
+ summary = self.summarize(raw_content)
20
+ elif isinstance(raw_content, list) and all(isinstance(item, str) for item in raw_content):
21
+ summary = self.summarize("\n".join(raw_content))
22
+ else:
23
+ raise ValueError("Raw content must be a string or a list of strings.")
24
+
25
+ return summary
26
+
27
+ def summarize(self, content):
28
+ """Summarize the content using the LLM."""
29
+ return self.llm.call([
30
+ {"role": "user", "content": content}
31
+ ])
agents/web_crawler_agent.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from crewai import Agent, Task
2
+ import requests
3
+
4
+ class WebCrawlerAgent(Agent):
5
+ def __init__(self, llm, role, backstory, goal, serper_api_key):
6
+ super().__init__(llm=llm, role=role, backstory=backstory, goal=goal)
7
+ self._serper_api_key = serper_api_key
8
+
9
+ def web_crawl(self, query):
10
+ """Fetch search results from the Serper.dev API."""
11
+ url = "https://google.serper.dev/search"
12
+ headers = {"X-API-KEY": self._serper_api_key}
13
+ payload = {"q": query}
14
+
15
+ response = requests.post(url, json=payload, headers=headers)
16
+ if response.status_code == 200:
17
+ try:
18
+ results = response.json()
19
+ return results.get("organic", [])
20
+ except Exception as e:
21
+ raise ValueError(f"Failed to parse JSON: {e}")
22
+ else:
23
+ raise Exception(f"Serper API error: {response.status_code}, {response.text}")
24
+
25
+ def execute_task(self, task: Task, context: dict = None, tools: list = None):
26
+ """Execute the task by performing a web search and returning results as a string."""
27
+ query = task.description
28
+ if not query:
29
+ raise ValueError("Task description must include a 'query' field.")
30
+
31
+ search_results = self.web_crawl(query)
32
+
33
+ search_results_str = "\n".join(
34
+ [result.get('title', 'No Title') for result in search_results]
35
+ )
36
+
37
+ return search_results_str
app.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from crewai import Agent, Task, Crew, Process
4
+ from langchain_together import ChatTogether
5
+ from agents.web_crawler_agent import WebCrawlerAgent
6
+ from agents.summarizer_agent import SummarizerAgent
7
+ from agents.report_generator_agent import ReportGeneratorAgent
8
+ from dotenv import load_dotenv
9
+
10
+ load_dotenv()
11
+
12
+ model = os.getenv("MODEL_NAME")
13
+ os.environ['OPENAI_API_KEY'] = os.getenv("OPENAI_API_KEY")
14
+
15
+ llm = ChatTogether(model=model)
16
+
17
+ web_crawler_agent = WebCrawlerAgent(
18
+ llm=llm,
19
+ role="web crawler",
20
+ backstory="I search the web to gather information on a given topic.",
21
+ goal="Fetch web search results for a given query.",
22
+ serper_api_key=os.getenv("SERPER_API_KEY")
23
+ )
24
+
25
+
26
+ summarizer_agent = SummarizerAgent(llm)
27
+
28
+ report_generator_agent = ReportGeneratorAgent(llm=llm)
29
+
30
+ def generate_report(query, report_style):
31
+ web_crawler_task = Task(
32
+ description=query,
33
+ expected_output="A detailed response about the query.",
34
+ agent=web_crawler_agent
35
+ )
36
+
37
+ summarizer_task = Task(
38
+ description="Summarize the web data.",
39
+ expected_output="Concise summaries of the search results.",
40
+ agent=summarizer_agent
41
+ )
42
+
43
+ report_task = Task(
44
+ description=f"Generate a final research report in '{report_style}' style by removing bias if any.",
45
+ expected_output="A comprehensive research report.",
46
+ agent=report_generator_agent
47
+ )
48
+
49
+ crew = Crew(
50
+ agents=[web_crawler_agent, summarizer_agent, report_generator_agent],
51
+ tasks=[web_crawler_task, summarizer_task, report_task],
52
+ verbose=True,
53
+ process=Process.sequential,
54
+ context_flow={
55
+ "summarizer_task": "web_crawler_task",
56
+ "report_task": "summarizer_task",
57
+ }
58
+ )
59
+
60
+ result = crew.kickoff()
61
+ formatted_report = f"###### Report Generated in {report_style} Style:\n\n{result.raw}"
62
+
63
+ return formatted_report
64
+
65
+ with gr.Blocks() as demo:
66
+ gr.Markdown("## 🚀 Research Report Generation and Analysis Tool")
67
+ gr.Markdown("#### Enter your query and choose the report style to generate a detailed, formatted report!")
68
+
69
+ with gr.Row():
70
+ query_input = gr.Textbox(
71
+ label="Enter Your Query",
72
+ placeholder="e.g., Recent advancements in Indian Agriculture",
73
+ lines=2,
74
+ elem_id="query-input"
75
+ )
76
+
77
+ with gr.Row():
78
+ report_style = gr.Radio(
79
+ choices=[ "Detailed Analysis", "Brief Summary","Bullet Points"],
80
+ label="Select Report Style",
81
+ value="Detailed Analysis"
82
+ )
83
+
84
+ with gr.Row():
85
+ generate_button = gr.Button("Generate Report", elem_id="generate-button", variant="primary")
86
+
87
+ with gr.Row():
88
+ report_output = gr.Markdown(
89
+ label="Generated Report"
90
+ )
91
+
92
+ generate_button.click(
93
+ generate_report,
94
+ inputs=[query_input, report_style],
95
+ outputs=[report_output]
96
+ )
97
+
98
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ crewai
3
+ requests
4
+ python-dotenv
5
+ langchain-together