embedingHF commited on
Commit
42a8353
Β·
verified Β·
1 Parent(s): a9eb679

Update ai/ai_assistant.py

Browse files
Files changed (1) hide show
  1. 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": "Official documents, forms, print-ready files",
17
- "suggested_formats": {
18
- "word": "For editing documents",
19
- "txt": "For text extraction",
20
- "html": "For web display"
21
- }
22
  },
23
- "word": {
24
- "best_for": "Editable documents, reports, letters",
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": "Simple text, code, notes",
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": "Photographs, web images",
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": "Graphics, logos, screenshots with transparency",
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": "Web images, modern browsers",
59
- "suggested_formats": {
60
- "png": "For editing",
61
- "jpg": "For compatibility"
62
- }
63
  }
64
  },
 
65
  "video": {
66
  "mp4": {
67
- "best_for": "Universal compatibility, web, social media",
68
- "suggested_formats": {
69
- "gif": "For short animations",
70
- "avi": "For editing",
71
- "mkv": "For high quality storage"
72
- }
73
  },
74
- "avi": {
75
- "best_for": "Editing, Windows compatibility",
76
- "suggested_formats": {
77
- "mp4": "For web sharing",
78
- "mkv": "For compression"
79
- }
80
  }
81
  },
 
82
  "audio": {
83
  "mp3": {
84
- "best_for": "Music, podcasts, universal playback",
85
- "suggested_formats": {
86
- "wav": "For editing",
87
- "flac": "For lossless quality",
88
- "ogg": "For open source projects"
89
- }
90
  },
91
- "wav": {
92
- "best_for": "Professional audio, editing",
93
- "suggested_formats": {
94
- "mp3": "For smaller size",
95
- "flac": "For lossless compression"
96
- }
97
  }
98
  }
99
  }
100
 
101
- def ask(self, question: str, context: Dict[str, Any]) -> str:
102
- """Process AI queries and provide intelligent responses"""
103
- question_lower = question.lower()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
- # Check for conversion commands
106
- if self.is_conversion_command(question_lower):
107
- return self.execute_conversion_command(question, context)
 
108
 
109
- # Check for format suggestions
110
- elif "suggest" in question_lower or "recommend" in question_lower or "best format" in question_lower:
111
- return self.suggest_format(question_lower, context)
 
112
 
113
- # Check for file analysis
114
- elif "what is this" in question_lower or "analyze" in question_lower:
115
- return self.analyze_file(context)
116
 
117
- # Check for general help
118
- elif "help" in question_lower or "how to" in question_lower:
119
- return self.get_help(question_lower, context)
120
 
121
- # Default response
122
- else:
123
- return self.get_general_response(question_lower, context)
124
 
125
- def is_conversion_command(self, text: str) -> bool:
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
- def parse_conversion_request(self, text: str, context: Dict[str, Any]) -> Tuple[Optional[str], Optional[str]]:
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
- match = re.search(pattern, text.lower())
 
 
 
 
 
141
  if match:
142
- target_format = match.group(1)
143
- return target_format, None
144
 
145
- return None, None
 
 
 
 
146
 
147
- def execute_conversion_command(self, question: str, context: Dict[str, Any]) -> str:
148
- """Execute a conversion command"""
149
- target_format, _ = self.parse_conversion_request(question, context)
 
 
 
 
 
 
 
 
 
 
150
 
151
  if not target_format:
152
- return self.get_conversion_help()
153
 
154
- # Get current context
155
- current_type = context.get("file_type", "").lower()
156
- current_format = context.get("output_format", "").lower()
157
 
158
- if not current_type or not current_format:
159
- return "⚠️ Please select a file type and output format first in the settings panel."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
- response = f"🎯 **Conversion Command Received**\n\n"
162
- response += f"Converting from **{current_format.upper()}** to **{target_format.upper()}**...\n\n"
163
 
164
- # Check if format is supported
165
- supported_formats = {
166
- "document": ["pdf", "docx", "txt", "html", "md"],
167
- "image": ["jpg", "png", "webp", "bmp", "tiff"],
168
- "video": ["mp4", "avi", "mkv", "mov", "webm", "gif"],
169
- "audio": ["mp3", "wav", "ogg", "m4a", "flac"]
170
- }
171
 
172
- if current_type in supported_formats and target_format in supported_formats[current_type]:
173
- response += f"βœ… **{target_format.upper()}** is supported!\n\n"
174
- response += f"πŸ’‘ **Tip**: Make sure you've added files to convert, then click 'Start Conversion'\n\n"
 
175
 
176
- # Add format-specific advice
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
- response += "πŸ”„ **Next Steps**:\n"
185
- response += "1. Select the target format from the dropdown\n"
186
- response += "2. Add files to convert\n"
187
- response += "3. Click 'Start Conversion'"
188
 
189
- return response
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 or PDF (for consistent formatting)",
214
- "email": "PDF (preserves formatting) or TXT (small size)",
215
- "print": "PDF (best for printing)",
216
- "edit": "DOCX (Microsoft Word) or ODT (OpenOffice)",
217
- "compress": "TXT (smallest) or compressed PDF",
218
- "quality": "PDF (preserves all formatting)"
219
  },
 
220
  "image": {
221
- "web": "WebP (best compression) or JPEG (good quality)",
222
- "email": "JPEG (small size) or PNG (if transparency needed)",
223
- "print": "TIFF (high quality) or PNG",
224
- "edit": "PNG (lossless) or PSD",
225
- "compress": "JPEG (80% quality) or WebP",
226
- "quality": "PNG or TIFF (lossless)"
227
  },
 
228
  "video": {
229
- "web": "MP4 with H.264 codec (best compatibility)",
230
- "email": "GIF (short clips) or small MP4",
231
- "print": "Not applicable for video",
232
- "edit": "AVI or MOV (uncompressed)",
233
- "compress": "MP4 with H.265 codec (smallest)",
234
- "quality": "MKV or MOV (ProRes)"
235
  },
 
236
  "audio": {
237
- "web": "MP3 (128-192kbps) or OGG",
238
- "email": "MP3 (low bitrate)",
239
- "print": "Not applicable for audio",
240
- "edit": "WAV or FLAC (lossless)",
241
- "compress": "MP3 or AAC",
242
- "quality": "FLAC or WAV (lossless)"
243
  }
244
  }
245
 
246
- if current_type in suggestions:
247
- if use_case in suggestions[current_type]:
248
- recommended = suggestions[current_type][use_case]
249
- response = f"πŸ“‹ **Format Recommendation**\n\n"
250
- response += f"Based on your request to **{use_case}** a {current_type}:\n"
251
- response += f"βœ… Recommended format: **{recommended}**\n\n"
252
-
253
- # Add additional tips
254
- tips = {
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
- response = f"πŸ’‘ **Format Suggestions for {current_type.title()}s:**\n\n"
277
- response += f"{default_suggestions.get(current_type, 'Choose based on your needs')}\n\n"
278
- response += f"Tell me more about your use case (web, print, edit, compress) for better recommendations!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
279
 
280
  return response
281
 
282
- def analyze_file(self, context: Dict[str, Any]) -> str:
283
- """Analyze file and provide insights"""
284
- current_type = context.get("file_type", "").lower()
285
- current_format = context.get("output_format", "").upper()
286
 
287
- response = f"πŸ” **File Analysis**\n\n"
288
- response += f"πŸ“„ Type: {current_type.title()}\n"
289
- response += f"🎯 Current format: {current_format}\n"
 
 
 
 
 
 
 
290
 
291
- if current_type in self.knowledge_base and current_format.lower() in self.knowledge_base[current_type]:
292
- info = self.knowledge_base[current_type][current_format.lower()]
293
- response += f"πŸ“Œ Best for: {info['best_for']}\n\n"
294
 
295
- if "suggested_formats" in info:
296
- response += f"πŸ”„ **Suggested conversions:**\n"
297
- for fmt, reason in info['suggested_formats'].items():
298
- response += f" β€’ {fmt.upper()} - {reason}\n"
299
- else:
300
- response += f"\nπŸ’‘ Try converting to a different format for better results!\n"
 
301
 
302
- return response
 
 
 
 
303
 
304
- def get_help(self, question: str, context: Dict[str, Any]) -> str:
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
- return response
 
319
 
320
- def get_general_response(self, question: str, context: Dict[str, Any]) -> str:
321
- """General response when no specific command is detected"""
322
- response = "πŸ€– **AI Assistant Ready**\n\n"
323
 
324
- # Try to understand intent
325
- if "thank" in question:
326
- return "You're welcome! 😊 Let me know if you need help with any conversions!"
327
 
328
- if "hello" in question or "hi" in question:
329
- return "Hello! πŸ‘‹ I'm your AI conversion assistant. How can I help you today?\n\nTry asking me to 'convert this to PDF' or 'suggest best format for web'!"
330
 
331
- response += "I can help you:\n"
332
- response += "β€’ **Convert files** using natural language\n"
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
- return response
 
 
 
 
343
 
344
- def get_format_advice(self, file_type: str, target_format: str) -> str:
345
- """Get specific advice for format conversion"""
346
- advice = {
347
- ("image", "jpg"): "Good for photos. Use quality 85-90 for best balance.",
348
- ("image", "png"): "Best for graphics with transparency. Larger file size.",
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 advice.get((file_type, target_format), "")
 
 
 
359
 
360
  def get_conversion_help(self) -> str:
361
- """Help for conversion commands"""
362
- return """
363
- 🎯 **Conversion Commands**
364
-
365
- Try these examples:
366
- β€’ "**Convert this to PDF**" - Changes output format to PDF
367
- β€’ "**Make it MP3**" - Changes to audio format
368
- β€’ "**Transform to PNG**" - Changes image format
369
- β€’ "**Change this to AVI**" - Changes video format
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
+ )