chaaim123 commited on
Commit
8cd81a5
·
verified ·
1 Parent(s): 6ab7368

create app.py

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