typesdigital's picture
Create app.py
014adc1 verified
import os
import gradio as gr
from crewai import Agent, Task, Crew, Process
from crewai_tools import SerperDevTool
from langchain_groq import ChatGroq
# Set up environment variables
os.environ["GROQ_API_KEY"] = "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"
os.environ["SERPER_API_KEY"] = "206256c6acfbcd5a46195f3312aaa7e8ed38ae5f"
# Initialize Groq LLM
groq_llm = ChatGroq(
model_name="mixtral-8x7b-32768",
temperature=0.7,
max_tokens=32768
)
# Initialize search tool
search_tool = SerperDevTool()
# Define agents
researcher = Agent(
role='Senior Research Analyst',
goal='Uncover cutting-edge developments in AI and data science',
backstory="""You work at a leading tech think tank.
Your expertise lies in identifying emerging trends.
You have a knack for dissecting complex data and presenting actionable insights.""",
verbose=True,
allow_delegation=False,
llm=groq_llm,
tools=[search_tool]
)
writer = Agent(
role='Tech Content Strategist',
goal='Craft compelling content on tech advancements',
backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
You transform complex concepts into compelling narratives.""",
verbose=True,
allow_delegation=True,
llm=groq_llm
)
# Create tasks
def create_tasks(topic):
task1 = Task(
description=f"""Conduct a brief analysis of the latest advancements in {topic}.
Identify key trends and potential impacts.""",
expected_output="Concise analysis in 2-3 sentences",
agent=researcher
)
task2 = Task(
description=f"""Using the insights about {topic}, create a short, engaging response
that highlights the most significant points. Keep it brief and conversational.""",
expected_output="Conversational response of 2-3 sentences",
agent=writer
)
return [task1, task2]
# Function to run the crew
def run_crew(topic):
try:
tasks = create_tasks(topic)
crew = Crew(
agents=[researcher, writer],
tasks=tasks,
verbose=2,
process=Process.sequential
)
result = crew.kickoff()
return result
except Exception as e:
return f"I apologize, but I encountered an issue while processing your request. Please try again or rephrase your question. Error details: {str(e)}"
# Chatbot function
def chatbot(message):
if not message.strip():
return "Please enter a valid question or topic."
response = run_crew(message)
return response
# Create Gradio interface
iface = gr.Interface(
fn=chatbot,
inputs=gr.Textbox(lines=2, placeholder="Ask about any AI or technology topic..."),
outputs=gr.Textbox(),
title="AI Research Assistant Chatbot",
description="Ask about any AI or technology topic, and I'll provide a brief, informative response.",
examples=[
["What are the latest advancements in natural language processing?"],
["Tell me about recent breakthroughs in quantum computing."],
["What are the current trends in computer vision?"]
],
allow_flagging="never"
)
# Launch the interface
iface.launch(share=True)