SurabhiT commited on
Commit
e2f3005
1 Parent(s): 0cfa293

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +292 -0
app.py ADDED
@@ -0,0 +1,292 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # from groq import Groq
2
+
3
+ # client = Groq()
4
+ # completion = client.chat.completions.create(
5
+ # model="llama3-70b-8192",
6
+ # messages=[
7
+ # {
8
+ # "role": "user",
9
+ # "content": "Write a fairy tale"
10
+ # }
11
+ # ],
12
+ # temperature=1,
13
+ # max_tokens=1024,
14
+ # top_p=1,
15
+ # stream=True,
16
+ # stop=None,
17
+ # )
18
+
19
+ # for chunk in completion:
20
+ # print(chunk.choices[0].delta.content or "", end="")
21
+
22
+
23
+ import os
24
+ import tkinter as tk
25
+ from tkinter import filedialog
26
+ from crewai import Agent, Task, Crew
27
+ from langchain_openai import ChatOpenAI
28
+ from langchain_community.llms import Ollama
29
+ from langchain.agents.agent_types import AgentType
30
+ from langchain_experimental.agents.agent_toolkits import create_csv_agent
31
+ from langchain_openai import ChatOpenAI, OpenAI
32
+ from langchain_google_genai import ChatGoogleGenerativeAI
33
+ from langchain_groq import ChatGroq
34
+ from crewai.process import Process
35
+ import gradio as gr
36
+ import numpy as np
37
+
38
+ os.environ["OPENAI_API_KEY"] = "NA"
39
+ os.environ["GOOGLE_API_KEY"] = "AIzaSyD1Gxk5OZMnqAlzWoYjAoDBeu3Z5l2TS7U"
40
+
41
+ os.environ["GROQ_API_KEY"] = "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"
42
+
43
+ from crewai_tools import PDFSearchTool
44
+ from crewai_tools import FileReadTool
45
+ from crewai_tools import DOCXSearchTool
46
+ from crewai_tools import TXTSearchTool
47
+ from crewai_tools import CSVSearchTool
48
+
49
+ llm = ChatGroq(
50
+ model="llama3-70b-8192",
51
+ verbose=True,
52
+ temperature=0.8,
53
+ api_key=os.environ["GROQ_API_KEY"]
54
+ )
55
+
56
+ #--------------------------------------------Class for choosing agent---------------------------------------#
57
+ class agentCollection:
58
+
59
+ def agentPDF(filepath):
60
+ agentpdf = Agent(
61
+ role="PDF Content Searcher and Writer",
62
+ goal="Retrieve and summarize relevant content from a PDF provided by the user",
63
+ backstory="You are an expert in navigating and extracting information from PDF documents. Your task is to find the most relevant and accurate content within the PDF and provide a detailed and concise summary that addresses the user's query.",
64
+ verbose=True,
65
+ tools=[toolsCollection.toolPDF(filepath)],
66
+ llm=llm,
67
+ max_iter=10
68
+ )
69
+ return agentpdf
70
+
71
+ def agentFile(filepath):
72
+ agentfile = Agent(
73
+ role="General File Content Searcher and Writer",
74
+ goal="Retrieve and summarize relevant content from various file formats provided by the user",
75
+ backstory="You have extensive experience in handling different types of files, including PDFs, DOCX, TXT, and CSV. Your role is to expertly extract and summarize the most pertinent information from any file format to meet the user's needs.",
76
+ verbose=True,
77
+ tools=[toolsCollection.toolFile(filepath)],
78
+ llm=llm,
79
+ max_iter=10
80
+ )
81
+ return agentfile
82
+
83
+ def agentTXT(filepath):
84
+ agenttxt = Agent(
85
+ role="Text File Content Searcher and Writer",
86
+ goal="Retrieve and summarize relevant content from text files provided by the user",
87
+ backstory="You specialize in working with plain text files. Your job is to sift through the text and identify the most relevant information, providing a clear and accurate summary that fulfills the user's query.",
88
+ verbose=True,
89
+ tools=[toolsCollection.toolTXT(filepath)],
90
+ llm=llm,
91
+ max_iter=10
92
+ )
93
+ return agenttxt
94
+
95
+ def agentDOCX(filepath):
96
+ agentdoc = Agent(
97
+ role="DOCX Content Searcher and Writer",
98
+ goal="Retrieve and summarize relevant content from DOCX files provided by the user",
99
+ backstory="You are proficient in reading and extracting information from DOCX documents. Your expertise allows you to locate and summarize the most relevant content within a DOCX file, ensuring the user's query is answered thoroughly and accurately.",
100
+ verbose=True,
101
+ tools=[toolsCollection.toolDOCX(filepath)],
102
+ llm=llm,
103
+ max_iter=10
104
+ )
105
+ return agentdoc
106
+
107
+ def agentCSV(filepath):
108
+ agentcsv = create_csv_agent(
109
+ ChatGroq(temperature=0, model="llama3-70b-8192"),
110
+ filepath,
111
+ verbose=True,
112
+ agent_type=AgentType.ZERO_SHOT_REACT_DESCRIPTION
113
+ )
114
+ return agentcsv
115
+
116
+ def agentContentWriter():
117
+ agentwriter = Agent(
118
+ role="Content Writer",
119
+ goal="Write a comprehensive report or blog based on the data received from other agents",
120
+ backstory="""You are a skilled content writer with expertise in synthesizing information from various sources. Your task is to use the summaries and insights provided by other agents to create a well-structured and coherent report or blog that addresses the user's query in detail.""",
121
+ verbose=True,
122
+ llm=llm,
123
+ max_iter=7
124
+ )
125
+ return agentwriter
126
+
127
+ #--------------------------------------------Class for choosing tool---------------------------------------#
128
+
129
+ class toolsCollection:
130
+
131
+ def toolPDF(filepath):
132
+ if filepath == "":
133
+ print("FILE NOT FOUND")
134
+ return
135
+ pdftool = PDFSearchTool(
136
+ config=dict(
137
+ llm=dict(
138
+ provider="ollama",
139
+ config=dict(
140
+ model="llama3-70b-8192",
141
+ ),
142
+ ),
143
+ embedder=dict(
144
+ provider="huggingface",
145
+ config=dict(
146
+ model="sentence-transformers/msmarco-distilbert-base-v4"
147
+ ),
148
+ ),
149
+ ),
150
+ pdf=filepath
151
+ )
152
+ return pdftool
153
+
154
+ def toolFile(filepath):
155
+ filetool = FileReadTool(
156
+ config=dict(
157
+ llm=dict(
158
+ provider="ollama",
159
+ config=dict(
160
+ model="llama3-70b-8192",
161
+ ),
162
+ ),
163
+ embedder=dict(
164
+ provider="huggingface",
165
+ config=dict(
166
+ model="sentence-transformers/msmarco-distilbert-base-v4"
167
+ ),
168
+ ),
169
+ ),
170
+ file_path=filepath
171
+ )
172
+ return filetool
173
+
174
+ def toolTXT(filepath):
175
+ txttool = TXTSearchTool(
176
+ config=dict(
177
+ llm=dict(
178
+ provider="ollama",
179
+ config=dict(
180
+ model="llama3-70b-8192",
181
+ ),
182
+ ),
183
+ embedder=dict(
184
+ provider="huggingface",
185
+ config=dict(
186
+ model="sentence-transformers/msmarco-distilbert-base-v4"
187
+ ),
188
+ ),
189
+ ),
190
+ txt=filepath
191
+ )
192
+ return txttool
193
+
194
+ def toolDOCX(filepath):
195
+ if filepath == "":
196
+ print("FILE NOT FOUND")
197
+ return
198
+ docxtool = DOCXSearchTool(
199
+ config=dict(
200
+ llm=dict(
201
+ provider="ollama",
202
+ config=dict(
203
+ model="llama3-70b-8192",
204
+ ),
205
+ ),
206
+ embedder=dict(
207
+ provider="huggingface",
208
+ config=dict(
209
+ model="sentence-transformers/msmarco-distilbert-base-v4"
210
+ ),
211
+ ),
212
+ ),
213
+ docx=filepath
214
+ )
215
+ return docxtool
216
+
217
+ def toolCSV(filepath):
218
+ csvtool = CSVSearchTool(
219
+ config=dict(
220
+ llm=dict(
221
+ provider="ollama",
222
+ config=dict(
223
+ model="llama3-70b-8192",
224
+ ),
225
+ ),
226
+ embedder=dict(
227
+ provider="huggingface",
228
+ config=dict(
229
+ model="sentence-transformers/msmarco-distilbert-base-v4"
230
+ ),
231
+ ),
232
+ ),
233
+ csv=filepath
234
+ )
235
+ return csvtool
236
+
237
+ def run_ai(file, query, required_ans_format):
238
+ filepath = file.name
239
+
240
+ if filepath.endswith(".pdf"):
241
+ myagent = agentCollection.agentPDF(filepath)
242
+ elif filepath.endswith(".json"):
243
+ myagent = agentCollection.agentFile(filepath)
244
+ elif filepath.endswith(".docx"):
245
+ myagent = agentCollection.agentDOCX(filepath)
246
+ elif filepath.endswith(".txt"):
247
+ myagent = agentCollection.agentTXT(filepath)
248
+ elif filepath.endswith(".csv"):
249
+ myagent = agentCollection.agentCSV(filepath)
250
+ return myagent.run(query)
251
+
252
+
253
+ task = Task(
254
+ description=f"Summarize the detailed description of the {query}",
255
+ expected_output=f'{required_ans_format} : {query}',
256
+ agent=myagent,
257
+ )
258
+
259
+ content_writer_agent = agentCollection.agentContentWriter()
260
+ content_writer_task = Task(
261
+ description=f"Write a comprehensive report on the topic '{query}' using the summaries and insights from the other agents",
262
+ expected_output=f'{required_ans_format} : {query}',
263
+ agent=content_writer_agent,
264
+
265
+ )
266
+
267
+ crew = Crew(
268
+ agents=[myagent, content_writer_agent],
269
+ tasks=[task, content_writer_task],
270
+ process=Process.sequential,
271
+ verbose=2
272
+ )
273
+
274
+ # crew = Crew(
275
+ # agents=[myagent],
276
+ # tasks=[task],
277
+ # process=Process.sequential,
278
+ # verbose=2
279
+ # )
280
+
281
+ result = crew.kickoff()
282
+ return result
283
+
284
+ iface = gr.Interface(
285
+ fn=run_ai,
286
+ inputs=[gr.File(label="Upload File"), "text", "text"],
287
+ outputs="text",
288
+ title="Document and Data Analyzer",
289
+ description="Upload a file, enter your query, and specify the format of the expected answer"
290
+ )
291
+
292
+ iface.launch()