File size: 3,229 Bytes
014adc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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)