Upload app.py
Browse files
app.py
CHANGED
@@ -0,0 +1,172 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from crewai import Agent, Task, Crew, Process
|
2 |
+
from crewai_tools import SerperDevTool
|
3 |
+
import gradio as gr
|
4 |
+
import os
|
5 |
+
import markdown as md
|
6 |
+
from datetime import datetime
|
7 |
+
|
8 |
+
# Model options
|
9 |
+
llm_models = [
|
10 |
+
"gemini/gemini-1.5-flash",
|
11 |
+
"gemini/gemini-1.5-pro",
|
12 |
+
"gemini/gemini-pro"
|
13 |
+
]
|
14 |
+
|
15 |
+
selected_model = llm_models[0]
|
16 |
+
|
17 |
+
|
18 |
+
def set_model(selected_model_name):
|
19 |
+
global selected_model
|
20 |
+
selected_model = selected_model_name
|
21 |
+
# return f"Model set to: {selected_model}"
|
22 |
+
|
23 |
+
|
24 |
+
def toggle_serper_input(choice):
|
25 |
+
return gr.Textbox(visible=(choice == "Yes"))
|
26 |
+
|
27 |
+
|
28 |
+
def run_crew(gemini_api_key, search_choice, serper_api_key, topic):
|
29 |
+
try:
|
30 |
+
# Validate inputs
|
31 |
+
if not gemini_api_key:
|
32 |
+
raise ValueError("Gemini API key is required")
|
33 |
+
|
34 |
+
os.environ['GEMINI_API_KEY'] = gemini_api_key
|
35 |
+
|
36 |
+
# Configure search tool
|
37 |
+
search_tool = None
|
38 |
+
if search_choice == "Yes":
|
39 |
+
if not serper_api_key:
|
40 |
+
raise ValueError("Serper API key is required for online search")
|
41 |
+
os.environ['SERPER_API_KEY'] = serper_api_key
|
42 |
+
search_tool = SerperDevTool()
|
43 |
+
|
44 |
+
# Create agents
|
45 |
+
researcher = Agent(
|
46 |
+
role="Online Research Specialist",
|
47 |
+
goal="Aggregate comprehensive information on {topic}",
|
48 |
+
verbose=True,
|
49 |
+
backstory="Expert research analyst with data sourcing expertise",
|
50 |
+
tools=[search_tool] if search_tool else [],
|
51 |
+
llm=selected_model,
|
52 |
+
allow_delegation=True
|
53 |
+
)
|
54 |
+
|
55 |
+
content_writer = Agent(
|
56 |
+
role="Expert Content Writer",
|
57 |
+
goal="Create SEO-optimized content on {topic}",
|
58 |
+
verbose=True,
|
59 |
+
backstory="Professional writer with digital journalism background",
|
60 |
+
tools=[],
|
61 |
+
llm=selected_model,
|
62 |
+
allow_delegation=False
|
63 |
+
)
|
64 |
+
|
65 |
+
# Create tasks
|
66 |
+
research_task = Task(
|
67 |
+
description=f"Conduct SEO research on '{topic}'",
|
68 |
+
expected_output="Detailed research report with SEO recommendations",
|
69 |
+
tools=[search_tool] if search_tool else [],
|
70 |
+
agent=researcher
|
71 |
+
)
|
72 |
+
|
73 |
+
writer_task = Task(
|
74 |
+
description=f"Write SEO-optimized article on '{topic}'",
|
75 |
+
expected_output="Polished article draft ready for publication",
|
76 |
+
agent=content_writer,
|
77 |
+
output_file="content.md"
|
78 |
+
)
|
79 |
+
|
80 |
+
# Create and run crew
|
81 |
+
crew = Crew(
|
82 |
+
agents=[researcher, content_writer],
|
83 |
+
tasks=[research_task, writer_task],
|
84 |
+
process=Process.sequential,
|
85 |
+
verbose=True,
|
86 |
+
max_rpm=100,
|
87 |
+
share_crew=True,
|
88 |
+
output_log_file=True
|
89 |
+
)
|
90 |
+
|
91 |
+
result = crew.kickoff(inputs={'topic': topic})
|
92 |
+
|
93 |
+
# Return results
|
94 |
+
with open("content.md", "r") as f:
|
95 |
+
content = f.read()
|
96 |
+
with open("logs.txt", 'r') as f:
|
97 |
+
logs = f.read()
|
98 |
+
os.remove("logs.txt")
|
99 |
+
print(f"{datetime.now()}::{topic}-->{content}")
|
100 |
+
return content, logs
|
101 |
+
|
102 |
+
except Exception as e:
|
103 |
+
return f"Error: {str(e)}", str(e)
|
104 |
+
|
105 |
+
|
106 |
+
# UI Setup
|
107 |
+
with gr.Blocks(theme=gr.themes.Soft(font=[gr.themes.GoogleFont("Roboto Mono")]),
|
108 |
+
css='footer {visibility: hidden}') as demo:
|
109 |
+
gr.Markdown("# AI Agents 🤖🕵🏻")
|
110 |
+
|
111 |
+
with gr.Tabs():
|
112 |
+
with gr.TabItem("Intro"):
|
113 |
+
gr.Markdown(md.description)
|
114 |
+
|
115 |
+
with gr.TabItem("SEO Content Generator Agent"):
|
116 |
+
with gr.Accordion("How to get GEMINI API KEY ============================================", open=False):
|
117 |
+
gr.Markdown(md.gemini_api_key)
|
118 |
+
with gr.Accordion("How to get SERPER API KEY ============================================", open=False):
|
119 |
+
gr.Markdown(md.serper_api_key)
|
120 |
+
with gr.Row():
|
121 |
+
with gr.Column(scale=1):
|
122 |
+
model_dropdown = gr.Dropdown(
|
123 |
+
llm_models,
|
124 |
+
label="1. Select AI Model",
|
125 |
+
value=llm_models[0]
|
126 |
+
)
|
127 |
+
gemini_key = gr.Textbox(
|
128 |
+
label="2. Enter Gemini API Key",
|
129 |
+
type="password",
|
130 |
+
placeholder="Paste your Gemini API key here..."
|
131 |
+
)
|
132 |
+
search_toggle = gr.Radio(
|
133 |
+
["Yes", "No"],
|
134 |
+
label="3. Enable Online Search?",
|
135 |
+
value="No"
|
136 |
+
)
|
137 |
+
serper_key = gr.Textbox(
|
138 |
+
label="4. Enter Serper API Key",
|
139 |
+
type="password",
|
140 |
+
visible=False,
|
141 |
+
placeholder="Paste Serper API key if enabled..."
|
142 |
+
)
|
143 |
+
topic_input = gr.Textbox(
|
144 |
+
label="5. Enter Content Topic",
|
145 |
+
placeholder="Enter your article topic here..."
|
146 |
+
)
|
147 |
+
run_btn = gr.Button("Generate Content", variant="primary")
|
148 |
+
|
149 |
+
with gr.Column(scale=3):
|
150 |
+
output = gr.Markdown(
|
151 |
+
label="Generated Content",
|
152 |
+
value="Your content will appear here..."
|
153 |
+
)
|
154 |
+
with gr.Accordion("Process Logs", open=False):
|
155 |
+
logs = gr.Markdown()
|
156 |
+
|
157 |
+
# Event handlers
|
158 |
+
model_dropdown.change(set_model, model_dropdown)
|
159 |
+
search_toggle.change(
|
160 |
+
toggle_serper_input,
|
161 |
+
inputs=search_toggle,
|
162 |
+
outputs=serper_key
|
163 |
+
)
|
164 |
+
run_btn.click(
|
165 |
+
run_crew,
|
166 |
+
inputs=[gemini_key, search_toggle, serper_key, topic_input],
|
167 |
+
outputs=[output, logs],
|
168 |
+
show_progress="full"
|
169 |
+
)
|
170 |
+
|
171 |
+
if __name__ == "__main__":
|
172 |
+
demo.launch()
|