Spaces:
No application file
No application file
typesdigital
commited on
Create app.py
Browse files- crewAI-main/app.py +101 -0
crewAI-main/app.py
ADDED
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import gradio as gr
|
3 |
+
from crewai import Agent, Task, Crew, Process
|
4 |
+
from crewai_tools import SerperDevTool
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
|
7 |
+
# Set up environment variables
|
8 |
+
os.environ["GROQ_API_KEY"] = "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"
|
9 |
+
os.environ["SERPER_API_KEY"] = "206256c6acfbcd5a46195f3312aaa7e8ed38ae5f"
|
10 |
+
|
11 |
+
|
12 |
+
# Initialize Groq LLM
|
13 |
+
groq_llm = ChatGroq(
|
14 |
+
model_name="mixtral-8x7b-32768",
|
15 |
+
temperature=0.7,
|
16 |
+
max_tokens=32768
|
17 |
+
)
|
18 |
+
|
19 |
+
# Initialize search tool
|
20 |
+
search_tool = SerperDevTool()
|
21 |
+
|
22 |
+
# Define agents
|
23 |
+
researcher = Agent(
|
24 |
+
role='Senior Research Analyst',
|
25 |
+
goal='Uncover cutting-edge developments in AI and data science',
|
26 |
+
backstory="""You work at a leading tech think tank.
|
27 |
+
Your expertise lies in identifying emerging trends.
|
28 |
+
You have a knack for dissecting complex data and presenting actionable insights.""",
|
29 |
+
verbose=True,
|
30 |
+
allow_delegation=False,
|
31 |
+
llm=groq_llm,
|
32 |
+
tools=[search_tool]
|
33 |
+
)
|
34 |
+
|
35 |
+
writer = Agent(
|
36 |
+
role='Tech Content Strategist',
|
37 |
+
goal='Craft compelling content on tech advancements',
|
38 |
+
backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
|
39 |
+
You transform complex concepts into compelling narratives.""",
|
40 |
+
verbose=True,
|
41 |
+
allow_delegation=True,
|
42 |
+
llm=groq_llm
|
43 |
+
)
|
44 |
+
|
45 |
+
# Create tasks
|
46 |
+
def create_tasks(topic):
|
47 |
+
task1 = Task(
|
48 |
+
description=f"""Conduct a brief analysis of the latest advancements in {topic}.
|
49 |
+
Identify key trends and potential impacts.""",
|
50 |
+
expected_output="Concise analysis in 2-3 sentences",
|
51 |
+
agent=researcher
|
52 |
+
)
|
53 |
+
|
54 |
+
task2 = Task(
|
55 |
+
description=f"""Using the insights about {topic}, create a short, engaging response
|
56 |
+
that highlights the most significant points. Keep it brief and conversational.""",
|
57 |
+
expected_output="Conversational response of 2-3 sentences",
|
58 |
+
agent=writer
|
59 |
+
)
|
60 |
+
|
61 |
+
return [task1, task2]
|
62 |
+
|
63 |
+
# Function to run the crew
|
64 |
+
def run_crew(topic):
|
65 |
+
try:
|
66 |
+
tasks = create_tasks(topic)
|
67 |
+
crew = Crew(
|
68 |
+
agents=[researcher, writer],
|
69 |
+
tasks=tasks,
|
70 |
+
verbose=2,
|
71 |
+
process=Process.sequential
|
72 |
+
)
|
73 |
+
result = crew.kickoff()
|
74 |
+
return result
|
75 |
+
except Exception as e:
|
76 |
+
return f"I apologize, but I encountered an issue while processing your request. Please try again or rephrase your question. Error details: {str(e)}"
|
77 |
+
|
78 |
+
# Chatbot function
|
79 |
+
def chatbot(message):
|
80 |
+
if not message.strip():
|
81 |
+
return "Please enter a valid question or topic."
|
82 |
+
response = run_crew(message)
|
83 |
+
return response
|
84 |
+
|
85 |
+
# Create Gradio interface
|
86 |
+
iface = gr.Interface(
|
87 |
+
fn=chatbot,
|
88 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask about any AI or technology topic..."),
|
89 |
+
outputs=gr.Textbox(),
|
90 |
+
title="AI Research Assistant Chatbot",
|
91 |
+
description="Ask about any AI or technology topic, and I'll provide a brief, informative response.",
|
92 |
+
examples=[
|
93 |
+
["What are the latest advancements in natural language processing?"],
|
94 |
+
["Tell me about recent breakthroughs in quantum computing."],
|
95 |
+
["What are the current trends in computer vision?"]
|
96 |
+
],
|
97 |
+
allow_flagging="never"
|
98 |
+
)
|
99 |
+
|
100 |
+
# Launch the interface
|
101 |
+
iface.launch(share=True)
|