lattifai 0.2.4__py3-none-any.whl → 0.4.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
lattifai/__init__.py CHANGED
@@ -2,7 +2,21 @@ import os
2
2
  import sys
3
3
  import warnings
4
4
 
5
- from .base_client import LattifAIError
5
+ from .errors import (
6
+ AlignmentError,
7
+ APIError,
8
+ AudioFormatError,
9
+ AudioLoadError,
10
+ AudioProcessingError,
11
+ ConfigurationError,
12
+ DependencyError,
13
+ LatticeDecodingError,
14
+ LatticeEncodingError,
15
+ LattifAIError,
16
+ ModelLoadError,
17
+ SubtitleParseError,
18
+ SubtitleProcessingError,
19
+ )
6
20
  from .io import SubtitleIO
7
21
 
8
22
  try:
@@ -47,12 +61,29 @@ def __getattr__(name):
47
61
  from .client import LattifAI
48
62
 
49
63
  return LattifAI
64
+ if name == 'AsyncLattifAI':
65
+ from .client import AsyncLattifAI
66
+
67
+ return AsyncLattifAI
50
68
  raise AttributeError(f"module '{__name__}' has no attribute '{name}'")
51
69
 
52
70
 
53
71
  __all__ = [
54
72
  'LattifAI', # noqa: F822
73
+ 'AsyncLattifAI', # noqa: F822
55
74
  'LattifAIError',
75
+ 'AudioProcessingError',
76
+ 'AudioLoadError',
77
+ 'AudioFormatError',
78
+ 'SubtitleProcessingError',
79
+ 'SubtitleParseError',
80
+ 'AlignmentError',
81
+ 'LatticeEncodingError',
82
+ 'LatticeDecodingError',
83
+ 'ModelLoadError',
84
+ 'DependencyError',
85
+ 'APIError',
86
+ 'ConfigurationError',
56
87
  'SubtitleIO',
57
88
  '__version__',
58
89
  ]
lattifai/base_client.py CHANGED
@@ -6,11 +6,8 @@ from typing import Any, Awaitable, Callable, Dict, Optional, Union # noqa: F401
6
6
 
7
7
  import httpx
8
8
 
9
-
10
- class LattifAIError(Exception):
11
- """Base exception for LattifAI errors."""
12
-
13
- pass
9
+ # Import from errors module for consistency
10
+ from .errors import APIError, ConfigurationError, LattifAIError
14
11
 
15
12
 
16
13
  class BaseAPIClient(ABC):
@@ -28,7 +25,7 @@ class BaseAPIClient(ABC):
28
25
  if api_key is None:
29
26
  api_key = os.environ.get('LATTIFAI_API_KEY')
30
27
  if api_key is None:
31
- raise LattifAIError(
28
+ raise ConfigurationError(
32
29
  'The api_key client option must be set either by passing api_key to the client '
33
30
  'or by setting the LATTIFAI_API_KEY environment variable'
34
31
  )
@@ -116,3 +113,14 @@ class AsyncAPIClient(BaseAPIClient):
116
113
  ) -> httpx.Response:
117
114
  """Make an HTTP request."""
118
115
  return await self._client.request(method=method, url=url, json=json, files=files, **kwargs)
116
+
117
+ async def post(
118
+ self,
119
+ api_endpoint: str,
120
+ *,
121
+ json: Optional[Dict[str, Any]] = None,
122
+ files: Optional[Dict[str, Any]] = None,
123
+ **kwargs,
124
+ ) -> httpx.Response:
125
+ """Make a POST request to the specified API endpoint."""
126
+ return await self._request('POST', api_endpoint, json=json, files=files, **kwargs)
lattifai/bin/__init__.py CHANGED
@@ -1,2 +1,3 @@
1
+ from .agent import * # noqa
1
2
  from .align import * # noqa
2
3
  from .subtitle import * # noqa
lattifai/bin/agent.py ADDED
@@ -0,0 +1,325 @@
1
+ """
2
+ Agent command for YouTube workflow
3
+ """
4
+
5
+ import asyncio
6
+ import os
7
+ import sys
8
+ from typing import List, Optional
9
+
10
+ import click
11
+ import colorful
12
+ from lhotse.utils import Pathlike
13
+
14
+ from lattifai.bin.cli_base import cli
15
+ from lattifai.io import OUTPUT_SUBTITLE_FORMATS
16
+
17
+
18
+ @cli.command()
19
+ @click.option('--youtube', '--yt', is_flag=True, help='Process YouTube URL through agentic workflow.')
20
+ @click.option(
21
+ '--api-key',
22
+ '--api_key',
23
+ type=str,
24
+ help='LattifAI API key for alignment (overrides LATTIFAI_API_KEY env var).',
25
+ )
26
+ @click.option(
27
+ '--gemini-api-key',
28
+ '--gemini_api_key',
29
+ type=str,
30
+ help='Gemini API key for transcription (overrides GEMINI_API_KEY env var).',
31
+ )
32
+ @click.option(
33
+ '-D',
34
+ '--device',
35
+ type=click.Choice(['cpu', 'cuda', 'mps'], case_sensitive=False),
36
+ default='cpu',
37
+ help='Device to use for inference.',
38
+ )
39
+ @click.option(
40
+ '-M',
41
+ '--model-name-or-path',
42
+ '--model_name_or_path',
43
+ type=str,
44
+ default='Lattifai/Lattice-1-Alpha',
45
+ help='Model name or path for alignment.',
46
+ )
47
+ @click.option(
48
+ '--media-format',
49
+ '--media_format',
50
+ type=click.Choice(
51
+ ['mp3', 'wav', 'm4a', 'aac', 'opus', 'mp4', 'webm', 'mkv', 'avi', 'mov', 'flv', 'wmv', 'mpeg', 'mpg', '3gp'],
52
+ case_sensitive=False,
53
+ ),
54
+ default='mp4',
55
+ help='Media format for YouTube download (audio or video).',
56
+ )
57
+ @click.option(
58
+ '--output-format',
59
+ '--output_format',
60
+ type=click.Choice(OUTPUT_SUBTITLE_FORMATS, case_sensitive=False),
61
+ default='srt',
62
+ help='Subtitle output format.',
63
+ )
64
+ @click.option(
65
+ '--output-dir',
66
+ '--output_dir',
67
+ type=click.Path(exists=False, file_okay=False, dir_okay=True),
68
+ help='Output directory for generated files (default: current directory).',
69
+ )
70
+ @click.option(
71
+ '--max-retries',
72
+ '--max_retries',
73
+ type=int,
74
+ default=0,
75
+ help='Maximum number of retries for failed steps.',
76
+ )
77
+ @click.option(
78
+ '-S',
79
+ '--split-sentence',
80
+ '--split_sentence',
81
+ is_flag=True,
82
+ default=False,
83
+ help='Re-segment subtitles by semantics.',
84
+ )
85
+ @click.option(
86
+ '--word-level',
87
+ '--word_level',
88
+ is_flag=True,
89
+ default=False,
90
+ help='Include word-level alignment timestamps in output (for JSON, TextGrid, and subtitle formats).',
91
+ )
92
+ @click.option('--verbose', '-v', is_flag=True, help='Enable verbose logging.')
93
+ @click.option('--force', '-f', is_flag=True, help='Force overwrite existing files without confirmation.')
94
+ @click.argument('url', type=str, required=True)
95
+ def agent(
96
+ youtube: bool,
97
+ url: str,
98
+ api_key: Optional[str] = None,
99
+ gemini_api_key: Optional[str] = None,
100
+ device: str = 'cpu',
101
+ model_name_or_path: str = 'Lattifai/Lattice-1-Alpha',
102
+ media_format: str = 'mp4',
103
+ output_format: str = 'srt',
104
+ output_dir: Optional[str] = None,
105
+ max_retries: int = 0,
106
+ split_sentence: bool = False,
107
+ word_level: bool = False,
108
+ verbose: bool = False,
109
+ force: bool = False,
110
+ ):
111
+ """
112
+ LattifAI Agentic Workflow Agent
113
+
114
+ Process multimedia content through intelligent agent-based pipelines.
115
+
116
+ Example:
117
+ lattifai agent --youtube https://www.youtube.com/watch?v=example
118
+ """
119
+
120
+ if not youtube:
121
+ click.echo(colorful.red('❌ Please specify a workflow type. Use --youtube for YouTube processing.'))
122
+ return
123
+
124
+ # Setup logging
125
+ import logging
126
+
127
+ log_level = logging.DEBUG if verbose else logging.INFO
128
+ logging.basicConfig(level=log_level, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
129
+
130
+ # Set default output directory
131
+ if not output_dir:
132
+ output_dir = os.getcwd()
133
+
134
+ # Get API keys
135
+ lattifai_api_key = api_key or os.getenv('LATTIFAI_API_KEY')
136
+ gemini_key = gemini_api_key or os.getenv('GEMINI_API_KEY')
137
+
138
+ if not gemini_key:
139
+ click.echo(
140
+ colorful.red(
141
+ '❌ Gemini API key is required. Set GEMINI_API_KEY environment variable or use --gemini-api-key option.'
142
+ )
143
+ )
144
+ return
145
+
146
+ try:
147
+ # Run the YouTube workflow
148
+ asyncio.run(
149
+ _run_youtube_workflow(
150
+ url=url,
151
+ lattifai_api_key=lattifai_api_key,
152
+ gemini_api_key=gemini_key,
153
+ device=device,
154
+ model_name_or_path=model_name_or_path,
155
+ media_format=media_format,
156
+ output_format=output_format,
157
+ output_dir=output_dir,
158
+ max_retries=max_retries,
159
+ split_sentence=split_sentence,
160
+ word_level=word_level,
161
+ force_overwrite=force,
162
+ )
163
+ )
164
+
165
+ except KeyboardInterrupt:
166
+ click.echo(colorful.yellow('\n⚠️ Process interrupted by user'))
167
+ sys.exit(1)
168
+ except Exception as e:
169
+ from lattifai.errors import LattifAIError
170
+
171
+ # Extract error message without support info (to avoid duplication)
172
+ if isinstance(e, LattifAIError):
173
+ # Use the get_message() method which includes proper formatting
174
+ click.echo(colorful.red('❌ Workflow failed:'))
175
+ click.echo(e.get_message())
176
+ # Show support info once at the end
177
+ click.echo(e.get_support_info())
178
+ else:
179
+ click.echo(colorful.red(f'❌ Workflow failed: {str(e)}'))
180
+
181
+ if verbose:
182
+ import traceback
183
+
184
+ traceback.print_exc()
185
+ sys.exit(1)
186
+
187
+
188
+ async def _run_youtube_workflow(
189
+ url: str,
190
+ lattifai_api_key: Optional[str],
191
+ gemini_api_key: str,
192
+ device: str,
193
+ model_name_or_path: str,
194
+ media_format: str,
195
+ output_format: str,
196
+ output_dir: str,
197
+ max_retries: int,
198
+ split_sentence: bool = False,
199
+ word_level: bool = False,
200
+ force_overwrite: bool = False,
201
+ ):
202
+ """Run the YouTube processing workflow"""
203
+
204
+ click.echo(colorful.cyan('🚀 LattifAI Agentic Workflow - YouTube Processing'))
205
+ click.echo(f'📺 YouTube URL: {url}')
206
+ click.echo(f'🎬 Media format: {media_format}')
207
+ click.echo(f'📝 Output format: {output_format}')
208
+ click.echo(f'📁 Output directory: {output_dir}')
209
+ click.echo(f'🔄 Max retries: {max_retries}')
210
+ click.echo()
211
+
212
+ # Import workflow components
213
+ from lattifai import AsyncLattifAI
214
+ from lattifai.workflows import YouTubeSubtitleAgent
215
+ from lattifai.workflows.gemini import GeminiTranscriber
216
+ from lattifai.workflows.youtube import YouTubeDownloader
217
+
218
+ # Initialize components with their configuration (only persistent config, not runtime params)
219
+ downloader = YouTubeDownloader()
220
+ transcriber = GeminiTranscriber(api_key=gemini_api_key)
221
+ aligner = AsyncLattifAI(model_name_or_path=model_name_or_path, device=device, api_key=lattifai_api_key)
222
+
223
+ # Initialize agent with components
224
+ agent = YouTubeSubtitleAgent(
225
+ downloader=downloader,
226
+ transcriber=transcriber,
227
+ aligner=aligner,
228
+ max_retries=max_retries,
229
+ )
230
+
231
+ # Process the URL
232
+ result = await agent.process_youtube_url(
233
+ url=url,
234
+ output_dir=output_dir,
235
+ media_format=media_format,
236
+ force_overwrite=force_overwrite,
237
+ output_format=output_format,
238
+ split_sentence=split_sentence,
239
+ word_level=word_level,
240
+ )
241
+
242
+ # Display results
243
+ click.echo(colorful.bold_white_on_green('🎉 Workflow completed successfully!'))
244
+ click.echo()
245
+ click.echo(colorful.bold_white_on_green('📊 Results:'))
246
+
247
+ # Show metadata
248
+ metadata = result.get('metadata', {})
249
+ if metadata:
250
+ click.echo(f'🎬 Title: {metadata.get("title", "Unknown")}')
251
+ click.echo(f'👤 Uploader: {metadata.get("uploader", "Unknown").strip()}')
252
+ click.echo(f'⏱️ Duration: {metadata.get("duration", 0)} seconds')
253
+ click.echo()
254
+
255
+ # Show exported files
256
+ exported_files = result.get('exported_files', {})
257
+ if exported_files:
258
+ click.echo(colorful.bold_white_on_green('📄 Generated subtitle files:'))
259
+ for format_name, file_path in exported_files.items():
260
+ click.echo(f' {format_name.upper()}: {file_path}')
261
+ click.echo()
262
+
263
+ # Show subtitle count
264
+ subtitle_count = result.get('subtitle_count', 0)
265
+ click.echo(f'📝 Generated {subtitle_count} subtitle segments')
266
+
267
+ click.echo(colorful.bold_white_on_green('✨ All done! Your aligned subtitles are ready.'))
268
+
269
+
270
+ # Add dependencies check
271
+ def check_dependencies():
272
+ """Check if required dependencies are installed"""
273
+ missing_deps = []
274
+
275
+ try:
276
+ from google import genai
277
+ except ImportError:
278
+ missing_deps.append('google-genai')
279
+
280
+ try:
281
+ import yt_dlp
282
+ except ImportError:
283
+ missing_deps.append('yt-dlp')
284
+
285
+ try:
286
+ from dotenv import load_dotenv
287
+ except ImportError:
288
+ missing_deps.append('python-dotenv')
289
+
290
+ if missing_deps:
291
+ click.echo(colorful.red('❌ Missing required dependencies:'))
292
+ for dep in missing_deps:
293
+ click.echo(f' - {dep}')
294
+ click.echo()
295
+ click.echo('Install them with:')
296
+ click.echo(f' pip install {" ".join(missing_deps)}')
297
+ return False
298
+
299
+ return True
300
+
301
+
302
+ # Check dependencies when module is imported
303
+ if not check_dependencies():
304
+ pass # Don't exit on import, let the command handle it
305
+
306
+
307
+ if __name__ == '__main__':
308
+ import os
309
+
310
+ asyncio.run(
311
+ _run_youtube_workflow(
312
+ url='https://www.youtube.com/watch?v=DQacCB9tDaw',
313
+ lattifai_api_key=os.getenv('LATTIFAI_API_KEY'),
314
+ gemini_api_key=os.getenv('GEMINI_API_KEY', ''),
315
+ device='cpu',
316
+ model_name_or_path='Lattifai/Lattice-1-Alpha',
317
+ media_format='mp4',
318
+ output_format='TextGrid',
319
+ output_dir='~/Downloads/lattifai_youtube',
320
+ max_retries=0,
321
+ split_sentence=True,
322
+ word_level=False,
323
+ force_overwrite=False,
324
+ )
325
+ )