Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,63 @@
|
|
| 1 |
-
import os
|
| 2 |
import gradio as gr
|
|
|
|
|
|
|
| 3 |
from crewai import Agent, Task, Crew, Process
|
| 4 |
-
from
|
| 5 |
-
from langchain.tools import DuckDuckGoSearchRun
|
| 6 |
|
| 7 |
-
# Initialize the GROQ
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
model_name="mixtral-8x7b-32768"
|
| 11 |
)
|
| 12 |
|
| 13 |
# Initialize the search tool
|
| 14 |
search_tool = DuckDuckGoSearchRun()
|
| 15 |
|
| 16 |
-
# Create
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
|
| 27 |
-
writer =
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
verbose=True,
|
| 32 |
-
allow_delegation=False,
|
| 33 |
-
llm=groq_llm
|
| 34 |
)
|
| 35 |
|
| 36 |
-
editor =
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
verbose=True,
|
| 41 |
-
allow_delegation=False,
|
| 42 |
-
llm=groq_llm
|
| 43 |
)
|
| 44 |
|
| 45 |
def create_crew(query):
|
|
@@ -69,18 +87,21 @@ def create_crew(query):
|
|
| 69 |
|
| 70 |
return crew
|
| 71 |
|
| 72 |
-
def process_query(query):
|
| 73 |
crew = create_crew(query)
|
| 74 |
result = crew.kickoff()
|
| 75 |
return result
|
| 76 |
|
| 77 |
-
# Create Gradio interface
|
| 78 |
iface = gr.Interface(
|
| 79 |
fn=process_query,
|
| 80 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 81 |
outputs=gr.Textbox(lines=10, label="AI Agent Response"),
|
| 82 |
-
title="
|
| 83 |
-
description="
|
| 84 |
)
|
| 85 |
|
| 86 |
iface.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
from crewai import Agent, Task, Crew, Process
|
| 5 |
+
from langchain_community.tools import DuckDuckGoSearchRun
|
|
|
|
| 6 |
|
| 7 |
+
# Initialize the GROQ client
|
| 8 |
+
client = Groq(
|
| 9 |
+
api_key=os.environ["GROQ_API_KEY"],
|
|
|
|
| 10 |
)
|
| 11 |
|
| 12 |
# Initialize the search tool
|
| 13 |
search_tool = DuckDuckGoSearchRun()
|
| 14 |
|
| 15 |
+
# Create a function to generate text using GROQ
|
| 16 |
+
def generate_text(prompt, max_tokens=500):
|
| 17 |
+
chat_completion = client.chat.completions.create(
|
| 18 |
+
messages=[
|
| 19 |
+
{
|
| 20 |
+
"role": "user",
|
| 21 |
+
"content": prompt,
|
| 22 |
+
}
|
| 23 |
+
],
|
| 24 |
+
model="mixtral-8x7b-32768",
|
| 25 |
+
max_tokens=max_tokens,
|
| 26 |
+
)
|
| 27 |
+
return chat_completion.choices[0].message.content
|
| 28 |
+
|
| 29 |
+
# Create agents using the GROQ text generation function
|
| 30 |
+
def create_agent(role, goal, backstory):
|
| 31 |
+
return Agent(
|
| 32 |
+
role=role,
|
| 33 |
+
goal=goal,
|
| 34 |
+
backstory=backstory,
|
| 35 |
+
verbose=True,
|
| 36 |
+
allow_delegation=False,
|
| 37 |
+
llm_config={
|
| 38 |
+
"function": generate_text,
|
| 39 |
+
"config": {
|
| 40 |
+
"max_tokens": 500
|
| 41 |
+
}
|
| 42 |
+
}
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
researcher = create_agent(
|
| 46 |
+
'Senior Researcher',
|
| 47 |
+
'Conduct thorough research on given topics',
|
| 48 |
+
'You are an experienced researcher with a keen eye for detail and the ability to find relevant information quickly.'
|
| 49 |
)
|
| 50 |
|
| 51 |
+
writer = create_agent(
|
| 52 |
+
'Content Writer',
|
| 53 |
+
'Create engaging and informative content based on research',
|
| 54 |
+
'You are a skilled writer capable of turning complex information into easily understandable and engaging content.'
|
|
|
|
|
|
|
|
|
|
| 55 |
)
|
| 56 |
|
| 57 |
+
editor = create_agent(
|
| 58 |
+
'Editor',
|
| 59 |
+
'Refine and improve the written content',
|
| 60 |
+
'You are a meticulous editor with a strong command of language and an eye for clarity and coherence.'
|
|
|
|
|
|
|
|
|
|
| 61 |
)
|
| 62 |
|
| 63 |
def create_crew(query):
|
|
|
|
| 87 |
|
| 88 |
return crew
|
| 89 |
|
| 90 |
+
def process_query(query, max_tokens=500):
|
| 91 |
crew = create_crew(query)
|
| 92 |
result = crew.kickoff()
|
| 93 |
return result
|
| 94 |
|
| 95 |
+
# Create the Gradio interface
|
| 96 |
iface = gr.Interface(
|
| 97 |
fn=process_query,
|
| 98 |
+
inputs=[
|
| 99 |
+
gr.Textbox(lines=5, label="Enter your query"),
|
| 100 |
+
gr.Slider(minimum=100, maximum=1000, value=500, step=50, label="Max Tokens")
|
| 101 |
+
],
|
| 102 |
outputs=gr.Textbox(lines=10, label="AI Agent Response"),
|
| 103 |
+
title="CrewAI-powered Chatbot",
|
| 104 |
+
description="Enter a query and get a researched, written, and edited response using CrewAI and GROQ API."
|
| 105 |
)
|
| 106 |
|
| 107 |
iface.launch()
|