Update ai/ai_assistant.py
Browse files- ai/ai_assistant.py +381 -285
ai/ai_assistant.py
CHANGED
|
@@ -1,378 +1,474 @@
|
|
| 1 |
import re
|
| 2 |
-
import os
|
| 3 |
-
import subprocess
|
| 4 |
from pathlib import Path
|
| 5 |
from typing import Dict, Any, Tuple, Optional
|
| 6 |
|
| 7 |
|
| 8 |
class AIAssistant:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def __init__(self):
|
|
|
|
| 10 |
self.knowledge_base = self.load_knowledge_base()
|
| 11 |
|
| 12 |
def load_knowledge_base(self):
|
|
|
|
| 13 |
return {
|
|
|
|
| 14 |
"document": {
|
| 15 |
"pdf": {
|
| 16 |
-
"best_for": "
|
| 17 |
-
"suggested_formats": {
|
| 18 |
-
"word": "For editing documents",
|
| 19 |
-
"txt": "For text extraction",
|
| 20 |
-
"html": "For web display"
|
| 21 |
-
}
|
| 22 |
},
|
| 23 |
-
"
|
| 24 |
-
"best_for": "
|
| 25 |
-
"suggested_formats": {
|
| 26 |
-
"pdf": "For sharing without edits",
|
| 27 |
-
"txt": "For plain text",
|
| 28 |
-
"html": "For web publishing"
|
| 29 |
-
}
|
| 30 |
},
|
| 31 |
"txt": {
|
| 32 |
-
"best_for": "
|
| 33 |
-
"suggested_formats": {
|
| 34 |
-
"pdf": "For professional sharing",
|
| 35 |
-
"docx": "For formatted documents",
|
| 36 |
-
"md": "For documentation"
|
| 37 |
-
}
|
| 38 |
}
|
| 39 |
},
|
|
|
|
| 40 |
"image": {
|
| 41 |
"jpg": {
|
| 42 |
-
"best_for": "
|
| 43 |
-
"suggested_formats": {
|
| 44 |
-
"png": "For images with transparency",
|
| 45 |
-
"webp": "For better web compression",
|
| 46 |
-
"bmp": "For uncompressed quality"
|
| 47 |
-
}
|
| 48 |
},
|
| 49 |
"png": {
|
| 50 |
-
"best_for": "
|
| 51 |
-
"suggested_formats": {
|
| 52 |
-
"jpg": "For smaller file size (photos)",
|
| 53 |
-
"webp": "For web optimization",
|
| 54 |
-
"ico": "For icons"
|
| 55 |
-
}
|
| 56 |
},
|
| 57 |
"webp": {
|
| 58 |
-
"best_for": "
|
| 59 |
-
"suggested_formats": {
|
| 60 |
-
"png": "For editing",
|
| 61 |
-
"jpg": "For compatibility"
|
| 62 |
-
}
|
| 63 |
}
|
| 64 |
},
|
|
|
|
| 65 |
"video": {
|
| 66 |
"mp4": {
|
| 67 |
-
"best_for": "Universal compatibility
|
| 68 |
-
"suggested_formats": {
|
| 69 |
-
"gif": "For short animations",
|
| 70 |
-
"avi": "For editing",
|
| 71 |
-
"mkv": "For high quality storage"
|
| 72 |
-
}
|
| 73 |
},
|
| 74 |
-
"
|
| 75 |
-
"best_for": "
|
| 76 |
-
"suggested_formats": {
|
| 77 |
-
"mp4": "For web sharing",
|
| 78 |
-
"mkv": "For compression"
|
| 79 |
-
}
|
| 80 |
}
|
| 81 |
},
|
|
|
|
| 82 |
"audio": {
|
| 83 |
"mp3": {
|
| 84 |
-
"best_for": "
|
| 85 |
-
"suggested_formats": {
|
| 86 |
-
"wav": "For editing",
|
| 87 |
-
"flac": "For lossless quality",
|
| 88 |
-
"ogg": "For open source projects"
|
| 89 |
-
}
|
| 90 |
},
|
| 91 |
-
"
|
| 92 |
-
"best_for": "
|
| 93 |
-
"suggested_formats": {
|
| 94 |
-
"mp3": "For smaller size",
|
| 95 |
-
"flac": "For lossless compression"
|
| 96 |
-
}
|
| 97 |
}
|
| 98 |
}
|
| 99 |
}
|
| 100 |
|
| 101 |
-
def ask(
|
| 102 |
-
|
| 103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
|
|
|
| 108 |
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
|
|
|
| 112 |
|
| 113 |
-
|
| 114 |
-
elif "what is this" in question_lower or "analyze" in question_lower:
|
| 115 |
-
return self.analyze_file(context)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
elif "help" in question_lower or "how to" in question_lower:
|
| 119 |
-
return self.get_help(question_lower, context)
|
| 120 |
|
| 121 |
-
|
| 122 |
-
else:
|
| 123 |
-
return self.get_general_response(question_lower, context)
|
| 124 |
|
| 125 |
-
|
| 126 |
-
"""Check if user wants to convert a file"""
|
| 127 |
-
commands = ['convert', 'change', 'transform', 'turn into', 'make it']
|
| 128 |
-
return any(cmd in text for cmd in commands)
|
| 129 |
|
| 130 |
-
|
| 131 |
-
"""Parse natural language conversion request"""
|
| 132 |
-
# Pattern: "convert this to pdf" or "change this file to mp3"
|
| 133 |
-
patterns = [
|
| 134 |
-
r'(?:convert|change|transform|turn)\s+(?:this|it|the\s+file)\s+(?:to|into)\s+(\w+)',
|
| 135 |
-
r'to\s+(\w+)\s+format',
|
| 136 |
-
r'convert\s+to\s+(\w+)',
|
| 137 |
]
|
| 138 |
|
| 139 |
for pattern in patterns:
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
if match:
|
| 142 |
-
target_format = match.group(1)
|
| 143 |
-
return target_format, None
|
| 144 |
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
if not target_format:
|
| 152 |
-
return self.get_conversion_help()
|
| 153 |
|
| 154 |
-
|
| 155 |
-
current_type = context.get("file_type", "").lower()
|
| 156 |
-
current_format = context.get("output_format", "").lower()
|
| 157 |
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
-
|
| 162 |
-
response += f"Converting from **{current_format.upper()}** to **{target_format.upper()}**...\n\n"
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
"audio": ["mp3", "wav", "ogg", "m4a", "flac"]
|
| 170 |
-
}
|
| 171 |
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
|
| 176 |
-
|
| 177 |
-
advice = self.get_format_advice(current_type, target_format)
|
| 178 |
-
if advice:
|
| 179 |
-
response += f"π **Advice**: {advice}\n\n"
|
| 180 |
-
else:
|
| 181 |
-
response += f"β **{target_format.upper()}** is not supported for {current_type} files.\n\n"
|
| 182 |
-
response += f"Supported formats: {', '.join(supported_formats.get(current_type, []))}\n\n"
|
| 183 |
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
response += "3. Click 'Start Conversion'"
|
| 188 |
|
| 189 |
-
|
| 190 |
|
| 191 |
-
def suggest_format(self, question: str, context: Dict[str, Any]) -> str:
|
| 192 |
-
"""Suggest best format based on use case"""
|
| 193 |
-
current_type = context.get("file_type", "").lower()
|
| 194 |
-
|
| 195 |
-
# Determine use case
|
| 196 |
-
use_case = ""
|
| 197 |
-
if "web" in question or "website" in question:
|
| 198 |
-
use_case = "web"
|
| 199 |
-
elif "email" in question:
|
| 200 |
-
use_case = "email"
|
| 201 |
-
elif "print" in question:
|
| 202 |
-
use_case = "print"
|
| 203 |
-
elif "edit" in question or "editing" in question:
|
| 204 |
-
use_case = "edit"
|
| 205 |
-
elif "compress" in question or "small" in question:
|
| 206 |
-
use_case = "compress"
|
| 207 |
-
elif "quality" in question or "lossless" in question:
|
| 208 |
-
use_case = "quality"
|
| 209 |
-
|
| 210 |
-
# Format suggestions by use case and type
|
| 211 |
-
suggestions = {
|
| 212 |
"document": {
|
| 213 |
-
"web": "HTML
|
| 214 |
-
"
|
| 215 |
-
"
|
| 216 |
-
"
|
| 217 |
-
"compress": "TXT (smallest) or compressed PDF",
|
| 218 |
-
"quality": "PDF (preserves all formatting)"
|
| 219 |
},
|
|
|
|
| 220 |
"image": {
|
| 221 |
-
"web": "
|
| 222 |
-
"
|
| 223 |
-
"
|
| 224 |
-
"
|
| 225 |
-
"compress": "JPEG (80% quality) or WebP",
|
| 226 |
-
"quality": "PNG or TIFF (lossless)"
|
| 227 |
},
|
|
|
|
| 228 |
"video": {
|
| 229 |
-
"web": "MP4
|
| 230 |
-
"
|
| 231 |
-
"
|
| 232 |
-
"edit": "AVI or MOV (uncompressed)",
|
| 233 |
-
"compress": "MP4 with H.265 codec (smallest)",
|
| 234 |
-
"quality": "MKV or MOV (ProRes)"
|
| 235 |
},
|
|
|
|
| 236 |
"audio": {
|
| 237 |
-
"
|
| 238 |
-
"
|
| 239 |
-
"
|
| 240 |
-
"edit": "WAV or FLAC (lossless)",
|
| 241 |
-
"compress": "MP3 or AAC",
|
| 242 |
-
"quality": "FLAC or WAV (lossless)"
|
| 243 |
}
|
| 244 |
}
|
| 245 |
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
"web": f"π‘ For web, also consider optimizing file size and using responsive formats.",
|
| 256 |
-
"email": f"π‘ Keep file size under 10-20MB for email attachments.",
|
| 257 |
-
"print": f"π‘ Use at least 300 DPI for print quality.",
|
| 258 |
-
"edit": f"π‘ Lossless formats preserve quality for multiple edits.",
|
| 259 |
-
"compress": f"π‘ Balance between quality and file size.",
|
| 260 |
-
"quality": f"π‘ Lossless formats preserve original quality."
|
| 261 |
-
}
|
| 262 |
-
if use_case in tips:
|
| 263 |
-
response += f"{tips[use_case]}\n\n"
|
| 264 |
-
|
| 265 |
-
response += f"π To convert: Select **{recommended.split()[0]}** from the Output Format dropdown."
|
| 266 |
-
return response
|
| 267 |
-
|
| 268 |
-
# Default suggestions
|
| 269 |
-
default_suggestions = {
|
| 270 |
-
"document": "PDF for sharing, DOCX for editing, TXT for simplicity",
|
| 271 |
-
"image": "JPEG for photos, PNG for graphics, WebP for web",
|
| 272 |
-
"video": "MP4 for compatibility, AVI for editing, MKV for storage",
|
| 273 |
-
"audio": "MP3 for general use, WAV for editing, FLAC for quality"
|
| 274 |
}
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
return response
|
| 281 |
|
| 282 |
-
def
|
| 283 |
-
"""Analyze file and provide insights"""
|
| 284 |
-
current_type = context.get("file_type", "").lower()
|
| 285 |
-
current_format = context.get("output_format", "").upper()
|
| 286 |
|
| 287 |
-
|
| 288 |
-
|
| 289 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 290 |
|
| 291 |
-
|
| 292 |
-
info = self.knowledge_base[current_type][current_format.lower()]
|
| 293 |
-
response += f"π Best for: {info['best_for']}\n\n"
|
| 294 |
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
|
|
|
| 301 |
|
| 302 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 303 |
|
| 304 |
-
|
| 305 |
-
"""Get help on specific topics"""
|
| 306 |
-
response = "π€ **How I Can Help You**\n\n"
|
| 307 |
-
response += "I can assist with:\n"
|
| 308 |
-
response += "β
**Convert files** - Say: 'convert this to mp4' or 'make it PDF'\n"
|
| 309 |
-
response += "β
**Suggest formats** - Ask: 'what's the best format for web?'\n"
|
| 310 |
-
response += "β
**Analyze files** - Ask: 'analyze this file' or 'what is this good for?'\n"
|
| 311 |
-
response += "β
**Optimize settings** - Ask: 'how to compress video?'\n\n"
|
| 312 |
-
response += "π¬ **Examples:**\n"
|
| 313 |
-
response += "β’ 'Convert this to PNG'\n"
|
| 314 |
-
response += "β’ 'Best format for email attachment'\n"
|
| 315 |
-
response += "β’ 'How to reduce file size?'\n"
|
| 316 |
-
response += "β’ 'What format should I use for web?'"
|
| 317 |
|
| 318 |
-
|
|
|
|
| 319 |
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
response = "π€ **AI Assistant Ready**\n\n"
|
| 323 |
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
return "You're welcome! π Let me know if you need help with any conversions!"
|
| 327 |
|
| 328 |
-
|
| 329 |
-
|
| 330 |
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
response += "β’ **Recommend formats** based on your needs\n"
|
| 334 |
-
response += "β’ **Analyze files** and suggest improvements\n"
|
| 335 |
-
response += "β’ **Optimize** quality vs file size\n\n"
|
| 336 |
-
response += "π‘ **Quick commands:**\n"
|
| 337 |
-
response += " β 'Convert this to MP3'\n"
|
| 338 |
-
response += " β 'Best format for web'\n"
|
| 339 |
-
response += " β 'How to compress video?'\n"
|
| 340 |
-
response += " β 'Analyze this file'"
|
| 341 |
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
("
|
| 348 |
-
|
| 349 |
-
("image", "webp"): "Modern format with great compression. Supported by all modern browsers.",
|
| 350 |
-
("video", "mp4"): "Most compatible. H.264 codec is universal.",
|
| 351 |
-
("video", "gif"): "Good for short animations. Keep duration under 5 seconds.",
|
| 352 |
-
("audio", "mp3"): "Universal format. 192kbps is good for most uses.",
|
| 353 |
-
("audio", "flac"): "Lossless format. Perfect for archiving.",
|
| 354 |
-
("document", "pdf"): "Preserves formatting across all devices.",
|
| 355 |
-
("document", "txt"): "Plain text - very small but loses all formatting."
|
| 356 |
}
|
| 357 |
|
| 358 |
-
return
|
|
|
|
|
|
|
|
|
|
| 359 |
|
| 360 |
def get_conversion_help(self) -> str:
|
| 361 |
-
|
| 362 |
-
return
|
| 363 |
-
π―
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
β’
|
| 367 |
-
β’
|
| 368 |
-
β’
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
**How to use:**
|
| 372 |
-
1. Select the file type (Document/Image/Video/Audio)
|
| 373 |
-
2. Say what format you want
|
| 374 |
-
3. Click "Ask AI" or press Enter
|
| 375 |
-
4. Then click "Start Conversion"
|
| 376 |
-
|
| 377 |
-
π‘ **Tip:** Be specific about the format you want!
|
| 378 |
-
"""
|
|
|
|
| 1 |
import re
|
|
|
|
|
|
|
| 2 |
from pathlib import Path
|
| 3 |
from typing import Dict, Any, Tuple, Optional
|
| 4 |
|
| 5 |
|
| 6 |
class AIAssistant:
|
| 7 |
+
|
| 8 |
+
SUPPORTED_FORMATS = {
|
| 9 |
+
"document": ["pdf", "docx", "txt", "html", "md"],
|
| 10 |
+
"image": ["jpg", "jpeg", "png", "webp", "bmp", "tiff"],
|
| 11 |
+
"video": ["mp4", "avi", "mkv", "mov", "webm", "gif"],
|
| 12 |
+
"audio": ["mp3", "wav", "ogg", "m4a", "flac", "aac"]
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
def __init__(self):
|
| 16 |
+
|
| 17 |
self.knowledge_base = self.load_knowledge_base()
|
| 18 |
|
| 19 |
def load_knowledge_base(self):
|
| 20 |
+
|
| 21 |
return {
|
| 22 |
+
|
| 23 |
"document": {
|
| 24 |
"pdf": {
|
| 25 |
+
"best_for": "Sharing and printing documents"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
},
|
| 27 |
+
"docx": {
|
| 28 |
+
"best_for": "Editing documents"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
},
|
| 30 |
"txt": {
|
| 31 |
+
"best_for": "Plain text and notes"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
}
|
| 33 |
},
|
| 34 |
+
|
| 35 |
"image": {
|
| 36 |
"jpg": {
|
| 37 |
+
"best_for": "Photos and web images"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
},
|
| 39 |
"png": {
|
| 40 |
+
"best_for": "Transparency and graphics"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
},
|
| 42 |
"webp": {
|
| 43 |
+
"best_for": "Modern web compression"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
},
|
| 46 |
+
|
| 47 |
"video": {
|
| 48 |
"mp4": {
|
| 49 |
+
"best_for": "Universal compatibility"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
},
|
| 51 |
+
"mkv": {
|
| 52 |
+
"best_for": "High quality storage"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
},
|
| 55 |
+
|
| 56 |
"audio": {
|
| 57 |
"mp3": {
|
| 58 |
+
"best_for": "Universal playback"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
},
|
| 60 |
+
"flac": {
|
| 61 |
+
"best_for": "Lossless audio"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
}
|
| 63 |
}
|
| 64 |
}
|
| 65 |
|
| 66 |
+
def ask(
|
| 67 |
+
self,
|
| 68 |
+
question: str,
|
| 69 |
+
context: Dict[str, Any]
|
| 70 |
+
) -> str:
|
| 71 |
+
|
| 72 |
+
if not question.strip():
|
| 73 |
+
|
| 74 |
+
return (
|
| 75 |
+
"π¬ Ask me something like:\n"
|
| 76 |
+
"β’ Convert this to PDF\n"
|
| 77 |
+
"β’ Best format for web\n"
|
| 78 |
+
"β’ Analyze this file"
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
question_lower = question.lower().strip()
|
| 82 |
+
|
| 83 |
+
# Greetings
|
| 84 |
+
if any(
|
| 85 |
+
x in question_lower
|
| 86 |
+
for x in ["hi", "hello", "hey"]
|
| 87 |
+
):
|
| 88 |
+
|
| 89 |
+
return (
|
| 90 |
+
"π Hello! I'm your AI File Assistant.\n\n"
|
| 91 |
+
"Try:\n"
|
| 92 |
+
"β’ Convert this to MP4\n"
|
| 93 |
+
"β’ Best format for web\n"
|
| 94 |
+
"β’ Analyze this file"
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
# Thanks
|
| 98 |
+
if "thank" in question_lower:
|
| 99 |
+
|
| 100 |
+
return (
|
| 101 |
+
"π You're welcome!"
|
| 102 |
+
)
|
| 103 |
+
|
| 104 |
+
# Conversion request
|
| 105 |
+
if self.is_conversion_command(
|
| 106 |
+
question_lower
|
| 107 |
+
):
|
| 108 |
+
|
| 109 |
+
return self.execute_conversion_command(
|
| 110 |
+
question_lower,
|
| 111 |
+
context
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
# Recommendations
|
| 115 |
+
if any(
|
| 116 |
+
x in question_lower
|
| 117 |
+
for x in [
|
| 118 |
+
"best format",
|
| 119 |
+
"recommend",
|
| 120 |
+
"suggest"
|
| 121 |
+
]
|
| 122 |
+
):
|
| 123 |
+
|
| 124 |
+
return self.suggest_format(
|
| 125 |
+
question_lower,
|
| 126 |
+
context
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
# Analyze
|
| 130 |
+
if any(
|
| 131 |
+
x in question_lower
|
| 132 |
+
for x in [
|
| 133 |
+
"analyze",
|
| 134 |
+
"what is this",
|
| 135 |
+
"what format"
|
| 136 |
+
]
|
| 137 |
+
):
|
| 138 |
+
|
| 139 |
+
return self.analyze_file(
|
| 140 |
+
context
|
| 141 |
+
)
|
| 142 |
+
|
| 143 |
+
# Help
|
| 144 |
+
if any(
|
| 145 |
+
x in question_lower
|
| 146 |
+
for x in [
|
| 147 |
+
"help",
|
| 148 |
+
"how to"
|
| 149 |
+
]
|
| 150 |
+
):
|
| 151 |
+
|
| 152 |
+
return self.get_help()
|
| 153 |
+
|
| 154 |
+
return self.get_general_response()
|
| 155 |
+
|
| 156 |
+
def is_conversion_command(
|
| 157 |
+
self,
|
| 158 |
+
text: str
|
| 159 |
+
) -> bool:
|
| 160 |
+
|
| 161 |
+
keywords = [
|
| 162 |
+
"convert",
|
| 163 |
+
"change",
|
| 164 |
+
"transform",
|
| 165 |
+
"make",
|
| 166 |
+
"turn into"
|
| 167 |
+
]
|
| 168 |
|
| 169 |
+
return any(
|
| 170 |
+
k in text
|
| 171 |
+
for k in keywords
|
| 172 |
+
)
|
| 173 |
|
| 174 |
+
def parse_conversion_request(
|
| 175 |
+
self,
|
| 176 |
+
text: str
|
| 177 |
+
) -> Optional[str]:
|
| 178 |
|
| 179 |
+
patterns = [
|
|
|
|
|
|
|
| 180 |
|
| 181 |
+
r"to\s+(\w+)",
|
|
|
|
|
|
|
| 182 |
|
| 183 |
+
r"into\s+(\w+)",
|
|
|
|
|
|
|
| 184 |
|
| 185 |
+
r"make\s+(?:it\s+)?(\w+)",
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
r"convert\s+(?:this\s+)?(?:file\s+)?to\s+(\w+)"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 188 |
]
|
| 189 |
|
| 190 |
for pattern in patterns:
|
| 191 |
+
|
| 192 |
+
match = re.search(
|
| 193 |
+
pattern,
|
| 194 |
+
text
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
if match:
|
|
|
|
|
|
|
| 198 |
|
| 199 |
+
fmt = match.group(1).lower()
|
| 200 |
+
|
| 201 |
+
# normalize jpeg
|
| 202 |
+
if fmt == "jpeg":
|
| 203 |
+
fmt = "jpg"
|
| 204 |
|
| 205 |
+
return fmt
|
| 206 |
+
|
| 207 |
+
return None
|
| 208 |
+
|
| 209 |
+
def execute_conversion_command(
|
| 210 |
+
self,
|
| 211 |
+
question: str,
|
| 212 |
+
context: Dict[str, Any]
|
| 213 |
+
) -> str:
|
| 214 |
+
|
| 215 |
+
target_format = self.parse_conversion_request(
|
| 216 |
+
question
|
| 217 |
+
)
|
| 218 |
|
| 219 |
if not target_format:
|
|
|
|
| 220 |
|
| 221 |
+
return self.get_conversion_help()
|
|
|
|
|
|
|
| 222 |
|
| 223 |
+
file_type = context.get(
|
| 224 |
+
"file_type",
|
| 225 |
+
""
|
| 226 |
+
).lower()
|
| 227 |
+
|
| 228 |
+
current_format = context.get(
|
| 229 |
+
"output_format",
|
| 230 |
+
""
|
| 231 |
+
).lower()
|
| 232 |
+
|
| 233 |
+
if not file_type:
|
| 234 |
+
|
| 235 |
+
return (
|
| 236 |
+
"β Please select a file type first."
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
supported = self.SUPPORTED_FORMATS.get(
|
| 240 |
+
file_type,
|
| 241 |
+
[]
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
if target_format not in supported:
|
| 245 |
+
|
| 246 |
+
return (
|
| 247 |
+
f"β {target_format.upper()} "
|
| 248 |
+
f"is not supported for "
|
| 249 |
+
f"{file_type} files.\n\n"
|
| 250 |
+
f"Supported:\n"
|
| 251 |
+
f"{', '.join(supported).upper()}"
|
| 252 |
+
)
|
| 253 |
+
|
| 254 |
+
response = (
|
| 255 |
+
"π― Conversion Ready\n\n"
|
| 256 |
+
f"Current Type: {file_type.title()}\n"
|
| 257 |
+
f"Target Format: {target_format.upper()}\n\n"
|
| 258 |
+
)
|
| 259 |
+
|
| 260 |
+
advice = self.get_format_advice(
|
| 261 |
+
file_type,
|
| 262 |
+
target_format
|
| 263 |
+
)
|
| 264 |
+
|
| 265 |
+
if advice:
|
| 266 |
+
|
| 267 |
+
response += (
|
| 268 |
+
f"π‘ Tip: {advice}\n\n"
|
| 269 |
+
)
|
| 270 |
+
|
| 271 |
+
response += (
|
| 272 |
+
"β
Now:\n"
|
| 273 |
+
"1. Select the format\n"
|
| 274 |
+
"2. Add files\n"
|
| 275 |
+
"3. Click Start Conversion"
|
| 276 |
+
)
|
| 277 |
|
| 278 |
+
return response
|
|
|
|
| 279 |
|
| 280 |
+
def suggest_format(
|
| 281 |
+
self,
|
| 282 |
+
question: str,
|
| 283 |
+
context: Dict[str, Any]
|
| 284 |
+
) -> str:
|
|
|
|
|
|
|
| 285 |
|
| 286 |
+
file_type = context.get(
|
| 287 |
+
"file_type",
|
| 288 |
+
""
|
| 289 |
+
).lower()
|
| 290 |
|
| 291 |
+
if not file_type:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
+
return (
|
| 294 |
+
"β Please select a file type first."
|
| 295 |
+
)
|
|
|
|
| 296 |
|
| 297 |
+
recommendations = {
|
| 298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
"document": {
|
| 300 |
+
"web": "HTML",
|
| 301 |
+
"print": "PDF",
|
| 302 |
+
"edit": "DOCX",
|
| 303 |
+
"small": "TXT"
|
|
|
|
|
|
|
| 304 |
},
|
| 305 |
+
|
| 306 |
"image": {
|
| 307 |
+
"web": "WEBP",
|
| 308 |
+
"quality": "PNG",
|
| 309 |
+
"photo": "JPG",
|
| 310 |
+
"transparent": "PNG"
|
|
|
|
|
|
|
| 311 |
},
|
| 312 |
+
|
| 313 |
"video": {
|
| 314 |
+
"web": "MP4",
|
| 315 |
+
"quality": "MKV",
|
| 316 |
+
"small": "WEBM"
|
|
|
|
|
|
|
|
|
|
| 317 |
},
|
| 318 |
+
|
| 319 |
"audio": {
|
| 320 |
+
"quality": "FLAC",
|
| 321 |
+
"small": "MP3",
|
| 322 |
+
"edit": "WAV"
|
|
|
|
|
|
|
|
|
|
| 323 |
}
|
| 324 |
}
|
| 325 |
|
| 326 |
+
use_case = "web"
|
| 327 |
+
|
| 328 |
+
keywords = {
|
| 329 |
+
"print": ["print"],
|
| 330 |
+
"edit": ["edit"],
|
| 331 |
+
"small": ["small", "compress"],
|
| 332 |
+
"quality": ["quality", "lossless"],
|
| 333 |
+
"photo": ["photo"],
|
| 334 |
+
"transparent": ["transparent"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
}
|
| 336 |
|
| 337 |
+
for case, words in keywords.items():
|
| 338 |
+
|
| 339 |
+
if any(w in question for w in words):
|
| 340 |
+
|
| 341 |
+
use_case = case
|
| 342 |
+
break
|
| 343 |
+
|
| 344 |
+
fmt = recommendations.get(
|
| 345 |
+
file_type,
|
| 346 |
+
{}
|
| 347 |
+
).get(
|
| 348 |
+
use_case,
|
| 349 |
+
"PDF"
|
| 350 |
+
)
|
| 351 |
+
|
| 352 |
+
return (
|
| 353 |
+
f"π Recommended Format\n\n"
|
| 354 |
+
f"For {use_case} use:\n"
|
| 355 |
+
f"β
{fmt}\n\n"
|
| 356 |
+
f"Type:\n"
|
| 357 |
+
f"Convert this to {fmt}"
|
| 358 |
+
)
|
| 359 |
+
|
| 360 |
+
def analyze_file(
|
| 361 |
+
self,
|
| 362 |
+
context: Dict[str, Any]
|
| 363 |
+
) -> str:
|
| 364 |
+
|
| 365 |
+
file_type = context.get(
|
| 366 |
+
"file_type",
|
| 367 |
+
"Unknown"
|
| 368 |
+
)
|
| 369 |
+
|
| 370 |
+
fmt = context.get(
|
| 371 |
+
"output_format",
|
| 372 |
+
"Unknown"
|
| 373 |
+
).lower()
|
| 374 |
+
|
| 375 |
+
response = (
|
| 376 |
+
"π File Analysis\n\n"
|
| 377 |
+
f"Type: {file_type.title()}\n"
|
| 378 |
+
f"Format: {fmt.upper()}\n\n"
|
| 379 |
+
)
|
| 380 |
+
|
| 381 |
+
kb = self.knowledge_base.get(
|
| 382 |
+
file_type,
|
| 383 |
+
{}
|
| 384 |
+
)
|
| 385 |
+
|
| 386 |
+
if fmt in kb:
|
| 387 |
+
|
| 388 |
+
response += (
|
| 389 |
+
f"π Best For:\n"
|
| 390 |
+
f"{kb[fmt]['best_for']}"
|
| 391 |
+
)
|
| 392 |
+
|
| 393 |
+
else:
|
| 394 |
+
|
| 395 |
+
response += (
|
| 396 |
+
"No detailed analysis available."
|
| 397 |
+
)
|
| 398 |
|
| 399 |
return response
|
| 400 |
|
| 401 |
+
def get_help(self) -> str:
|
|
|
|
|
|
|
|
|
|
| 402 |
|
| 403 |
+
return (
|
| 404 |
+
"π€ AI Assistant Help\n\n"
|
| 405 |
+
"Commands:\n"
|
| 406 |
+
"β’ Convert this to PDF\n"
|
| 407 |
+
"β’ Best format for web\n"
|
| 408 |
+
"β’ Analyze this file\n"
|
| 409 |
+
"β’ Compress this video\n\n"
|
| 410 |
+
"Supported:\n"
|
| 411 |
+
"Documents, Images, Videos, Audio"
|
| 412 |
+
)
|
| 413 |
|
| 414 |
+
def get_general_response(self) -> str:
|
|
|
|
|
|
|
| 415 |
|
| 416 |
+
return (
|
| 417 |
+
"π€ AI Assistant Ready\n\n"
|
| 418 |
+
"Try:\n"
|
| 419 |
+
"β’ Convert this to PNG\n"
|
| 420 |
+
"β’ Best format for web\n"
|
| 421 |
+
"β’ Analyze this file"
|
| 422 |
+
)
|
| 423 |
|
| 424 |
+
def get_format_advice(
|
| 425 |
+
self,
|
| 426 |
+
file_type: str,
|
| 427 |
+
target_format: str
|
| 428 |
+
) -> str:
|
| 429 |
|
| 430 |
+
tips = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 431 |
|
| 432 |
+
("image", "jpg"):
|
| 433 |
+
"Best for photos and smaller size.",
|
| 434 |
|
| 435 |
+
("image", "png"):
|
| 436 |
+
"Supports transparency.",
|
|
|
|
| 437 |
|
| 438 |
+
("image", "webp"):
|
| 439 |
+
"Excellent modern compression.",
|
|
|
|
| 440 |
|
| 441 |
+
("video", "mp4"):
|
| 442 |
+
"Most compatible format.",
|
| 443 |
|
| 444 |
+
("video", "gif"):
|
| 445 |
+
"Keep clips short for smaller size.",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 446 |
|
| 447 |
+
("audio", "mp3"):
|
| 448 |
+
"Universal audio playback.",
|
| 449 |
+
|
| 450 |
+
("audio", "flac"):
|
| 451 |
+
"Lossless quality.",
|
| 452 |
|
| 453 |
+
("document", "pdf"):
|
| 454 |
+
"Preserves formatting.",
|
| 455 |
+
|
| 456 |
+
("document", "txt"):
|
| 457 |
+
"Very small file size."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
}
|
| 459 |
|
| 460 |
+
return tips.get(
|
| 461 |
+
(file_type, target_format),
|
| 462 |
+
""
|
| 463 |
+
)
|
| 464 |
|
| 465 |
def get_conversion_help(self) -> str:
|
| 466 |
+
|
| 467 |
+
return (
|
| 468 |
+
"π― Conversion Help\n\n"
|
| 469 |
+
"Examples:\n"
|
| 470 |
+
"β’ Convert this to PDF\n"
|
| 471 |
+
"β’ Make it MP3\n"
|
| 472 |
+
"β’ Change this to PNG\n"
|
| 473 |
+
"β’ Transform to MP4"
|
| 474 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|