from pathlib import Path from urllib.parse import urlparse from .file_analyzer import analyze_file, get_supported_extensions MAX_ROUTED_TEXT_CHARACTERS = 400000 def process_input(input_data, multimodal_manager): raw_input = str(input_data).strip() if not raw_input: raise ValueError("Please enter some input.") if is_youtube_url(raw_input): transcript = multimodal_manager.extract_youtube_transcript(raw_input) return format_routed_text( "youtube", transcript, raw_input, ) if looks_like_file_path(raw_input): file_path = Path(raw_input) file_data = analyze_file(file_path, multimodal_manager) return format_routed_text( file_data["kind"], file_data["content"], file_data["name"], ) return raw_input def looks_like_file_path(raw_input): file_path = Path(raw_input) if file_path.exists(): return True if file_path.suffix.lower() in get_supported_extensions(): return True if "\\" in raw_input or "/" in raw_input: return True if len(raw_input) > 2 and raw_input[1] == ":": return True return False def is_youtube_url(raw_input): parsed_url = urlparse(raw_input) host = parsed_url.netloc.lower() return host in { "youtube.com", "www.youtube.com", "m.youtube.com", "youtu.be", "www.youtu.be", } def format_routed_text(input_kind, content, source_name): clean_content = content.strip() if not clean_content: raise ValueError("No readable content was found for this input.") if len(clean_content) > MAX_ROUTED_TEXT_CHARACTERS: clean_content = clean_content[:MAX_ROUTED_TEXT_CHARACTERS].strip() if input_kind == "pdf": return f"Source: PDF file ({source_name})\n\n{clean_content}" if input_kind == "image": return f"Source: Image file ({source_name})\n\n{clean_content}" if input_kind == "document": return f"Source: Document file ({source_name})\n\n{clean_content}" if input_kind == "media": return f"Source: Audio or video file ({source_name})\n\n{clean_content}" if input_kind == "youtube": return f"Source: YouTube video ({source_name})\n\n{clean_content}" return clean_content