Spaces:
Runtime error
Runtime error
# ========== 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() | |