Ishanpardeshi commited on
Commit
8bd4068
1 Parent(s): 2ecc108

Update app.py

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