Ishanpardeshi commited on
Commit
10a04b6
1 Parent(s): 2960904

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +215 -0
app.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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()