""" AI Dataset Studio - Minimal Version Guaranteed to work with basic dependencies only """ import gradio as gr import pandas as pd import json import re import requests from bs4 import BeautifulSoup from urllib.parse import urlparse from datetime import datetime import logging from typing import Dict, List, Tuple, Optional, Any from dataclasses import dataclass, asdict import uuid import time # Configure logging logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) @dataclass class SimpleScrapedItem: """Simplified scraped content structure""" id: str url: str title: str content: str word_count: int scraped_at: str quality_score: float = 0.0 class SimpleWebScraper: """Simplified web scraper with basic functionality""" def __init__(self): self.session = requests.Session() self.session.headers.update({ 'User-Agent': 'Mozilla/5.0 (compatible; AI-DatasetStudio/1.0)', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8' }) def scrape_url(self, url: str) -> Optional[SimpleScrapedItem]: """Scrape a single URL""" try: if not self._validate_url(url): return None response = self.session.get(url, timeout=10) response.raise_for_status() soup = BeautifulSoup(response.content, 'html.parser') # Extract title title_tag = soup.find('title') title = title_tag.get_text().strip() if title_tag else "Untitled" # Extract content # Remove unwanted elements for element in soup(['script', 'style', 'nav', 'header', 'footer']): element.decompose() # Try to find main content content_element = (soup.find('article') or soup.find('main') or soup.find(class_='content') or soup.find('body')) if content_element: content = content_element.get_text(separator=' ', strip=True) else: content = soup.get_text(separator=' ', strip=True) # Clean content content = re.sub(r'\s+', ' ', content).strip() # Calculate basic metrics word_count = len(content.split()) quality_score = min(1.0, word_count / 100) if word_count > 0 else 0.0 return SimpleScrapedItem( id=str(uuid.uuid4()), url=url, title=title, content=content, word_count=word_count, scraped_at=datetime.now().isoformat(), quality_score=quality_score ) except Exception as e: logger.error(f"Failed to scrape {url}: {e}") return None def _validate_url(self, url: str) -> bool: """Basic URL validation""" try: parsed = urlparse(url) return parsed.scheme in ['http', 'https'] and parsed.netloc except: return False def batch_scrape(self, urls: List[str], progress_callback=None) -> List[SimpleScrapedItem]: """Scrape multiple URLs""" results = [] total = len(urls) for i, url in enumerate(urls): if progress_callback: progress_callback((i + 1) / total, f"Scraping {i+1}/{total}") item = self.scrape_url(url) if item: results.append(item) time.sleep(1) # Rate limiting return results class SimpleDataProcessor: """Basic data processing""" def process_items(self, items: List[SimpleScrapedItem], options: Dict[str, bool]) -> List[SimpleScrapedItem]: """Process scraped items""" processed = [] for item in items: # Apply quality filter if options.get('quality_filter', True) and item.quality_score < 0.3: continue # Clean text if requested if options.get('clean_text', True): item.content = self._clean_text(item.content) processed.append(item) return processed def _clean_text(self, text: str) -> str: """Basic text cleaning""" # Remove URLs text = re.sub(r'http\S+', '', text) # Remove extra whitespace text = re.sub(r'\s+', ' ', text) # Remove common navigation text text = re.sub(r'(Click here|Read more|Subscribe|Advertisement)', '', text, flags=re.IGNORECASE) return text.strip() class SimpleExporter: """Basic export functionality""" def export_dataset(self, items: List[SimpleScrapedItem], format_type: str) -> str: """Export dataset""" timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") if format_type == "json": filename = f"dataset_{timestamp}.json" data = [asdict(item) for item in items] with open(filename, 'w', encoding='utf-8') as f: json.dump(data, f, indent=2, ensure_ascii=False) return filename elif format_type == "csv": filename = f"dataset_{timestamp}.csv" data = [asdict(item) for item in items] df = pd.DataFrame(data) df.to_csv(filename, index=False) return filename else: raise ValueError(f"Unsupported format: {format_type}") class SimpleDatasetStudio: """Simplified main application""" def __init__(self): self.scraper = SimpleWebScraper() self.processor = SimpleDataProcessor() self.exporter = SimpleExporter() self.scraped_items = [] self.processed_items = [] self.current_project = None def create_project(self, name: str) -> Dict[str, Any]: """Create a new project""" self.current_project = { 'name': name, 'id': str(uuid.uuid4()), 'created_at': datetime.now().isoformat() } self.scraped_items = [] self.processed_items = [] return self.current_project def scrape_urls(self, urls: List[str], progress_callback=None) -> Tuple[int, List[str]]: """Scrape URLs""" url_list = [url.strip() for url in urls if url.strip()] if not url_list: return 0, ["No valid URLs provided"] self.scraped_items = self.scraper.batch_scrape(url_list, progress_callback) success_count = len(self.scraped_items) failed_count = len(url_list) - success_count errors = [] if failed_count > 0: errors.append(f"{failed_count} URLs failed") return success_count, errors def process_data(self, options: Dict[str, bool]) -> int: """Process scraped data""" if not self.scraped_items: return 0 self.processed_items = self.processor.process_items(self.scraped_items, options) return len(self.processed_items) def get_preview(self) -> List[Dict[str, Any]]: """Get data preview""" items = self.processed_items or self.scraped_items preview = [] for item in items[:5]: preview.append({ 'Title': item.title[:50] + "..." if len(item.title) > 50 else item.title, 'Content Preview': item.content[:100] + "..." if len(item.content) > 100 else item.content, 'Word Count': item.word_count, 'Quality Score': round(item.quality_score, 2), 'URL': item.url[:50] + "..." if len(item.url) > 50 else item.url }) return preview def get_stats(self) -> Dict[str, Any]: """Get dataset statistics""" items = self.processed_items or self.scraped_items if not items: return {} word_counts = [item.word_count for item in items] quality_scores = [item.quality_score for item in items] return { 'total_items': len(items), 'avg_word_count': round(sum(word_counts) / len(word_counts)), 'avg_quality': round(sum(quality_scores) / len(quality_scores), 2), 'min_words': min(word_counts), 'max_words': max(word_counts) } def export_data(self, format_type: str) -> str: """Export dataset""" items = self.processed_items or self.scraped_items if not items: raise ValueError("No data to export") return self.exporter.export_dataset(items, format_type) def create_simple_interface(): """Create simplified Gradio interface""" studio = SimpleDatasetStudio() # Custom CSS css = """ .container { max-width: 1200px; margin: auto; } .header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 2rem; border-radius: 10px; text-align: center; margin-bottom: 2rem; } .step-box { background: #f8f9ff; border: 1px solid #e1e5ff; border-radius: 8px; padding: 1.5rem; margin: 1rem 0; } """ with gr.Blocks(css=css, title="AI Dataset Studio - Simple") as interface: # Header gr.HTML("""
Create datasets from web content - No complex setup required!