Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,215 +1,216 @@
|
|
1 |
-
import os
|
2 |
-
import pandas as pd
|
3 |
-
import gradio as gr
|
4 |
-
from crewai import Agent, Task, Crew
|
5 |
-
from langchain_openai import ChatOpenAI
|
6 |
-
from crewai_tools import PDFSearchTool, FileReadTool, DOCXSearchTool, CSVSearchTool
|
7 |
-
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
-
from langchain.agents.agent_types import AgentType
|
9 |
-
from langchain_experimental.agents.agent_toolkits import create_csv_agent
|
10 |
-
from langchain_groq import ChatGroq
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
os.environ["
|
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 |
-
You are
|
107 |
-
You
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
You are
|
121 |
-
You
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
You are
|
135 |
-
You
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
You are
|
149 |
-
You are
|
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 |
-
gr.
|
207 |
-
gr.Textbox(label="
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
)
|
213 |
-
|
214 |
-
|
215 |
-
interface
|
|
|
|
1 |
+
import os
|
2 |
+
import pandas as pd
|
3 |
+
import gradio as gr
|
4 |
+
from crewai import Agent, Task, Crew
|
5 |
+
from langchain_openai import ChatOpenAI
|
6 |
+
from crewai_tools import PDFSearchTool, FileReadTool, DOCXSearchTool, CSVSearchTool
|
7 |
+
from langchain_google_genai import ChatGoogleGenerativeAI
|
8 |
+
from langchain.agents.agent_types import AgentType
|
9 |
+
from langchain_experimental.agents.agent_toolkits import create_csv_agent
|
10 |
+
from langchain_groq import ChatGroq
|
11 |
+
import asyncio
|
12 |
+
|
13 |
+
# API keys-----------------move them to ENV
|
14 |
+
os.environ["OPENAI_API_KEY"] = "NA"
|
15 |
+
os.environ["GOOGLE_API_KEY"] = "AIzaSyD7jKc5MdkRLakxcyhvrpie8XgbwY98NMo"
|
16 |
+
|
17 |
+
# Load The Groq model for LLM
|
18 |
+
llm = ChatGroq(
|
19 |
+
api_key="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",
|
20 |
+
model="llama3-70b-8192"
|
21 |
+
)
|
22 |
+
|
23 |
+
#<-----------------------------Tools----------------------------------->
|
24 |
+
class tools:
|
25 |
+
def pdfRead(path):
|
26 |
+
PDFtool = PDFSearchTool(
|
27 |
+
config=dict(
|
28 |
+
llm=dict(
|
29 |
+
provider="google",
|
30 |
+
config=dict(
|
31 |
+
model="gemini-1.5-flash-latest",
|
32 |
+
),
|
33 |
+
),
|
34 |
+
embedder=dict(
|
35 |
+
provider="huggingface",
|
36 |
+
config=dict(
|
37 |
+
model="sentence-transformers/msmarco-distilbert-base-v4"
|
38 |
+
|
39 |
+
),
|
40 |
+
),
|
41 |
+
),
|
42 |
+
pdf=path
|
43 |
+
)
|
44 |
+
return PDFtool
|
45 |
+
|
46 |
+
def fileRead(path):
|
47 |
+
Filetool = FileReadTool(
|
48 |
+
config=dict(
|
49 |
+
llm=dict(
|
50 |
+
provider="google",
|
51 |
+
config=dict(
|
52 |
+
model="gemini-1.5-flash-latest",
|
53 |
+
),
|
54 |
+
),
|
55 |
+
embedder=dict(
|
56 |
+
provider="huggingface",
|
57 |
+
config=dict(
|
58 |
+
model="sentence-transformers/msmarco-distilbert-base-v4"
|
59 |
+
|
60 |
+
),
|
61 |
+
),
|
62 |
+
),
|
63 |
+
file_path=path
|
64 |
+
)
|
65 |
+
return Filetool
|
66 |
+
|
67 |
+
def docsRead(path):
|
68 |
+
Docstool = DOCXSearchTool(
|
69 |
+
config=dict(
|
70 |
+
llm=dict(
|
71 |
+
provider="google",
|
72 |
+
config=dict(
|
73 |
+
model="gemini-1.5-flash-latest",
|
74 |
+
),
|
75 |
+
),
|
76 |
+
embedder=dict(
|
77 |
+
provider="huggingface",
|
78 |
+
config=dict(
|
79 |
+
model="sentence-transformers/msmarco-distilbert-base-v4"
|
80 |
+
|
81 |
+
),
|
82 |
+
),
|
83 |
+
),
|
84 |
+
docx=path
|
85 |
+
)
|
86 |
+
return Docstool
|
87 |
+
#<-----------------------------Tools----------------------------------->
|
88 |
+
|
89 |
+
#<------------------------------Agents START------------------------->
|
90 |
+
|
91 |
+
class AgentLoader:
|
92 |
+
|
93 |
+
def csvReaderAgent(path):
|
94 |
+
agent = create_csv_agent(
|
95 |
+
ChatGoogleGenerativeAI(temperature=0.6, model="gemini-1.5-flash-latest"),
|
96 |
+
path,
|
97 |
+
verbose=True,
|
98 |
+
agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
99 |
+
)
|
100 |
+
return agent
|
101 |
+
|
102 |
+
def fileReaderAgent(path):
|
103 |
+
FileReader = Agent(
|
104 |
+
role='File searcher',
|
105 |
+
goal='To analyse and generate optimal and reliable results',
|
106 |
+
backstory="""You are a File specialist and can handle multiple file formats like .txt, .csv, .json etc.
|
107 |
+
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.
|
108 |
+
You should also provide the results of your analysis and searching.""",
|
109 |
+
llm=llm,
|
110 |
+
verbose=True,
|
111 |
+
tools=[tools.fileRead(path)],
|
112 |
+
allow_delegation=False
|
113 |
+
)
|
114 |
+
return FileReader
|
115 |
+
|
116 |
+
def PdfReaderAgent(path):
|
117 |
+
PdfReader = Agent(
|
118 |
+
role='PDF searcher',
|
119 |
+
goal='To analyse and generate optimal and reliable results',
|
120 |
+
backstory="""You are a PDF specialist and content writer.
|
121 |
+
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.
|
122 |
+
You should also provide the results of your analysis and searching.""",
|
123 |
+
llm=llm,
|
124 |
+
verbose=True,
|
125 |
+
tools=[tools.pdfRead(path)],
|
126 |
+
allow_delegation=False
|
127 |
+
)
|
128 |
+
return PdfReader
|
129 |
+
|
130 |
+
def DocsReaderAgent(path):
|
131 |
+
DocsReader = Agent(
|
132 |
+
role='Docs searcher',
|
133 |
+
goal='To analyse and generate optimal and reliable results',
|
134 |
+
backstory="""You are a Docs specialist and content writer.
|
135 |
+
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.
|
136 |
+
You should also provide the results of your analysis and searching.""",
|
137 |
+
llm=llm,
|
138 |
+
verbose=True,
|
139 |
+
tools=[tools.docsRead(path)],
|
140 |
+
allow_delegation=False
|
141 |
+
)
|
142 |
+
return DocsReader
|
143 |
+
|
144 |
+
def writerAgent():
|
145 |
+
writer=Agent(
|
146 |
+
role='Content Writer',
|
147 |
+
goal='To provide QUICK and reliable output',
|
148 |
+
backstory="""You are content specialist.
|
149 |
+
You are responsible to generate high quality results in the required format very quickly as soon as data is available.
|
150 |
+
You are very accurate and fast at the same time.""",
|
151 |
+
verbose=True,
|
152 |
+
llm=llm,
|
153 |
+
max_iter=5
|
154 |
+
)
|
155 |
+
return writer
|
156 |
+
|
157 |
+
#<------------------------------Agents END------------------------->
|
158 |
+
|
159 |
+
#<-------------------------------Tasks---------------------------->
|
160 |
+
def getTasks(query, agent, exp):
|
161 |
+
task_read=Task(
|
162 |
+
description=f'{query}',
|
163 |
+
agent=agent,
|
164 |
+
expected_output=f'A detailed information on {query}'
|
165 |
+
)
|
166 |
+
|
167 |
+
task_write=Task(
|
168 |
+
description=f'{query}',
|
169 |
+
agent=AgentLoader.writerAgent(),
|
170 |
+
expected_output=exp
|
171 |
+
)
|
172 |
+
|
173 |
+
return [task_read, task_write]
|
174 |
+
|
175 |
+
# Gradio interface function
|
176 |
+
async def process_file(file, query, expected_output):
|
177 |
+
path = file.name
|
178 |
+
|
179 |
+
if path.endswith(".pdf"):
|
180 |
+
agent = AgentLoader.PdfReaderAgent(path)
|
181 |
+
elif path.endswith(".docx"):
|
182 |
+
agent = AgentLoader.DocsReaderAgent(path)
|
183 |
+
elif path.endswith(".json") or path.endswith(".txt"):
|
184 |
+
agent = AgentLoader.fileReaderAgent(path)
|
185 |
+
elif path.endswith(".csv"):
|
186 |
+
agent = AgentLoader.csvReaderAgent(path)
|
187 |
+
results = await anyio.to_thread.run_sync(agent.run, query)
|
188 |
+
else:
|
189 |
+
return 'File NOT supported'
|
190 |
+
|
191 |
+
if not path.endswith(".csv"):
|
192 |
+
task1 = getTasks(query, agent, expected_output)
|
193 |
+
mycrew = Crew(
|
194 |
+
agents=[agent, AgentLoader.writerAgent()],
|
195 |
+
tasks=task1,
|
196 |
+
verbose=True
|
197 |
+
)
|
198 |
+
results = await anyio.to_thread.run_sync(mycrew.kickoff)
|
199 |
+
|
200 |
+
return results
|
201 |
+
|
202 |
+
# Create the Gradio interface
|
203 |
+
interface = gr.Interface(
|
204 |
+
fn=process_file,
|
205 |
+
inputs=[
|
206 |
+
gr.File(label="Upload File"),
|
207 |
+
gr.Textbox(label="Query"),
|
208 |
+
gr.Textbox(label="Expected Output")
|
209 |
+
],
|
210 |
+
outputs="text",
|
211 |
+
title="File Analyzer",
|
212 |
+
description="Upload a file (CSV, PDF, DOCX, TXT, JSON) and enter your query to get detailed information."
|
213 |
+
)
|
214 |
+
|
215 |
+
# Launch the Gradio interface
|
216 |
+
interface.launch()
|