Spaces:
Runtime error
Runtime error
File size: 7,525 Bytes
10a04b6 |
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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
import os
import pandas as pd
import gradio as gr
from crewai import Agent, Task, Crew
from langchain_openai import ChatOpenAI
from crewai_tools import PDFSearchTool, FileReadTool, DOCXSearchTool, CSVSearchTool
from langchain_google_genai import ChatGoogleGenerativeAI
from langchain.agents.agent_types import AgentType
from langchain_experimental.agents.agent_toolkits import create_csv_agent
from langchain_groq import ChatGroq
# API keys-----------------move them to ENV
os.environ["OPENAI_API_KEY"] = "NA"
os.environ["GOOGLE_API_KEY"] = "AIzaSyD7jKc5MdkRLakxcyhvrpie8XgbwY98NMo"
# Load The Groq model for LLM
llm = ChatGroq(
api_key="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",
model="llama3-70b-8192"
)
#<-----------------------------Tools----------------------------------->
class tools:
def pdfRead(path):
PDFtool = PDFSearchTool(
config=dict(
llm=dict(
provider="google",
config=dict(
model="gemini-1.5-flash-latest",
),
),
embedder=dict(
provider="huggingface",
config=dict(
model="sentence-transformers/msmarco-distilbert-base-v4"
),
),
),
pdf=path
)
return PDFtool
def fileRead(path):
Filetool = FileReadTool(
config=dict(
llm=dict(
provider="google",
config=dict(
model="gemini-1.5-flash-latest",
),
),
embedder=dict(
provider="huggingface",
config=dict(
model="sentence-transformers/msmarco-distilbert-base-v4"
),
),
),
file_path=path
)
return Filetool
def docsRead(path):
Docstool = DOCXSearchTool(
config=dict(
llm=dict(
provider="google",
config=dict(
model="gemini-1.5-flash-latest",
),
),
embedder=dict(
provider="huggingface",
config=dict(
model="sentence-transformers/msmarco-distilbert-base-v4"
),
),
),
docx=path
)
return Docstool
#<-----------------------------Tools----------------------------------->
#<------------------------------Agents START------------------------->
class AgentLoader:
def csvReaderAgent(path):
agent = create_csv_agent(
ChatGoogleGenerativeAI(temperature=0.6, model="gemini-1.5-flash-latest"),
path,
verbose=True,
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION
)
return agent
def fileReaderAgent(path):
FileReader = Agent(
role='File searcher',
goal='To analyse and generate optimal and reliable results',
backstory="""You are a File specialist and can handle multiple file formats like .txt, .csv, .json etc.
You are responsible to analyse the file to find the relevant content that solves the problem of the user and generate high quality and reliable results.
You should also provide the results of your analysis and searching.""",
llm=llm,
verbose=True,
tools=[tools.fileRead(path)],
allow_delegation=False
)
return FileReader
def PdfReaderAgent(path):
PdfReader = Agent(
role='PDF searcher',
goal='To analyse and generate optimal and reliable results',
backstory="""You are a PDF specialist and content writer.
You are responsible to analyse the pdf to find the relevant content that solves the problem of the user and generate high quality and reliable results.
You should also provide the results of your analysis and searching.""",
llm=llm,
verbose=True,
tools=[tools.pdfRead(path)],
allow_delegation=False
)
return PdfReader
def DocsReaderAgent(path):
DocsReader = Agent(
role='Docs searcher',
goal='To analyse and generate optimal and reliable results',
backstory="""You are a Docs specialist and content writer.
You are responsible to analyse the pdf to find the relevant content that solves the problem of the user and generate high quality and reliable results.
You should also provide the results of your analysis and searching.""",
llm=llm,
verbose=True,
tools=[tools.docsRead(path)],
allow_delegation=False
)
return DocsReader
def writerAgent():
writer=Agent(
role='Content Writer',
goal='To provide QUICK and reliable output',
backstory="""You are content specialist.
You are responsible to generate high quality results in the required format very quickly as soon as data is available.
You are very accurate and fast at the same time.""",
verbose=True,
llm=llm,
max_iter=5
)
return writer
#<------------------------------Agents END------------------------->
#<-------------------------------Tasks---------------------------->
def getTasks(query, agent, exp):
task_read=Task(
description=f'{query}',
agent=agent,
expected_output=f'A detailed information on {query}'
)
task_write=Task(
description=f'{query}',
agent=AgentLoader.writerAgent(),
expected_output=exp
)
return [task_read, task_write]
# Gradio interface function
def process_file(file, query, expected_output):
path = file.name
if path.endswith(".pdf"):
agent = AgentLoader.PdfReaderAgent(path)
elif path.endswith(".docx"):
agent = AgentLoader.DocsReaderAgent(path)
elif path.endswith(".json") or path.endswith(".txt"):
agent = AgentLoader.fileReaderAgent(path)
elif path.endswith(".csv"):
agent = AgentLoader.csvReaderAgent(path)
results = agent.run(query)
else:
return 'File NOT supported'
if not path.endswith(".csv"):
task1 = getTasks(query, agent, expected_output)
mycrew = Crew(
agents=[agent, AgentLoader.writerAgent()],
tasks=task1,
verbose=True
)
results = mycrew.kickoff()
return results
# Create the Gradio interface
interface = gr.Interface(
fn=process_file,
inputs=[
gr.File(label="Upload File"),
gr.Textbox(label="Query"),
gr.Textbox(label="Expected Output")
],
outputs="text",
title="File Analyzer",
description="Upload a file (CSV, PDF, DOCX, TXT, JSON) and enter your query to get detailed information."
)
# Launch the Gradio interface
interface.launch() |