Daryl Lim commited on
Commit
1c30122
·
1 Parent(s): 6ce1f51

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +206 -0
app.py ADDED
@@ -0,0 +1,206 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This application provides an interface for generating text using the CrewAI framework
3
+ for orchestrating agents, and models powered by Groq.
4
+ The interface allows users to input a topic and receive a generated research article.
5
+ """
6
+
7
+ import gradio as gr
8
+ from crewai import Agent, Task, Crew, LLM, Process
9
+ from crewai_tools import tool
10
+ from langchain_community.tools import DuckDuckGoSearchRun
11
+
12
+ # Use DuckDuckGo Search component
13
+ @tool('DuckDuckGoSearch')
14
+ def search(query: str) -> str:
15
+ """
16
+ Perform a web search using DuckDuckGo.
17
+
18
+ Args:
19
+ query (str): The search query to be executed.
20
+
21
+ Returns:
22
+ str: The search result from DuckDuckGo.
23
+
24
+ Raises:
25
+ ValueError: If the query is an empty string.
26
+ Exception: For any unexpected error during the search execution.
27
+ """
28
+ if not query.strip():
29
+ raise ValueError("Query must not be empty.")
30
+
31
+ try:
32
+ result = DuckDuckGoSearchRun().run(query)
33
+ except Exception as error:
34
+ raise Exception(f"Search execution error: {error}")
35
+
36
+ return result
37
+
38
+ def generate_response(topic: str, api_key: str) -> str:
39
+ """
40
+ Generates a research article based on the input topic using a Groq model.
41
+
42
+ Args:
43
+ topic (str): The text of the topic to be researched.
44
+ api_key (str): The API key to authenticate the request to Groq.
45
+
46
+ Returns:
47
+ str: The generated research article or an error message.
48
+ """
49
+ if not topic:
50
+ return "Error: No topic provided. Please input a valid topic."
51
+
52
+ try:
53
+ # Configure the large language model LLM
54
+ llm = LLM(
55
+ model="groq/llama-3.1-8b-instant",
56
+ temperature=0.2,
57
+ top_p=0.5,
58
+ max_tokens=500,
59
+ presence_penalty=0.1,
60
+ frequency_penalty=0.1,
61
+ api_key=api_key
62
+ )
63
+ except Exception as error:
64
+ return f"Error configuring LLM: {str(error)}"
65
+
66
+ # Create the 'Researcher' agent
67
+ researcher = Agent(
68
+ role="Researcher",
69
+ goal="Conduct foundational research on {topic}.",
70
+ backstory="A researcher who identifies trends and patterns.",
71
+ llm=llm,
72
+ max_iter=3,
73
+ max_rpm=15,
74
+ max_execution_time=30,
75
+ verbose=True,
76
+ allow_delegation=False
77
+ )
78
+
79
+ # Create the 'Writer' agent
80
+ writer = Agent(
81
+ role="Writer",
82
+ goal="Write a clear and concise research article on {topic}.",
83
+ backstory="A writer who provides objective and impartial insights.",
84
+ llm=llm,
85
+ max_iter=3,
86
+ max_rpm=15,
87
+ max_execution_time=30,
88
+ verbose=True,
89
+ allow_delegation=False
90
+ )
91
+
92
+ # Create the research task
93
+ research = Task(
94
+ description="Find current and relevant information on {topic}.",
95
+ agent=researcher,
96
+ expected_output="A bullet list outline for a concise research article.",
97
+ tools=[search]
98
+ )
99
+
100
+ # Create the writing task
101
+ write = Task(
102
+ description="Write a clear and concise research article on {topic}.",
103
+ expected_output="An article with an introduction, main points, and a conclusion.",
104
+ agent=writer,
105
+ context=[research]
106
+ )
107
+
108
+ # Define the crew of agents and tasks
109
+ crew = Crew(
110
+ agents=[researcher, writer],
111
+ tasks=[research, write],
112
+ process=Process.sequential,
113
+ verbose=True
114
+ )
115
+
116
+ try:
117
+ # Inputs for the crew process
118
+ inputs = {"topic": topic}
119
+
120
+ # Execute the crew kickoff process
121
+ crew_output = crew.kickoff(inputs=inputs)
122
+
123
+ # Access the crew output
124
+ article = crew_output.raw if crew_output else "Error: No output from crew."
125
+
126
+ # Return the crew output
127
+ return article
128
+ except Exception as error:
129
+ return f"Error during crew execution: {str(e)}"
130
+
131
+ # Custom CSS for the Groq badge and color scheme
132
+ custom_css = """
133
+ .gradio-container {
134
+ background-color: #f5f5f5;
135
+ }
136
+ .gr-button-primary {
137
+ background-color: #f55036 !important;
138
+ border-color: #f55036 !important;
139
+ }
140
+ .gr-button-secondary {
141
+ color: #f55036 !important;
142
+ border-color: #f55036 !important;
143
+ }
144
+ #groq-badge {
145
+ position: fixed;
146
+ bottom: 20px;
147
+ right: 20px;
148
+ z-index: 1000;
149
+ }
150
+ """
151
+
152
+ # Define the Gradio interface
153
+ with gr.Blocks(theme=gr.themes.Default()) as demo:
154
+ gr.Markdown("# CrewAI + Groq Research Assistant")
155
+
156
+ # Textbox to input the Groq API key
157
+ api_key_input = gr.Textbox(
158
+ type="password",
159
+ label="Enter Groq API key",
160
+ placeholder="Enter your API key...",
161
+ lines=1
162
+ )
163
+
164
+ # Textbox to input the research topic
165
+ with gr.Row():
166
+ topic_input = gr.Textbox(
167
+ label="Enter Topic",
168
+ placeholder="Type a topic here...",
169
+ lines=1
170
+ )
171
+
172
+ # Markdown output to display the generated article
173
+ with gr.Row():
174
+ article_output = gr.Markdown(label="Generated Research Article")
175
+
176
+ # Submit button
177
+ submit_button = gr.Button("Generate Article", variant="primary")
178
+
179
+ # Add the Groq badge
180
+ gr.HTML(
181
+ """
182
+ <div id="groq-badge">
183
+ <div style="color: #f55036; font-weight: bold;">POWERED BY GROQ</div>
184
+ </div>
185
+ """
186
+ )
187
+
188
+ # Connect button click to the generate_response function
189
+ submit_button.click(
190
+ generate_response,
191
+ inputs=[topic_input, api_key_input],
192
+ outputs=[article_output]
193
+ )
194
+
195
+ # Markdown instructions for using the app
196
+ gr.Markdown(
197
+ """
198
+ ## How to use this app:
199
+ 1. Enter your [Groq API Key](https://console.groq.com/keys) in the provided field.
200
+ 2. Enter a topic.
201
+ 3. Click the "Generate Article" button to generate a research article.
202
+ """
203
+ )
204
+
205
+ # Launch the Gradio interface
206
+ demo.launch()