Spaces:
Sleeping
Sleeping
Download pdf_utils.py from prithvi55/talk2data: direct link, hf CLI and curl.
- Browser
- Download file 3.54 kB
-
https://huggingface.co/spaces/prithvi55/talk2data/resolve/main/pdf_utils.py
- Command line
-
hf download hf://spaces/prithvi55/talk2data/pdf_utils.py
-
curl -L -o pdf_utils.py https://huggingface.co/spaces/prithvi55/talk2data/resolve/main/pdf_utils.py
3.54 kB
| import pandas as pd | |
| try: | |
| import PyPDF2 | |
| except ImportError: | |
| PyPDF2 = None | |
| try: | |
| from docx import Document | |
| except ImportError: | |
| Document = None | |
| def extract_csv_text(uploaded_file): | |
| try: | |
| df = pd.read_csv(uploaded_file) | |
| # Create a summary: columns, shape, and first few rows | |
| summary = f"CSV Columns: {', '.join(df.columns)}\nShape: {df.shape}\nPreview:\n{df.head().to_string(index=False)}" | |
| return summary, None | |
| except Exception as e: | |
| return None, f"CSV extraction error: {e}" | |
| def extract_excel_text(uploaded_file): | |
| try: | |
| df = pd.read_excel(uploaded_file) | |
| summary = f"Excel Columns: {', '.join(df.columns)}\nShape: {df.shape}\nPreview:\n{df.head().to_string(index=False)}" | |
| return summary, None | |
| except Exception as e: | |
| return None, f"Excel extraction error: {e}" | |
| def extract_json_text(uploaded_file): | |
| try: | |
| import json | |
| # Read the JSON file | |
| json_data = json.load(uploaded_file) | |
| # Create a summary based on JSON structure | |
| if isinstance(json_data, list): | |
| # Array of objects | |
| summary = f"JSON Array with {len(json_data)} items\n" | |
| if json_data and isinstance(json_data[0], dict): | |
| keys = list(json_data[0].keys()) | |
| summary += f"Keys in first object: {', '.join(keys)}\n" | |
| summary += f"Sample data:\n{json.dumps(json_data[:3], indent=2)}" | |
| else: | |
| summary += f"First few items:\n{json.dumps(json_data[:5], indent=2)}" | |
| elif isinstance(json_data, dict): | |
| # Single object | |
| summary = f"JSON Object with keys: {', '.join(json_data.keys())}\n" | |
| summary += f"Data:\n{json.dumps(json_data, indent=2)[:1000]}..." | |
| else: | |
| # Simple value | |
| summary = f"JSON Value:\n{json.dumps(json_data, indent=2)}" | |
| return summary, None | |
| except Exception as e: | |
| return None, f"JSON extraction error: {e}" | |
| import pandas as pd | |
| try: | |
| import PyPDF2 | |
| except ImportError: | |
| PyPDF2 = None | |
| def extract_pdf_text(uploaded_file): | |
| if PyPDF2 is None: | |
| return None, "PyPDF2 not installed. PDF support unavailable." | |
| try: | |
| pdf_reader = PyPDF2.PdfReader(uploaded_file) | |
| text = "\n".join(page.extract_text() or '' for page in pdf_reader.pages) | |
| # Check if any text was extracted | |
| if not text or text.strip() == '': | |
| return None, "No text could be extracted from this PDF. The PDF might be image-based or encrypted." | |
| return text, None | |
| except Exception as e: | |
| return None, f"PDF extraction error: {str(e)}" | |
| def extract_docx_text(uploaded_file): | |
| """Extract text from DOCX files""" | |
| if Document is None: | |
| return None, "python-docx not installed. DOCX support unavailable." | |
| try: | |
| doc = Document(uploaded_file) | |
| paragraphs = [] | |
| for paragraph in doc.paragraphs: | |
| if paragraph.text.strip(): | |
| paragraphs.append(paragraph.text.strip()) | |
| text = "\n".join(paragraphs) | |
| # Check if any text was extracted | |
| if not text or text.strip() == '': | |
| return None, "No text could be extracted from this DOCX file." | |
| return text, None | |
| except Exception as e: | |
| return None, f"DOCX extraction error: {str(e)}" | |