| import re
|
| from pathlib import Path
|
| from typing import Dict, Any, Tuple, Optional
|
|
|
|
|
| class AIAssistant:
|
|
|
| SUPPORTED_FORMATS = {
|
| "document": ["pdf", "docx", "txt", "html", "md"],
|
| "image": ["jpg", "jpeg", "png", "webp", "bmp", "tiff"],
|
| "video": ["mp4", "avi", "mkv", "mov", "webm", "gif"],
|
| "audio": ["mp3", "wav", "ogg", "m4a", "flac", "aac"]
|
| }
|
|
|
| def __init__(self):
|
|
|
| self.knowledge_base = self.load_knowledge_base()
|
|
|
| def load_knowledge_base(self):
|
|
|
| return {
|
|
|
| "document": {
|
| "pdf": {
|
| "best_for": "Sharing and printing documents"
|
| },
|
| "docx": {
|
| "best_for": "Editing documents"
|
| },
|
| "txt": {
|
| "best_for": "Plain text and notes"
|
| }
|
| },
|
|
|
| "image": {
|
| "jpg": {
|
| "best_for": "Photos and web images"
|
| },
|
| "png": {
|
| "best_for": "Transparency and graphics"
|
| },
|
| "webp": {
|
| "best_for": "Modern web compression"
|
| }
|
| },
|
|
|
| "video": {
|
| "mp4": {
|
| "best_for": "Universal compatibility"
|
| },
|
| "mkv": {
|
| "best_for": "High quality storage"
|
| }
|
| },
|
|
|
| "audio": {
|
| "mp3": {
|
| "best_for": "Universal playback"
|
| },
|
| "flac": {
|
| "best_for": "Lossless audio"
|
| }
|
| }
|
| }
|
|
|
| def ask(
|
| self,
|
| question: str,
|
| context: Dict[str, Any]
|
| ) -> str:
|
|
|
| if not question.strip():
|
|
|
| return (
|
| "π¬ Ask me something like:\n"
|
| "β’ Convert this to PDF\n"
|
| "β’ Best format for web\n"
|
| "β’ Analyze this file"
|
| )
|
|
|
| question_lower = question.lower().strip()
|
|
|
|
|
| if any(
|
| x in question_lower
|
| for x in ["hi", "hello", "hey"]
|
| ):
|
|
|
| return (
|
| "π Hello! I'm your AI File Assistant.\n\n"
|
| "Try:\n"
|
| "β’ Convert this to MP4\n"
|
| "β’ Best format for web\n"
|
| "β’ Analyze this file"
|
| )
|
|
|
|
|
| if "thank" in question_lower:
|
|
|
| return (
|
| "π You're welcome!"
|
| )
|
|
|
|
|
| if self.is_conversion_command(
|
| question_lower
|
| ):
|
|
|
| return self.execute_conversion_command(
|
| question_lower,
|
| context
|
| )
|
|
|
|
|
| if any(
|
| x in question_lower
|
| for x in [
|
| "best format",
|
| "recommend",
|
| "suggest"
|
| ]
|
| ):
|
|
|
| return self.suggest_format(
|
| question_lower,
|
| context
|
| )
|
|
|
|
|
| if any(
|
| x in question_lower
|
| for x in [
|
| "analyze",
|
| "what is this",
|
| "what format"
|
| ]
|
| ):
|
|
|
| return self.analyze_file(
|
| context
|
| )
|
|
|
|
|
| if any(
|
| x in question_lower
|
| for x in [
|
| "help",
|
| "how to"
|
| ]
|
| ):
|
|
|
| return self.get_help()
|
|
|
| return self.get_general_response()
|
|
|
| def is_conversion_command(
|
| self,
|
| text: str
|
| ) -> bool:
|
|
|
| keywords = [
|
| "convert",
|
| "change",
|
| "transform",
|
| "make",
|
| "turn into"
|
| ]
|
|
|
| return any(
|
| k in text
|
| for k in keywords
|
| )
|
|
|
| def parse_conversion_request(
|
| self,
|
| text: str
|
| ) -> Optional[str]:
|
|
|
| patterns = [
|
|
|
| r"to\s+(\w+)",
|
|
|
| r"into\s+(\w+)",
|
|
|
| r"make\s+(?:it\s+)?(\w+)",
|
|
|
| r"convert\s+(?:this\s+)?(?:file\s+)?to\s+(\w+)"
|
| ]
|
|
|
| for pattern in patterns:
|
|
|
| match = re.search(
|
| pattern,
|
| text
|
| )
|
|
|
| if match:
|
|
|
| fmt = match.group(1).lower()
|
|
|
|
|
| if fmt == "jpeg":
|
| fmt = "jpg"
|
|
|
| return fmt
|
|
|
| return None
|
|
|
| def execute_conversion_command(
|
| self,
|
| question: str,
|
| context: Dict[str, Any]
|
| ) -> str:
|
|
|
| target_format = self.parse_conversion_request(
|
| question
|
| )
|
|
|
| if not target_format:
|
|
|
| return self.get_conversion_help()
|
|
|
| file_type = context.get(
|
| "file_type",
|
| ""
|
| ).lower()
|
|
|
| current_format = context.get(
|
| "output_format",
|
| ""
|
| ).lower()
|
|
|
| if not file_type:
|
|
|
| return (
|
| "β Please select a file type first."
|
| )
|
|
|
| supported = self.SUPPORTED_FORMATS.get(
|
| file_type,
|
| []
|
| )
|
|
|
| if target_format not in supported:
|
|
|
| return (
|
| f"β {target_format.upper()} "
|
| f"is not supported for "
|
| f"{file_type} files.\n\n"
|
| f"Supported:\n"
|
| f"{', '.join(supported).upper()}"
|
| )
|
|
|
| response = (
|
| "π― Conversion Ready\n\n"
|
| f"Current Type: {file_type.title()}\n"
|
| f"Target Format: {target_format.upper()}\n\n"
|
| )
|
|
|
| advice = self.get_format_advice(
|
| file_type,
|
| target_format
|
| )
|
|
|
| if advice:
|
|
|
| response += (
|
| f"π‘ Tip: {advice}\n\n"
|
| )
|
|
|
| response += (
|
| "β
Now:\n"
|
| "1. Select the format\n"
|
| "2. Add files\n"
|
| "3. Click Start Conversion"
|
| )
|
|
|
| return response
|
|
|
| def suggest_format(
|
| self,
|
| question: str,
|
| context: Dict[str, Any]
|
| ) -> str:
|
|
|
| file_type = context.get(
|
| "file_type",
|
| ""
|
| ).lower()
|
|
|
| if not file_type:
|
|
|
| return (
|
| "β Please select a file type first."
|
| )
|
|
|
| recommendations = {
|
|
|
| "document": {
|
| "web": "HTML",
|
| "print": "PDF",
|
| "edit": "DOCX",
|
| "small": "TXT"
|
| },
|
|
|
| "image": {
|
| "web": "WEBP",
|
| "quality": "PNG",
|
| "photo": "JPG",
|
| "transparent": "PNG"
|
| },
|
|
|
| "video": {
|
| "web": "MP4",
|
| "quality": "MKV",
|
| "small": "WEBM"
|
| },
|
|
|
| "audio": {
|
| "quality": "FLAC",
|
| "small": "MP3",
|
| "edit": "WAV"
|
| }
|
| }
|
|
|
| use_case = "web"
|
|
|
| keywords = {
|
| "print": ["print"],
|
| "edit": ["edit"],
|
| "small": ["small", "compress"],
|
| "quality": ["quality", "lossless"],
|
| "photo": ["photo"],
|
| "transparent": ["transparent"]
|
| }
|
|
|
| for case, words in keywords.items():
|
|
|
| if any(w in question for w in words):
|
|
|
| use_case = case
|
| break
|
|
|
| fmt = recommendations.get(
|
| file_type,
|
| {}
|
| ).get(
|
| use_case,
|
| "PDF"
|
| )
|
|
|
| return (
|
| f"π Recommended Format\n\n"
|
| f"For {use_case} use:\n"
|
| f"β
{fmt}\n\n"
|
| f"Type:\n"
|
| f"Convert this to {fmt}"
|
| )
|
|
|
| def analyze_file(
|
| self,
|
| context: Dict[str, Any]
|
| ) -> str:
|
|
|
| file_type = context.get(
|
| "file_type",
|
| "Unknown"
|
| )
|
|
|
| fmt = context.get(
|
| "output_format",
|
| "Unknown"
|
| ).lower()
|
|
|
| response = (
|
| "π File Analysis\n\n"
|
| f"Type: {file_type.title()}\n"
|
| f"Format: {fmt.upper()}\n\n"
|
| )
|
|
|
| kb = self.knowledge_base.get(
|
| file_type,
|
| {}
|
| )
|
|
|
| if fmt in kb:
|
|
|
| response += (
|
| f"π Best For:\n"
|
| f"{kb[fmt]['best_for']}"
|
| )
|
|
|
| else:
|
|
|
| response += (
|
| "No detailed analysis available."
|
| )
|
|
|
| return response
|
|
|
| def get_help(self) -> str:
|
|
|
| return (
|
| "π€ AI Assistant Help\n\n"
|
| "Commands:\n"
|
| "β’ Convert this to PDF\n"
|
| "β’ Best format for web\n"
|
| "β’ Analyze this file\n"
|
| "β’ Compress this video\n\n"
|
| "Supported:\n"
|
| "Documents, Images, Videos, Audio"
|
| )
|
|
|
| def get_general_response(self) -> str:
|
|
|
| return (
|
| "π€ AI Assistant Ready\n\n"
|
| "Try:\n"
|
| "β’ Convert this to PNG\n"
|
| "β’ Best format for web\n"
|
| "β’ Analyze this file"
|
| )
|
|
|
| def get_format_advice(
|
| self,
|
| file_type: str,
|
| target_format: str
|
| ) -> str:
|
|
|
| tips = {
|
|
|
| ("image", "jpg"):
|
| "Best for photos and smaller size.",
|
|
|
| ("image", "png"):
|
| "Supports transparency.",
|
|
|
| ("image", "webp"):
|
| "Excellent modern compression.",
|
|
|
| ("video", "mp4"):
|
| "Most compatible format.",
|
|
|
| ("video", "gif"):
|
| "Keep clips short for smaller size.",
|
|
|
| ("audio", "mp3"):
|
| "Universal audio playback.",
|
|
|
| ("audio", "flac"):
|
| "Lossless quality.",
|
|
|
| ("document", "pdf"):
|
| "Preserves formatting.",
|
|
|
| ("document", "txt"):
|
| "Very small file size."
|
| }
|
|
|
| return tips.get(
|
| (file_type, target_format),
|
| ""
|
| )
|
|
|
| def get_conversion_help(self) -> str:
|
|
|
| return (
|
| "π― Conversion Help\n\n"
|
| "Examples:\n"
|
| "β’ Convert this to PDF\n"
|
| "β’ Make it MP3\n"
|
| "β’ Change this to PNG\n"
|
| "β’ Transform to MP4"
|
| ) |