File size: 1,897 Bytes
66cb301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e6aad7
 
 
 
 
 
 
9abe9f0
85bd875
 
9abe9f0
66cb301
6e6aad7
 
66cb301
 
 
 
 
 
 
 
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
# ========== STARTUP DEPENDENCY CHECK ==========
import sys
import subprocess
import importlib

# List of required packages with their import names and pip names
required_packages = {
    'gradio': 'gradio',
    'pandas': 'pandas',
    'PyPDF2': 'PyPDF2',
    'transformers': 'transformers',
    'pdfplumber': 'pdfplumber',
    'typing_extensions': 'typing_extensions'  # Often needed for transformers
}

def check_and_install_packages():
    missing_packages = []
    for import_name, pkg_name in required_packages.items():
        try:
            importlib.import_module(import_name)
        except ImportError:
            missing_packages.append(pkg_name)

    if missing_packages:
        print(f"Missing packages: {', '.join(missing_packages)}")
        print("Attempting to install...")
        
        try:
            subprocess.check_call([sys.executable, "-m", "pip", "install", *missing_packages])
            print("Installation successful. Please restart the application.")
            sys.exit(0)
        except subprocess.CalledProcessError as e:
            print(f"Failed to install packages. Error: {e}")
            print("Please install them manually with:")
            print(f"pip install {' '.join(missing_packages)}")
            sys.exit(1)

check_and_install_packages()

# ========== MAIN IMPORTS (AFTER DEPENDENCY CHECK) ==========
import gradio as gr
import pandas as pd
import json
import os
import re
from PyPDF2 import PdfReader
from collections import defaultdict
from transformers import pipeline
from typing import List, Dict, Union
import pdfplumber

[... REST OF YOUR ORIGINAL CODE REMAINS EXACTLY THE SAME ...]

if __name__ == "__main__":
    # Check if running in a notebook environment
    try:
        from IPython import get_ipython
        if 'IPKernelApp' not in get_ipython().config:
            app.launch()
    except:
        app.launch()