Spaces:
Sleeping
Sleeping
File size: 3,544 Bytes
904103f | 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 | 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)}"
|