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
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
import gradio as gr
from transformers import pipeline
from PIL import Image
import pytesseract
#import fitz  # PyMuPDF
import os
from huggingface_hub import InferenceClient
from phi.agent import Agent, RunResponse
from phi.model.groq import Groq
from pydantic import BaseModel, Field
from typing import Optional
document_types = {
    # Wildlife Crime Control Bureau (WCCB)
    "CITES Certificate": {
        "code": "6261WC",
        "description": "A certificate used in the trade of endangered species in accordance with the CITES convention."
    },
    "Captive Bred/Artificially Propagated Certificate": {
        "code": "626WC3",
        "description": "A certificate issued by WCCB for species that fall under Appendix 1 of CITES, certifying that the species were bred or propagated artificially."
    },

    # Central Pollution Control Board (CPCB)
    "Extended Producers Responsibility Authorization (EPRA)": {
        "code": "651001",
        "description": "Authorization issued by CPCB for producers to manage the end-of-life disposal of their products responsibly."
    },

    # Central Drug Standard Control Organization (CDSCO)
    "Invoice": {
        "code": "380000",
        "description": "A commercial document issued by a seller to a buyer, indicating the products, quantities, and agreed prices for products or services."
    },
    "Invoice cum Packing List": {
        "code": "331000",
        "description": "A combined document serving as both an invoice and a packing list, detailing the contents of the shipment."
    },
    "Master Bill of Lading": {
        "code": "704000",
        "description": "A bill of lading issued by the carrier or its agent, covering multiple house bills of lading."
    },
    "House Bill of Lading": {
        "code": "714000",
        "description": "A bill of lading issued by a freight forwarder or consolidator to individual shippers, covering part of a larger shipment."
    },
    "Bill of Lading": {
        "code": "705000",
        "description": "A legal document issued by a carrier to a shipper, detailing the type, quantity, and destination of the goods being carried."
    },
    "House Waybill": {
        "code": "703000",
        "description": "A transport document issued by a freight forwarder or consolidator, serving as a receipt for goods shipped."
    },
    "Tanker Bill of Lading": {
        "code": "709000",
        "description": "A bill of lading specifically for liquid bulk cargo transported in tankers."
    },
    "Sea Waybill (Non Negotiable)": {
        "code": "710000",
        "description": "A non-negotiable transport document evidencing the contract for the carriage of goods by sea."
    },
    "Inland Waterway Bill of Lading": {
        "code": "711000",
        "description": "A negotiable transport document for goods transported via inland waterways."
    },
    "Air Waybill": {
        "code": "740000",
        "description": "A transport document issued by an airline for the carriage of goods by air."
    },
    "Master Air Waybill": {
        "code": "741000",
        "description": "A transport document issued by an airline or consolidator for consolidated shipments from multiple shippers."
    },
    "Waybill (Non Negotiable)": {
        "code": "700000",
        "description": "A non-negotiable document evidencing the contract for the transport of cargo."
    },

    # Other Documents
    "CC for Tea Import": {
        "code": "911TB1",
        "description": "Certificate for tea import issued by the Tea Board."
    },
    "Kimberley Process Certificate": {
        "code": "002KP0",
        "description": "A certificate issued by GJEPC for the export and import of rough diamonds, ensuring they are conflict-free."
    },
    "Import Certificate for Narcotic Drugs": {
        "code": "911BN2",
        "description": "A certificate issued by CBN for the import of narcotic drugs, ensuring compliance with regulations."
    },
    "Registration Certificate (Drugs)": {
        "code": "101DC1",
        "description": "A certificate issued by CDSCO for the registration of drug manufacturers."
    },
    "Registration Certificate (Cosmetics)": {
        "code": "101DC2",
        "description": "A certificate issued by CDSCO for the registration of cosmetic products."
    },
    "Import Permit for Non-Insecticidal Use": {
        "code": "911CI3",
        "description": "A permit issued by CIB&RC for the import of products intended for non-insecticidal use."
    },
    "Permit for Import of Livestock Products": {
        "code": "911DF1",
        "description": "A permit issued by DADF for the import of livestock products, ensuring compliance with health and safety standards."
    },
    "NOC for Import/Procurement of Aircraft": {
        "code": "911DA3",
        "description": "A No Objection Certificate issued by DGCA for the import or procurement of aircraft for air transport services."
    },
    "Licence Form LE-8 (Licence to Import Explosives)": {
        "code": "911PE1",
        "description": "A license issued by PESO for the import of explosives, ensuring compliance with safety regulations."
    },
    "License to Import Ammonium Nitrate": {
        "code": "911PE2",
        "description": "A license issued by PESO for the import of ammonium nitrate, ensuring compliance with safety regulations."
    },
    "Import Permit": {
        "code": "911PQ1",
        "description": "A permit issued by PQIS for the import of specific goods, ensuring compliance with regulatory requirements."
    },
    "No Objection Certificate (NOC) for Natural Rubber": {
        "code": "911RB1",
        "description": "A No Objection Certificate issued by the Rubber Board for the import of natural rubber."
    },
    "Registration-Cum-Membership Certificate (RCMC)": {
        "code": "101TO2",
        "description": "A certificate issued by the Tobacco Board for the registration and membership of exporters."
    },
    "Certificate of Registration (Publisher/Owner)": {
        "code": "101RN1",
        "description": "A certificate issued by RNI for the registration of publishers or owners of publications."
    },
    "No Objection Certificate for Procurement of Radiation Source": {
        "code": "911AB1",
        "description": "A No Objection Certificate issued by AERB for the procurement of radiation sources."
    },
    "Import License for Drugs": {
        "code": "9111DC",
        "description": "A license issued by CDSCO for the import of drugs, ensuring compliance with regulatory requirements."
    },
    "Certificate of Analysis": {
        "code": "001000",
        "description": "A certificate providing the results of an analysis of a product, attesting to its quality or purity."
    },
    "Certificate of Conformity": {
        "code": "002000",
        "description": "A certificate certifying that a product conforms to predefined specifications or standards."
    },
    "Certificate of Quality": {
        "code": "003000",
        "description": "A certificate certifying the quality of goods, services, or processes."
    },
    "Test Report": {
        "code": "004000",
        "description": "A report providing the results of tests conducted on a product or material."
    },
    "Price/Sales Catalogue": {
        "code": "005000",
        "description": "A document providing pricing and catalogue details for goods and services offered by a seller."
    },
    "Product Specification Report": {
        "code": "006000",
        "description": "A report providing detailed specifications of a product."
    },
    "Federal Label Approval": {
        "code": "011000",
        "description": "A document relating to federal label approval requirements for packaged products."
    },
    "Vaccination Certificate": {
        "code": "0380AQ",
        "description": "An official document proving immunization against certain diseases."
    },
    "Safety and Hazard Data Sheet": {
        "code": "0530HZ",
        "description": "A document providing information about the hazards of a material and advice on its safe handling and storage."
    },
    "Registration Document": {
        "code": "101000",
        "description": "An official document providing registration details for a business, organization, or individual."
    },
    "Importer Exporter Certificate": {
        "code": "101001",
        "description": "A certificate issued by DGFT to authorize an entity to engage in import or export activities."
    },
    "Purchase Order": {
        "code": "105000",
        "description": "A document issued by a buyer to a seller, specifying the details of a purchase transaction."
    },
    "Bond": {
        "code": "165000",
        "description": "A document indicating a financial guarantee for the payment of duties or performance of obligations."
    },
    "Crew List Declaration": {
        "code": "250000",
        "description": "A declaration listing the crew members aboard a conveyance, such as a ship or aircraft."
    },
    "Fumigation Certificate": {
        "code": "2670PQ",
        "description": "A certificate attesting that fumigation has been performed on goods to eliminate pests."
    },
    "Packing List": {
        "code": "271000",
        "description": "A document specifying the distribution of goods in individual packages within a shipment."
    },
    "Inspection Report": {
        "code": "293000",
        "description": "A report informing a party of the results of an inspection conducted on goods or facilities."
    },
    "Dangerous Goods List": {
        "code": "298000",
        "description": "A listing of all details of dangerous goods carried, including their classification and handling requirements."
    },
    "Contract": {
        "code": "315000",
        "description": "A document evidencing an agreement between parties for the supply of goods or services."
    },
    "Proforma Invoice": {
        "code": "325000",
        "description": "A preliminary invoice containing similar information to the final invoice but not claiming immediate payment."
    },
    "Banker's Guarantee": {
        "code": "430000",
        "description": "A document in which a bank guarantees the payment of a specified amount to a designated party under certain conditions."
    },
    "Documentary Credit": {
        "code": "465000",
        "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."
    },
    "Free Sale Certificate": {
        "code": "627000",
        "description": "A certificate confirming that a specified product is freely available for sale in the country of origin."
    },
    "Veterinary Quarantine Certificate": {
        "code": "629000",
        "description": "A certification that livestock or animal products are kept under health supervision for a specified period as per veterinary quarantine instructions."
    },
    "Health Certificate": {
        "code": "636000",
        "description": "A document legalized by a competent authority, declaring that a product is free from pathogens and fit for human consumption."
    },
    "Food Grade Certificate": {
        "code": "637000",
        "description": "A document certifying that a product is suitable for use in the food industry."
    },
    "Certificate of Compliance with OIE Standards": {
        "code": "648000",
        "description": "A certification that products have been treated in accordance with standards set by the World Organization for Animal Health (OIE)."
    },
    "Production Facility License": {
        "code": "649000",
        "description": "A license granted by a competent authority to a production facility for manufacturing specific products."
    },
    "Consent to Operate/Registration": {
        "code": "6490HZ",
        "description": "A certificate issued by a State Pollution Control Board to operate a facility, ensuring compliance with environmental regulations."
    },
    "Certified Copy of the Licensed Premises": {
        "code": "6490DC",
        "description": "A certified copy of the premises where drugs are stored, issued by a competent authority."
    },
    "Manufacturing License": {
        "code": "651000",
        "description": "A license granted by a competent authority to a manufacturer for the production of specific products."
    },
    "Industrial License": {
        "code": "651001",
        "description": "A license granted by a competent authority to establish and operate a specific industry."
    },
    "Phytosanitary Re-export Certificate": {
        "code": "6570PQ",
        "description": "A certificate attesting that a consignment meets phytosanitary import requirements for re-export."
    },
    "Rail Consignment Note": {
        "code": "720000",
        "description": "A transport document constituting a contract for the carriage of goods by rail."
    },
    "Road Consignment Note": {
        "code": "730000",
        "description": "A transport document evidencing a contract for the carriage of goods by road."
    },
    "Container Manifest": {
        "code": "788000",
        "description": "A document specifying the contents of particular freight containers or other transport units."
    },
    "Maritime Declaration of Health": {
        "code": "797000",
        "description": "A document certifying the health condition on board a vessel, valid until a specified date."
    },
    "Certificate of Registry": {
        "code": "798000",
        "description": "An official certificate stating the vessel's registry details."
    },
    "Ship's Stores Declaration": {
        "code": "799000",
        "description": "A declaration to Customs regarding the contents of the ship's stores, including goods for passengers and crew."
    },
    "Export Licence": {
        "code": "811000",
        "description": "A permit issued by a government authority authorizing the exportation of a specified commodity subject to certain conditions."
    },
    "Phytosanitary Certificate": {
        "code": "8510FS",
        "description": "A certificate attesting that a consignment meets phytosanitary import requirements."
    },
    "Sanitary Certificate": {
        "code": "852000",
        "description": "A document issued by the competent authority in the exporting country, certifying that alimentary and animal products are fit for human consumption."
    },
    "Veterinary Certificate": {
        "code": "853000",
        "description": "A document issued by the competent authority in the exporting country, certifying that live animals or birds are free from disease."
    },
    "Inspection Certificate": {
        "code": "856000",
        "description": "A document issued by a competent body, certifying that goods have been inspected in accordance with national or international standards."
    },
    "Pre-Shipment Inspection Certificate": {
        "code": "856001",
        "description": "A certificate issued prior to shipment, certifying that goods have been inspected and meet specified standards."
    },
    "Certificate of Origin": {
        "code": "861000",
        "description": "A document certifying that goods originate from a specific country."
    },
    "Preference Certificate of Origin": {
        "code": "864000",
        "description": "A certificate of origin meeting the requirements for preferential treatment under trade agreements."
    },
    "Certificate of Origin Form GSP": {
        "code": "865000",
        "description": "A specific form of certificate of origin for goods qualifying for preferential treatment under the Generalized System of Preferences (GSP)."
    },
    "Consular Invoice": {
        "code": "870000",
        "description": "A document prepared by an exporter and endorsed by the diplomatic representation of the importing country, used for customs purposes."
    },
    "Dangerous Goods Declaration": {
        "code": "890000",
        "description": "A document describing hazardous goods for transport purposes, certifying compliance with safety regulations."
    },
    "Import Licence": {
        "code": "911000",
        "description": "A document issued by the competent authority authorizing the import of specific goods under specified conditions."
    },
    "Value Declaration (GATT Valuation Declaration)": {
        "code": "934000",
        "description": "A document in which the importer declares the value of goods for customs purposes, including invoice price, freight, insurance, and other costs."
    },
    "Customs Invoice": {
        "code": "935000",
        "description": "A document required by customs in the importing country, detailing the invoice price, freight, insurance, and other costs for determining customs value."
    },
    "ATA Carnet": {
        "code": "955000",
        "description": "An international customs document used for the temporary admission of goods, serving as a passport for goods."
    },
    "Rejection Certificate": {
        "code": "XXX0FS",
        "description": "A certificate issued by the importing country when an export consignment is rejected, providing details of the rejection."
    },
    "Narcotics Certificate": {
        "code": "XXX001",
        "description": "A certificate issued by the country of origin certifying that an item does not contain any narcotic or psychotropic substances."
    },
    "Radio Activity Report": {
        "code": "XXX003",
        "description": "A report providing the levels of radioactive elements in food items, ensuring compliance with safety standards."
    },
    "Protocols Approval Letter": {
        "code": "XXX000",
        "description": "An approval letter containing details of protocols to be followed in the handling of a product, ensuring compliance with regulatory requirements."
    }
}
# Load zero-shot classifier pipeline
classifier = pipeline("zero-shot-classification", model="knowledgator/comprehend_it-base")

# Document type labels and descriptions
# document_types = {
#     # Wildlife Crime Control Bureau (WCCB)
#     "CITES Certificate": {
#         "code": "6261WC",
#         "description": "A certificate used in the trade of endangered species in accordance with the CITES convention."
#     },
#     "Captive Bred/Artificially Propagated Certificate": {
#         "code": "626WC3",
#         "description": "A certificate issued by WCCB for species that fall under Appendix 1 of CITES, certifying that the species were bred or propagated artificially."
#     },

#     # Central Pollution Control Board (CPCB)
#     "Extended Producers Responsibility Authorization (EPRA)": {
#         "code": "651001",
#         "description": "Authorization issued by CPCB for producers to manage the end-of-life disposal of their products responsibly."
#     },
# }

LABELS = list(document_types.keys())
label_info = "\n".join(f"- {label} ({document_types[label]['code']})" for label in LABELS)
print(label_info)

classifier = pipeline("zero-shot-classification", model="knowledgator/comprehend_it-base")
client = InferenceClient("huggingchat/pdf-to-markdown")
import os
os.environ["GROQ_API_KEY"] = os.environ.get("GROQ_API_KEY")


phi_agent = Agent(
    name="doc_classifier",
    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',
    instructions=[
        "Read OCR extracted document text and classify it into the top 3 document types provided.",
        f"Don't choose any document type of your own. Choose only from the given document types: {label_info}"
    ],
    model=Groq(id="deepseek-r1-distill-llama-70b"),
    response_model=StructuredOutput,
    markdown=True,
    debug_mode=True,
    show_tool_calls=True,
    monitoring=True
)
class StructuredOutput(BaseModel):
    doc_type_1: Optional[str] = Field(None)
    confidence_1: Optional[float] = Field(None)
    doc_type_2: Optional[str] = Field(None)
    confidence_2: Optional[float] = Field(None)
    doc_type_3: Optional[str] = Field(None)
    confidence_3: Optional[float] = Field(None)

def extract_text_from_pdf(file_path):
    try:
        with open(file_path, "rb") as f:
            result = client.post("/predict", files={"pdf_file": f})
            extracted_text = result[0] if isinstance(result, (list, tuple)) and len(result) > 0 else ""
            return extracted_text.strip()
    except Exception as e:
        return f"Error extracting text from PDF: {e}"

def extract_text_from_image(image):
    try:
        text = pytesseract.image_to_string(image)
        return text
    except Exception as e:
        return f"Error extracting text from image: {e}"

def classify_with_bart(text, candidate_labels):
    result = classifier(text, candidate_labels=candidate_labels, multi_label=True)
    top_scores = list(zip(result['labels'][:3], result['scores'][:3]))
    return {lbl: round(score * 100, 2) for lbl, score in top_scores}

def classify_with_phi(text, label_info):
    prompt = f'Classify the below text of document: {text} Given Document types: {label_info}'
    response: RunResponse = phi_agent.run(prompt)
    result = response.content
    return {
        result.doc_type_1: round(result.confidence_1 , 2),
        result.doc_type_2: round(result.confidence_2 , 2),
        result.doc_type_3: round(result.confidence_3 , 2)
    }

def process_file(input_file, classifier_choice):
    file_path = input_file.name
    file_ext = os.path.splitext(file_path)[-1].lower()
    text = ""

    if file_ext in [".jpg", ".jpeg", ".png", ".bmp"]:
        image = Image.open(file_path)
        text = extract_text_from_image(image)
    elif input_file.name.endswith(".pdf"):
        text = extract_text_from_pdf(file_path)
    else:
        return "Unsupported file format", {}, {}

    if not text.strip():
        return "No text extracted.", {}, {}

    if classifier_choice == "BART Zero-Shot Classifier":
        scores_dict = classify_with_bart(text, LABELS)
    else:
        scores_dict = classify_with_phi(text, label_info)

    return text, scores_dict, scores_dict

with gr.Blocks() as demo:
    gr.Markdown("""
    # 🧠e-Sanchit Document Type Classification App
    Upload a **PDF or image**, extract text using OCR, and classify it!
    """)

    with gr.Row():
        input_file = gr.File(label="Upload Image or PDF")
        classifier_choice = gr.Radio(["BART Zero-Shot Classifier", "Phi Agent Classifier"],value="Phi Agent Classifier",visible=False, label="Choose Classifier")

    output_text = gr.Textbox(label="Extracted Text", lines=10)
    output_labels = gr.Label(num_top_classes=3, label="Top 3 Predictions with Confidence Scores")
    #output_barplot = gr.BarPlot(label="Prediction Scores", x="Document Type", y="Confidence (%)")

    classify_button = gr.Button("Run Classification")

    classify_button.click(
        process_file,
        inputs=[input_file, classifier_choice],
        outputs=[output_text, output_labels]
    )

    gr.HTML(f"""
    <hr>
    <h4>📄 Available Document Types: Total 89 types </h4>
    <ul style='line-height:1.6;'>{label_info}</ul>
    """)

    demo.launch(debug=True)

# Initialize HuggingFace Inference client for PDF OC