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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import pipeline
|
3 |
+
from PIL import Image
|
4 |
+
import pytesseract
|
5 |
+
#import fitz # PyMuPDF
|
6 |
+
import os
|
7 |
+
from huggingface_hub import InferenceClient
|
8 |
+
from phi.agent import Agent, RunResponse
|
9 |
+
from phi.model.groq import Groq
|
10 |
+
from pydantic import BaseModel, Field
|
11 |
+
from typing import Optional
|
12 |
+
document_types = {
|
13 |
+
# Wildlife Crime Control Bureau (WCCB)
|
14 |
+
"CITES Certificate": {
|
15 |
+
"code": "6261WC",
|
16 |
+
"description": "A certificate used in the trade of endangered species in accordance with the CITES convention."
|
17 |
+
},
|
18 |
+
"Captive Bred/Artificially Propagated Certificate": {
|
19 |
+
"code": "626WC3",
|
20 |
+
"description": "A certificate issued by WCCB for species that fall under Appendix 1 of CITES, certifying that the species were bred or propagated artificially."
|
21 |
+
},
|
22 |
+
|
23 |
+
# Central Pollution Control Board (CPCB)
|
24 |
+
"Extended Producers Responsibility Authorization (EPRA)": {
|
25 |
+
"code": "651001",
|
26 |
+
"description": "Authorization issued by CPCB for producers to manage the end-of-life disposal of their products responsibly."
|
27 |
+
},
|
28 |
+
|
29 |
+
# Central Drug Standard Control Organization (CDSCO)
|
30 |
+
"Invoice": {
|
31 |
+
"code": "380000",
|
32 |
+
"description": "A commercial document issued by a seller to a buyer, indicating the products, quantities, and agreed prices for products or services."
|
33 |
+
},
|
34 |
+
"Invoice cum Packing List": {
|
35 |
+
"code": "331000",
|
36 |
+
"description": "A combined document serving as both an invoice and a packing list, detailing the contents of the shipment."
|
37 |
+
},
|
38 |
+
"Master Bill of Lading": {
|
39 |
+
"code": "704000",
|
40 |
+
"description": "A bill of lading issued by the carrier or its agent, covering multiple house bills of lading."
|
41 |
+
},
|
42 |
+
"House Bill of Lading": {
|
43 |
+
"code": "714000",
|
44 |
+
"description": "A bill of lading issued by a freight forwarder or consolidator to individual shippers, covering part of a larger shipment."
|
45 |
+
},
|
46 |
+
"Bill of Lading": {
|
47 |
+
"code": "705000",
|
48 |
+
"description": "A legal document issued by a carrier to a shipper, detailing the type, quantity, and destination of the goods being carried."
|
49 |
+
},
|
50 |
+
"House Waybill": {
|
51 |
+
"code": "703000",
|
52 |
+
"description": "A transport document issued by a freight forwarder or consolidator, serving as a receipt for goods shipped."
|
53 |
+
},
|
54 |
+
"Tanker Bill of Lading": {
|
55 |
+
"code": "709000",
|
56 |
+
"description": "A bill of lading specifically for liquid bulk cargo transported in tankers."
|
57 |
+
},
|
58 |
+
"Sea Waybill (Non Negotiable)": {
|
59 |
+
"code": "710000",
|
60 |
+
"description": "A non-negotiable transport document evidencing the contract for the carriage of goods by sea."
|
61 |
+
},
|
62 |
+
"Inland Waterway Bill of Lading": {
|
63 |
+
"code": "711000",
|
64 |
+
"description": "A negotiable transport document for goods transported via inland waterways."
|
65 |
+
},
|
66 |
+
"Air Waybill": {
|
67 |
+
"code": "740000",
|
68 |
+
"description": "A transport document issued by an airline for the carriage of goods by air."
|
69 |
+
},
|
70 |
+
"Master Air Waybill": {
|
71 |
+
"code": "741000",
|
72 |
+
"description": "A transport document issued by an airline or consolidator for consolidated shipments from multiple shippers."
|
73 |
+
},
|
74 |
+
"Waybill (Non Negotiable)": {
|
75 |
+
"code": "700000",
|
76 |
+
"description": "A non-negotiable document evidencing the contract for the transport of cargo."
|
77 |
+
},
|
78 |
+
|
79 |
+
# Other Documents
|
80 |
+
"CC for Tea Import": {
|
81 |
+
"code": "911TB1",
|
82 |
+
"description": "Certificate for tea import issued by the Tea Board."
|
83 |
+
},
|
84 |
+
"Kimberley Process Certificate": {
|
85 |
+
"code": "002KP0",
|
86 |
+
"description": "A certificate issued by GJEPC for the export and import of rough diamonds, ensuring they are conflict-free."
|
87 |
+
},
|
88 |
+
"Import Certificate for Narcotic Drugs": {
|
89 |
+
"code": "911BN2",
|
90 |
+
"description": "A certificate issued by CBN for the import of narcotic drugs, ensuring compliance with regulations."
|
91 |
+
},
|
92 |
+
"Registration Certificate (Drugs)": {
|
93 |
+
"code": "101DC1",
|
94 |
+
"description": "A certificate issued by CDSCO for the registration of drug manufacturers."
|
95 |
+
},
|
96 |
+
"Registration Certificate (Cosmetics)": {
|
97 |
+
"code": "101DC2",
|
98 |
+
"description": "A certificate issued by CDSCO for the registration of cosmetic products."
|
99 |
+
},
|
100 |
+
"Import Permit for Non-Insecticidal Use": {
|
101 |
+
"code": "911CI3",
|
102 |
+
"description": "A permit issued by CIB&RC for the import of products intended for non-insecticidal use."
|
103 |
+
},
|
104 |
+
"Permit for Import of Livestock Products": {
|
105 |
+
"code": "911DF1",
|
106 |
+
"description": "A permit issued by DADF for the import of livestock products, ensuring compliance with health and safety standards."
|
107 |
+
},
|
108 |
+
"NOC for Import/Procurement of Aircraft": {
|
109 |
+
"code": "911DA3",
|
110 |
+
"description": "A No Objection Certificate issued by DGCA for the import or procurement of aircraft for air transport services."
|
111 |
+
},
|
112 |
+
"Licence Form LE-8 (Licence to Import Explosives)": {
|
113 |
+
"code": "911PE1",
|
114 |
+
"description": "A license issued by PESO for the import of explosives, ensuring compliance with safety regulations."
|
115 |
+
},
|
116 |
+
"License to Import Ammonium Nitrate": {
|
117 |
+
"code": "911PE2",
|
118 |
+
"description": "A license issued by PESO for the import of ammonium nitrate, ensuring compliance with safety regulations."
|
119 |
+
},
|
120 |
+
"Import Permit": {
|
121 |
+
"code": "911PQ1",
|
122 |
+
"description": "A permit issued by PQIS for the import of specific goods, ensuring compliance with regulatory requirements."
|
123 |
+
},
|
124 |
+
"No Objection Certificate (NOC) for Natural Rubber": {
|
125 |
+
"code": "911RB1",
|
126 |
+
"description": "A No Objection Certificate issued by the Rubber Board for the import of natural rubber."
|
127 |
+
},
|
128 |
+
"Registration-Cum-Membership Certificate (RCMC)": {
|
129 |
+
"code": "101TO2",
|
130 |
+
"description": "A certificate issued by the Tobacco Board for the registration and membership of exporters."
|
131 |
+
},
|
132 |
+
"Certificate of Registration (Publisher/Owner)": {
|
133 |
+
"code": "101RN1",
|
134 |
+
"description": "A certificate issued by RNI for the registration of publishers or owners of publications."
|
135 |
+
},
|
136 |
+
"No Objection Certificate for Procurement of Radiation Source": {
|
137 |
+
"code": "911AB1",
|
138 |
+
"description": "A No Objection Certificate issued by AERB for the procurement of radiation sources."
|
139 |
+
},
|
140 |
+
"Import License for Drugs": {
|
141 |
+
"code": "9111DC",
|
142 |
+
"description": "A license issued by CDSCO for the import of drugs, ensuring compliance with regulatory requirements."
|
143 |
+
},
|
144 |
+
"Certificate of Analysis": {
|
145 |
+
"code": "001000",
|
146 |
+
"description": "A certificate providing the results of an analysis of a product, attesting to its quality or purity."
|
147 |
+
},
|
148 |
+
"Certificate of Conformity": {
|
149 |
+
"code": "002000",
|
150 |
+
"description": "A certificate certifying that a product conforms to predefined specifications or standards."
|
151 |
+
},
|
152 |
+
"Certificate of Quality": {
|
153 |
+
"code": "003000",
|
154 |
+
"description": "A certificate certifying the quality of goods, services, or processes."
|
155 |
+
},
|
156 |
+
"Test Report": {
|
157 |
+
"code": "004000",
|
158 |
+
"description": "A report providing the results of tests conducted on a product or material."
|
159 |
+
},
|
160 |
+
"Price/Sales Catalogue": {
|
161 |
+
"code": "005000",
|
162 |
+
"description": "A document providing pricing and catalogue details for goods and services offered by a seller."
|
163 |
+
},
|
164 |
+
"Product Specification Report": {
|
165 |
+
"code": "006000",
|
166 |
+
"description": "A report providing detailed specifications of a product."
|
167 |
+
},
|
168 |
+
"Federal Label Approval": {
|
169 |
+
"code": "011000",
|
170 |
+
"description": "A document relating to federal label approval requirements for packaged products."
|
171 |
+
},
|
172 |
+
"Vaccination Certificate": {
|
173 |
+
"code": "0380AQ",
|
174 |
+
"description": "An official document proving immunization against certain diseases."
|
175 |
+
},
|
176 |
+
"Safety and Hazard Data Sheet": {
|
177 |
+
"code": "0530HZ",
|
178 |
+
"description": "A document providing information about the hazards of a material and advice on its safe handling and storage."
|
179 |
+
},
|
180 |
+
"Registration Document": {
|
181 |
+
"code": "101000",
|
182 |
+
"description": "An official document providing registration details for a business, organization, or individual."
|
183 |
+
},
|
184 |
+
"Importer Exporter Certificate": {
|
185 |
+
"code": "101001",
|
186 |
+
"description": "A certificate issued by DGFT to authorize an entity to engage in import or export activities."
|
187 |
+
},
|
188 |
+
"Purchase Order": {
|
189 |
+
"code": "105000",
|
190 |
+
"description": "A document issued by a buyer to a seller, specifying the details of a purchase transaction."
|
191 |
+
},
|
192 |
+
"Bond": {
|
193 |
+
"code": "165000",
|
194 |
+
"description": "A document indicating a financial guarantee for the payment of duties or performance of obligations."
|
195 |
+
},
|
196 |
+
"Crew List Declaration": {
|
197 |
+
"code": "250000",
|
198 |
+
"description": "A declaration listing the crew members aboard a conveyance, such as a ship or aircraft."
|
199 |
+
},
|
200 |
+
"Fumigation Certificate": {
|
201 |
+
"code": "2670PQ",
|
202 |
+
"description": "A certificate attesting that fumigation has been performed on goods to eliminate pests."
|
203 |
+
},
|
204 |
+
"Packing List": {
|
205 |
+
"code": "271000",
|
206 |
+
"description": "A document specifying the distribution of goods in individual packages within a shipment."
|
207 |
+
},
|
208 |
+
"Inspection Report": {
|
209 |
+
"code": "293000",
|
210 |
+
"description": "A report informing a party of the results of an inspection conducted on goods or facilities."
|
211 |
+
},
|
212 |
+
"Dangerous Goods List": {
|
213 |
+
"code": "298000",
|
214 |
+
"description": "A listing of all details of dangerous goods carried, including their classification and handling requirements."
|
215 |
+
},
|
216 |
+
"Contract": {
|
217 |
+
"code": "315000",
|
218 |
+
"description": "A document evidencing an agreement between parties for the supply of goods or services."
|
219 |
+
},
|
220 |
+
"Proforma Invoice": {
|
221 |
+
"code": "325000",
|
222 |
+
"description": "A preliminary invoice containing similar information to the final invoice but not claiming immediate payment."
|
223 |
+
},
|
224 |
+
"Banker's Guarantee": {
|
225 |
+
"code": "430000",
|
226 |
+
"description": "A document in which a bank guarantees the payment of a specified amount to a designated party under certain conditions."
|
227 |
+
},
|
228 |
+
"Documentary Credit": {
|
229 |
+
"code": "465000",
|
230 |
+
"description": "A document issued by a bank stating that it will pay, accept, or negotiate drafts upon compliance with specified terms and presentation of required documents."
|
231 |
+
},
|
232 |
+
"Free Sale Certificate": {
|
233 |
+
"code": "627000",
|
234 |
+
"description": "A certificate confirming that a specified product is freely available for sale in the country of origin."
|
235 |
+
},
|
236 |
+
"Veterinary Quarantine Certificate": {
|
237 |
+
"code": "629000",
|
238 |
+
"description": "A certification that livestock or animal products are kept under health supervision for a specified period as per veterinary quarantine instructions."
|
239 |
+
},
|
240 |
+
"Health Certificate": {
|
241 |
+
"code": "636000",
|
242 |
+
"description": "A document legalized by a competent authority, declaring that a product is free from pathogens and fit for human consumption."
|
243 |
+
},
|
244 |
+
"Food Grade Certificate": {
|
245 |
+
"code": "637000",
|
246 |
+
"description": "A document certifying that a product is suitable for use in the food industry."
|
247 |
+
},
|
248 |
+
"Certificate of Compliance with OIE Standards": {
|
249 |
+
"code": "648000",
|
250 |
+
"description": "A certification that products have been treated in accordance with standards set by the World Organization for Animal Health (OIE)."
|
251 |
+
},
|
252 |
+
"Production Facility License": {
|
253 |
+
"code": "649000",
|
254 |
+
"description": "A license granted by a competent authority to a production facility for manufacturing specific products."
|
255 |
+
},
|
256 |
+
"Consent to Operate/Registration": {
|
257 |
+
"code": "6490HZ",
|
258 |
+
"description": "A certificate issued by a State Pollution Control Board to operate a facility, ensuring compliance with environmental regulations."
|
259 |
+
},
|
260 |
+
"Certified Copy of the Licensed Premises": {
|
261 |
+
"code": "6490DC",
|
262 |
+
"description": "A certified copy of the premises where drugs are stored, issued by a competent authority."
|
263 |
+
},
|
264 |
+
"Manufacturing License": {
|
265 |
+
"code": "651000",
|
266 |
+
"description": "A license granted by a competent authority to a manufacturer for the production of specific products."
|
267 |
+
},
|
268 |
+
"Industrial License": {
|
269 |
+
"code": "651001",
|
270 |
+
"description": "A license granted by a competent authority to establish and operate a specific industry."
|
271 |
+
},
|
272 |
+
"Phytosanitary Re-export Certificate": {
|
273 |
+
"code": "6570PQ",
|
274 |
+
"description": "A certificate attesting that a consignment meets phytosanitary import requirements for re-export."
|
275 |
+
},
|
276 |
+
"Rail Consignment Note": {
|
277 |
+
"code": "720000",
|
278 |
+
"description": "A transport document constituting a contract for the carriage of goods by rail."
|
279 |
+
},
|
280 |
+
"Road Consignment Note": {
|
281 |
+
"code": "730000",
|
282 |
+
"description": "A transport document evidencing a contract for the carriage of goods by road."
|
283 |
+
},
|
284 |
+
"Container Manifest": {
|
285 |
+
"code": "788000",
|
286 |
+
"description": "A document specifying the contents of particular freight containers or other transport units."
|
287 |
+
},
|
288 |
+
"Maritime Declaration of Health": {
|
289 |
+
"code": "797000",
|
290 |
+
"description": "A document certifying the health condition on board a vessel, valid until a specified date."
|
291 |
+
},
|
292 |
+
"Certificate of Registry": {
|
293 |
+
"code": "798000",
|
294 |
+
"description": "An official certificate stating the vessel's registry details."
|
295 |
+
},
|
296 |
+
"Ship's Stores Declaration": {
|
297 |
+
"code": "799000",
|
298 |
+
"description": "A declaration to Customs regarding the contents of the ship's stores, including goods for passengers and crew."
|
299 |
+
},
|
300 |
+
"Export Licence": {
|
301 |
+
"code": "811000",
|
302 |
+
"description": "A permit issued by a government authority authorizing the exportation of a specified commodity subject to certain conditions."
|
303 |
+
},
|
304 |
+
"Phytosanitary Certificate": {
|
305 |
+
"code": "8510FS",
|
306 |
+
"description": "A certificate attesting that a consignment meets phytosanitary import requirements."
|
307 |
+
},
|
308 |
+
"Sanitary Certificate": {
|
309 |
+
"code": "852000",
|
310 |
+
"description": "A document issued by the competent authority in the exporting country, certifying that alimentary and animal products are fit for human consumption."
|
311 |
+
},
|
312 |
+
"Veterinary Certificate": {
|
313 |
+
"code": "853000",
|
314 |
+
"description": "A document issued by the competent authority in the exporting country, certifying that live animals or birds are free from disease."
|
315 |
+
},
|
316 |
+
"Inspection Certificate": {
|
317 |
+
"code": "856000",
|
318 |
+
"description": "A document issued by a competent body, certifying that goods have been inspected in accordance with national or international standards."
|
319 |
+
},
|
320 |
+
"Pre-Shipment Inspection Certificate": {
|
321 |
+
"code": "856001",
|
322 |
+
"description": "A certificate issued prior to shipment, certifying that goods have been inspected and meet specified standards."
|
323 |
+
},
|
324 |
+
"Certificate of Origin": {
|
325 |
+
"code": "861000",
|
326 |
+
"description": "A document certifying that goods originate from a specific country."
|
327 |
+
},
|
328 |
+
"Preference Certificate of Origin": {
|
329 |
+
"code": "864000",
|
330 |
+
"description": "A certificate of origin meeting the requirements for preferential treatment under trade agreements."
|
331 |
+
},
|
332 |
+
"Certificate of Origin Form GSP": {
|
333 |
+
"code": "865000",
|
334 |
+
"description": "A specific form of certificate of origin for goods qualifying for preferential treatment under the Generalized System of Preferences (GSP)."
|
335 |
+
},
|
336 |
+
"Consular Invoice": {
|
337 |
+
"code": "870000",
|
338 |
+
"description": "A document prepared by an exporter and endorsed by the diplomatic representation of the importing country, used for customs purposes."
|
339 |
+
},
|
340 |
+
"Dangerous Goods Declaration": {
|
341 |
+
"code": "890000",
|
342 |
+
"description": "A document describing hazardous goods for transport purposes, certifying compliance with safety regulations."
|
343 |
+
},
|
344 |
+
"Import Licence": {
|
345 |
+
"code": "911000",
|
346 |
+
"description": "A document issued by the competent authority authorizing the import of specific goods under specified conditions."
|
347 |
+
},
|
348 |
+
"Value Declaration (GATT Valuation Declaration)": {
|
349 |
+
"code": "934000",
|
350 |
+
"description": "A document in which the importer declares the value of goods for customs purposes, including invoice price, freight, insurance, and other costs."
|
351 |
+
},
|
352 |
+
"Customs Invoice": {
|
353 |
+
"code": "935000",
|
354 |
+
"description": "A document required by customs in the importing country, detailing the invoice price, freight, insurance, and other costs for determining customs value."
|
355 |
+
},
|
356 |
+
"ATA Carnet": {
|
357 |
+
"code": "955000",
|
358 |
+
"description": "An international customs document used for the temporary admission of goods, serving as a passport for goods."
|
359 |
+
},
|
360 |
+
"Rejection Certificate": {
|
361 |
+
"code": "XXX0FS",
|
362 |
+
"description": "A certificate issued by the importing country when an export consignment is rejected, providing details of the rejection."
|
363 |
+
},
|
364 |
+
"Narcotics Certificate": {
|
365 |
+
"code": "XXX001",
|
366 |
+
"description": "A certificate issued by the country of origin certifying that an item does not contain any narcotic or psychotropic substances."
|
367 |
+
},
|
368 |
+
"Radio Activity Report": {
|
369 |
+
"code": "XXX003",
|
370 |
+
"description": "A report providing the levels of radioactive elements in food items, ensuring compliance with safety standards."
|
371 |
+
},
|
372 |
+
"Protocols Approval Letter": {
|
373 |
+
"code": "XXX000",
|
374 |
+
"description": "An approval letter containing details of protocols to be followed in the handling of a product, ensuring compliance with regulatory requirements."
|
375 |
+
}
|
376 |
+
}
|
377 |
+
# Load zero-shot classifier pipeline
|
378 |
+
classifier = pipeline("zero-shot-classification", model="knowledgator/comprehend_it-base")
|
379 |
+
|
380 |
+
# Document type labels and descriptions
|
381 |
+
# document_types = {
|
382 |
+
# # Wildlife Crime Control Bureau (WCCB)
|
383 |
+
# "CITES Certificate": {
|
384 |
+
# "code": "6261WC",
|
385 |
+
# "description": "A certificate used in the trade of endangered species in accordance with the CITES convention."
|
386 |
+
# },
|
387 |
+
# "Captive Bred/Artificially Propagated Certificate": {
|
388 |
+
# "code": "626WC3",
|
389 |
+
# "description": "A certificate issued by WCCB for species that fall under Appendix 1 of CITES, certifying that the species were bred or propagated artificially."
|
390 |
+
# },
|
391 |
+
|
392 |
+
# # Central Pollution Control Board (CPCB)
|
393 |
+
# "Extended Producers Responsibility Authorization (EPRA)": {
|
394 |
+
# "code": "651001",
|
395 |
+
# "description": "Authorization issued by CPCB for producers to manage the end-of-life disposal of their products responsibly."
|
396 |
+
# },
|
397 |
+
# }
|
398 |
+
|
399 |
+
LABELS = list(document_types.keys())
|
400 |
+
label_info = "\n".join(f"- {label} ({document_types[label]['code']})" for label in LABELS)
|
401 |
+
print(label_info)
|
402 |
+
|
403 |
+
classifier = pipeline("zero-shot-classification", model="knowledgator/comprehend_it-base")
|
404 |
+
client = InferenceClient("huggingchat/pdf-to-markdown")
|
405 |
+
import os
|
406 |
+
os.environ["GROQ_API_KEY"] = os.environ.get("GROQ_API_KEY")
|
407 |
+
|
408 |
+
|
409 |
+
phi_agent = Agent(
|
410 |
+
name="doc_classifier",
|
411 |
+
role=f'Classify the OCR extracted text into top 3 matching document types among the given document types only with confidence scores. Document types are {label_info}. Dont forget to mention the document codes also',
|
412 |
+
instructions=[
|
413 |
+
"Read OCR extracted document text and classify it into the top 3 document types provided.",
|
414 |
+
f"Don't choose any document type of your own. Choose only from the given document types: {label_info}"
|
415 |
+
],
|
416 |
+
model=Groq(id="deepseek-r1-distill-llama-70b"),
|
417 |
+
response_model=StructuredOutput,
|
418 |
+
markdown=True,
|
419 |
+
debug_mode=True,
|
420 |
+
show_tool_calls=True,
|
421 |
+
monitoring=True
|
422 |
+
)
|
423 |
+
class StructuredOutput(BaseModel):
|
424 |
+
doc_type_1: Optional[str] = Field(None)
|
425 |
+
confidence_1: Optional[float] = Field(None)
|
426 |
+
doc_type_2: Optional[str] = Field(None)
|
427 |
+
confidence_2: Optional[float] = Field(None)
|
428 |
+
doc_type_3: Optional[str] = Field(None)
|
429 |
+
confidence_3: Optional[float] = Field(None)
|
430 |
+
|
431 |
+
def extract_text_from_pdf(file_path):
|
432 |
+
try:
|
433 |
+
with open(file_path, "rb") as f:
|
434 |
+
result = client.post("/predict", files={"pdf_file": f})
|
435 |
+
extracted_text = result[0] if isinstance(result, (list, tuple)) and len(result) > 0 else ""
|
436 |
+
return extracted_text.strip()
|
437 |
+
except Exception as e:
|
438 |
+
return f"Error extracting text from PDF: {e}"
|
439 |
+
|
440 |
+
def extract_text_from_image(image):
|
441 |
+
try:
|
442 |
+
text = pytesseract.image_to_string(image)
|
443 |
+
return text
|
444 |
+
except Exception as e:
|
445 |
+
return f"Error extracting text from image: {e}"
|
446 |
+
|
447 |
+
def classify_with_bart(text, candidate_labels):
|
448 |
+
result = classifier(text, candidate_labels=candidate_labels, multi_label=True)
|
449 |
+
top_scores = list(zip(result['labels'][:3], result['scores'][:3]))
|
450 |
+
return {lbl: round(score * 100, 2) for lbl, score in top_scores}
|
451 |
+
|
452 |
+
def classify_with_phi(text, label_info):
|
453 |
+
prompt = f'Classify the below text of document: {text} Given Document types: {label_info}'
|
454 |
+
response: RunResponse = phi_agent.run(prompt)
|
455 |
+
result = response.content
|
456 |
+
return {
|
457 |
+
result.doc_type_1: round(result.confidence_1 , 2),
|
458 |
+
result.doc_type_2: round(result.confidence_2 , 2),
|
459 |
+
result.doc_type_3: round(result.confidence_3 , 2)
|
460 |
+
}
|
461 |
+
|
462 |
+
def process_file(input_file, classifier_choice):
|
463 |
+
file_path = input_file.name
|
464 |
+
file_ext = os.path.splitext(file_path)[-1].lower()
|
465 |
+
text = ""
|
466 |
+
|
467 |
+
if file_ext in [".jpg", ".jpeg", ".png", ".bmp"]:
|
468 |
+
image = Image.open(file_path)
|
469 |
+
text = extract_text_from_image(image)
|
470 |
+
elif input_file.name.endswith(".pdf"):
|
471 |
+
text = extract_text_from_pdf(file_path)
|
472 |
+
else:
|
473 |
+
return "Unsupported file format", {}, {}
|
474 |
+
|
475 |
+
if not text.strip():
|
476 |
+
return "No text extracted.", {}, {}
|
477 |
+
|
478 |
+
if classifier_choice == "BART Zero-Shot Classifier":
|
479 |
+
scores_dict = classify_with_bart(text, LABELS)
|
480 |
+
else:
|
481 |
+
scores_dict = classify_with_phi(text, label_info)
|
482 |
+
|
483 |
+
return text, scores_dict, scores_dict
|
484 |
+
|
485 |
+
with gr.Blocks() as demo:
|
486 |
+
gr.Markdown("""
|
487 |
+
# 🧠e-Sanchit Document Type Classification App
|
488 |
+
Upload a **PDF or image**, extract text using OCR, and classify it!
|
489 |
+
""")
|
490 |
+
|
491 |
+
with gr.Row():
|
492 |
+
input_file = gr.File(label="Upload Image or PDF")
|
493 |
+
classifier_choice = gr.Radio(["BART Zero-Shot Classifier", "Phi Agent Classifier"],value="Phi Agent Classifier",visible=False, label="Choose Classifier")
|
494 |
+
|
495 |
+
output_text = gr.Textbox(label="Extracted Text", lines=10)
|
496 |
+
output_labels = gr.Label(num_top_classes=3, label="Top 3 Predictions with Confidence Scores")
|
497 |
+
#output_barplot = gr.BarPlot(label="Prediction Scores", x="Document Type", y="Confidence (%)")
|
498 |
+
|
499 |
+
classify_button = gr.Button("Run Classification")
|
500 |
+
|
501 |
+
classify_button.click(
|
502 |
+
process_file,
|
503 |
+
inputs=[input_file, classifier_choice],
|
504 |
+
outputs=[output_text, output_labels]
|
505 |
+
)
|
506 |
+
|
507 |
+
gr.HTML(f"""
|
508 |
+
<hr>
|
509 |
+
<h4>📄 Available Document Types: Total 89 types </h4>
|
510 |
+
<ul style='line-height:1.6;'>{label_info}</ul>
|
511 |
+
""")
|
512 |
+
|
513 |
+
demo.launch(debug=True)
|
514 |
+
|
515 |
+
# Initialize HuggingFace Inference client for PDF OC
|