Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from crewai import Agent, Task, Crew, Process
|
2 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
3 |
+
from crewai_tools import SerperDevTool
|
4 |
+
import os
|
5 |
+
from langchain_community.utilities import GoogleSerperAPIWrapper
|
6 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
7 |
+
from langchain_openai import OpenAI
|
8 |
+
from langchain.agents import initialize_agent, Tool
|
9 |
+
from crewai_tools import tool
|
10 |
+
import gradio as gr
|
11 |
+
|
12 |
+
|
13 |
+
@tool('DuckDuckGoSearch')
|
14 |
+
def search(search_query: str):
|
15 |
+
"""Search the web for information on a given topic"""
|
16 |
+
return DuckDuckGoSearchRun().run(search_query)
|
17 |
+
|
18 |
+
|
19 |
+
|
20 |
+
llm = ChatGoogleGenerativeAI(
|
21 |
+
model="gemini-pro", verbose=True, temperature=0.1, google_api_key=os.getenv('GOOGLE_API_KEY')
|
22 |
+
)
|
23 |
+
|
24 |
+
search = DuckDuckGoSearchRun()
|
25 |
+
@tool('DuckDuckGoSearch')
|
26 |
+
def search(search_query: str):
|
27 |
+
"""Search the web for information on a given topic"""
|
28 |
+
return DuckDuckGoSearchRun(max_results=5).run(search_query)
|
29 |
+
|
30 |
+
SerpBot = Agent(
|
31 |
+
role='Search Expert',
|
32 |
+
goal=' You find all the information that the Writer requests you to find. Then you compile it and give it back to him.',
|
33 |
+
backstory="""
|
34 |
+
You are an agent that uses SERP in the best way to search for the input query. Action input
|
35 |
+
is the following format: {"search_query"="input text"}
|
36 |
+
""",
|
37 |
+
verbose=True,
|
38 |
+
tools=[search],
|
39 |
+
llm=llm
|
40 |
+
)
|
41 |
+
|
42 |
+
Writer = Agent(
|
43 |
+
role='Expert Writer',
|
44 |
+
goal='Write engaging, well written articles with a pinch of humour',
|
45 |
+
backstory="""
|
46 |
+
A seasoned funny writer who writes about the latest news on that topic and
|
47 |
+
can convert boring information to amazing articles that everyone loves reading.
|
48 |
+
Your articles have a satirical tone and the humour is based on real life events
|
49 |
+
""",
|
50 |
+
verbose=True,
|
51 |
+
allow_delegation=True,
|
52 |
+
tools=[search],
|
53 |
+
llm=llm
|
54 |
+
)
|
55 |
+
|
56 |
+
def get_human_input(input):
|
57 |
+
"""Get the human input for the search query"""
|
58 |
+
human_input = input
|
59 |
+
return human_input
|
60 |
+
|
61 |
+
def create_task(human_input):
|
62 |
+
"""Create a task based on the human input"""
|
63 |
+
task1 = Task(
|
64 |
+
description=f"Write a newsletter based on the following topics: +{human_input}. Research on topic separately and write detailed articles about them",
|
65 |
+
agent = Writer,
|
66 |
+
human_feedback=True,
|
67 |
+
expected_output="When the entire newsletter is generated",
|
68 |
+
)
|
69 |
+
return task1
|
70 |
+
|
71 |
+
def create_crew(task):
|
72 |
+
"""Create a crew with the task and agents"""
|
73 |
+
story_crew = Crew(
|
74 |
+
agents=[SerpBot, Writer],
|
75 |
+
tasks=[task],
|
76 |
+
verbose=True,
|
77 |
+
process=Process.sequential,
|
78 |
+
)
|
79 |
+
return story_crew
|
80 |
+
|
81 |
+
def kickoff_crew(story_crew):
|
82 |
+
"""Kick off the crew"""
|
83 |
+
story_output = story_crew.kickoff()
|
84 |
+
return story_output
|
85 |
+
|
86 |
+
def response(input,history=[]):
|
87 |
+
human_input = get_human_input(input)
|
88 |
+
task = create_task(human_input)
|
89 |
+
story_crew = create_crew(task)
|
90 |
+
res = kickoff_crew(story_crew)
|
91 |
+
return res
|
92 |
+
|
93 |
+
|
94 |
+
#story_output = kickoff_crew(story_crew)
|
95 |
+
gr.ChatInterface(response).launch(share=True, debug=True)
|
96 |
+
|
97 |
+
|