@weirdfingers/baseboards 0.2.1 → 0.4.0

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.
Files changed (56) hide show
  1. package/README.md +14 -4
  2. package/dist/index.js +13 -4
  3. package/dist/index.js.map +1 -1
  4. package/package.json +1 -1
  5. package/templates/api/ARTIFACT_RESOLUTION_GUIDE.md +148 -0
  6. package/templates/api/Dockerfile +2 -2
  7. package/templates/api/README.md +138 -6
  8. package/templates/api/config/generators.yaml +41 -7
  9. package/templates/api/docs/TESTING_LIVE_APIS.md +417 -0
  10. package/templates/api/pyproject.toml +49 -9
  11. package/templates/api/src/boards/__init__.py +1 -1
  12. package/templates/api/src/boards/auth/adapters/__init__.py +9 -2
  13. package/templates/api/src/boards/auth/factory.py +16 -2
  14. package/templates/api/src/boards/generators/__init__.py +2 -2
  15. package/templates/api/src/boards/generators/artifact_resolution.py +372 -0
  16. package/templates/api/src/boards/generators/artifacts.py +4 -4
  17. package/templates/api/src/boards/generators/base.py +8 -4
  18. package/templates/api/src/boards/generators/implementations/__init__.py +4 -2
  19. package/templates/api/src/boards/generators/implementations/fal/__init__.py +25 -0
  20. package/templates/api/src/boards/generators/implementations/fal/audio/__init__.py +4 -0
  21. package/templates/api/src/boards/generators/implementations/fal/audio/minimax_music_v2.py +173 -0
  22. package/templates/api/src/boards/generators/implementations/fal/audio/minimax_speech_2_6_turbo.py +221 -0
  23. package/templates/api/src/boards/generators/implementations/fal/image/__init__.py +17 -0
  24. package/templates/api/src/boards/generators/implementations/fal/image/flux_pro_kontext.py +216 -0
  25. package/templates/api/src/boards/generators/implementations/fal/image/flux_pro_ultra.py +197 -0
  26. package/templates/api/src/boards/generators/implementations/fal/image/imagen4_preview.py +191 -0
  27. package/templates/api/src/boards/generators/implementations/fal/image/imagen4_preview_fast.py +179 -0
  28. package/templates/api/src/boards/generators/implementations/fal/image/nano_banana.py +183 -0
  29. package/templates/api/src/boards/generators/implementations/fal/image/nano_banana_edit.py +212 -0
  30. package/templates/api/src/boards/generators/implementations/fal/utils.py +61 -0
  31. package/templates/api/src/boards/generators/implementations/fal/video/__init__.py +13 -0
  32. package/templates/api/src/boards/generators/implementations/fal/video/kling_video_v2_5_turbo_pro_text_to_video.py +168 -0
  33. package/templates/api/src/boards/generators/implementations/fal/video/sync_lipsync_v2.py +167 -0
  34. package/templates/api/src/boards/generators/implementations/fal/video/veo31_first_last_frame_to_video.py +180 -0
  35. package/templates/api/src/boards/generators/implementations/openai/__init__.py +1 -0
  36. package/templates/api/src/boards/generators/implementations/openai/audio/__init__.py +1 -0
  37. package/templates/api/src/boards/generators/implementations/{audio → openai/audio}/whisper.py +9 -6
  38. package/templates/api/src/boards/generators/implementations/openai/image/__init__.py +1 -0
  39. package/templates/api/src/boards/generators/implementations/{image → openai/image}/dalle3.py +8 -5
  40. package/templates/api/src/boards/generators/implementations/replicate/__init__.py +1 -0
  41. package/templates/api/src/boards/generators/implementations/replicate/image/__init__.py +1 -0
  42. package/templates/api/src/boards/generators/implementations/{image → replicate/image}/flux_pro.py +8 -5
  43. package/templates/api/src/boards/generators/implementations/replicate/video/__init__.py +1 -0
  44. package/templates/api/src/boards/generators/implementations/{video → replicate/video}/lipsync.py +9 -6
  45. package/templates/api/src/boards/generators/resolution.py +80 -20
  46. package/templates/api/src/boards/jobs/repository.py +49 -0
  47. package/templates/api/src/boards/storage/factory.py +16 -6
  48. package/templates/api/src/boards/workers/actors.py +69 -5
  49. package/templates/api/src/boards/workers/context.py +177 -21
  50. package/templates/web/package.json +2 -1
  51. package/templates/web/src/components/boards/GenerationInput.tsx +154 -52
  52. package/templates/web/src/components/boards/GeneratorSelector.tsx +57 -59
  53. package/templates/web/src/components/ui/dropdown-menu.tsx +200 -0
  54. package/templates/api/src/boards/generators/implementations/audio/__init__.py +0 -3
  55. package/templates/api/src/boards/generators/implementations/image/__init__.py +0 -3
  56. package/templates/api/src/boards/generators/implementations/video/__init__.py +0 -3
@@ -0,0 +1,173 @@
1
+ """
2
+ fal.ai minimax-music/v2 text-to-music generator.
3
+
4
+ Generate music from text prompts using the MiniMax Music 2.0 model, which leverages
5
+ advanced AI techniques to create high-quality, diverse musical compositions.
6
+
7
+ Based on Fal AI's fal-ai/minimax-music/v2 model.
8
+ See: https://fal.ai/models/fal-ai/minimax-music/v2
9
+ """
10
+
11
+ import os
12
+ from typing import Literal
13
+
14
+ from pydantic import BaseModel, Field
15
+
16
+ from ....base import BaseGenerator, GeneratorExecutionContext, GeneratorResult
17
+
18
+
19
+ class AudioSetting(BaseModel):
20
+ """Audio output settings for minimax-music/v2."""
21
+
22
+ format: Literal["mp3", "pcm", "flac"] = Field(
23
+ default="mp3",
24
+ description="Audio output format",
25
+ )
26
+ sample_rate: Literal[8000, 16000, 22050, 24000, 32000, 44100] = Field(
27
+ default=44100,
28
+ description="Audio sample rate in Hz",
29
+ )
30
+ bitrate: Literal[32000, 64000, 128000, 256000] = Field(
31
+ default=256000,
32
+ description="Audio bitrate in bits per second",
33
+ )
34
+
35
+
36
+ class MinimaxMusicV2Input(BaseModel):
37
+ """Input schema for minimax-music/v2 music generation."""
38
+
39
+ prompt: str = Field(
40
+ description="A description of the music, specifying style, mood, and scenario",
41
+ min_length=10,
42
+ max_length=300,
43
+ )
44
+ lyrics_prompt: str = Field(
45
+ description=(
46
+ "Lyrics of the song. Use \\n to separate lines. "
47
+ "Structure tags like [Intro], [Verse], [Chorus] supported"
48
+ ),
49
+ min_length=10,
50
+ max_length=3000,
51
+ )
52
+ audio_setting: AudioSetting | None = Field(
53
+ default=None,
54
+ description="Audio output settings (format, sample rate, bitrate)",
55
+ )
56
+
57
+
58
+ class FalMinimaxMusicV2Generator(BaseGenerator):
59
+ """minimax-music/v2 music generator using fal.ai."""
60
+
61
+ name = "fal-minimax-music-v2"
62
+ artifact_type = "audio"
63
+ description = "Fal: MiniMax Music 2.0 - generate music from text prompts and lyrics"
64
+
65
+ def get_input_schema(self) -> type[MinimaxMusicV2Input]:
66
+ return MinimaxMusicV2Input
67
+
68
+ async def generate(
69
+ self, inputs: MinimaxMusicV2Input, context: GeneratorExecutionContext
70
+ ) -> GeneratorResult:
71
+ """Generate music using fal.ai minimax-music/v2 model."""
72
+ # Check for API key (fal-client uses FAL_KEY environment variable)
73
+ if not os.getenv("FAL_KEY"):
74
+ raise ValueError("API configuration invalid. Missing FAL_KEY environment variable")
75
+
76
+ # Import fal_client
77
+ try:
78
+ import fal_client
79
+ except ImportError as e:
80
+ raise ImportError(
81
+ "fal.ai SDK is required for FalMinimaxMusicV2Generator. "
82
+ "Install with: pip install weirdfingers-boards[generators-fal]"
83
+ ) from e
84
+
85
+ # Prepare arguments for fal.ai API
86
+ from typing import Any
87
+
88
+ arguments: dict[str, Any] = {
89
+ "prompt": inputs.prompt,
90
+ "lyrics_prompt": inputs.lyrics_prompt,
91
+ }
92
+
93
+ # Add audio settings if provided
94
+ if inputs.audio_setting is not None:
95
+ arguments["audio_setting"] = {
96
+ "format": inputs.audio_setting.format,
97
+ "sample_rate": inputs.audio_setting.sample_rate,
98
+ "bitrate": inputs.audio_setting.bitrate,
99
+ }
100
+
101
+ # Submit async job and get handler
102
+ handler = await fal_client.submit_async(
103
+ "fal-ai/minimax-music/v2",
104
+ arguments=arguments,
105
+ )
106
+
107
+ # Store the external job ID for tracking
108
+ await context.set_external_job_id(handler.request_id)
109
+
110
+ # Stream progress updates (sample every 3rd event to avoid spam)
111
+ from .....progress.models import ProgressUpdate
112
+
113
+ event_count = 0
114
+ async for event in handler.iter_events(with_logs=True):
115
+ event_count += 1
116
+
117
+ # Process every 3rd event to provide feedback without overwhelming
118
+ if event_count % 3 == 0:
119
+ # Extract logs if available
120
+ logs = getattr(event, "logs", None)
121
+ if logs:
122
+ # Join log entries into a single message
123
+ if isinstance(logs, list):
124
+ message = " | ".join(str(log) for log in logs if log)
125
+ else:
126
+ message = str(logs)
127
+
128
+ if message:
129
+ await context.publish_progress(
130
+ ProgressUpdate(
131
+ job_id=handler.request_id,
132
+ status="processing",
133
+ progress=50.0, # Approximate mid-point progress
134
+ phase="processing",
135
+ message=message,
136
+ )
137
+ )
138
+
139
+ # Get final result
140
+ result = await handler.get()
141
+
142
+ # Extract audio from result
143
+ # fal.ai returns: {"audio": {"url": "...", "content_type": "...", "file_size": ...}}
144
+ audio_data = result.get("audio")
145
+ if not audio_data:
146
+ raise ValueError("No audio returned from fal.ai API")
147
+
148
+ audio_url = audio_data.get("url")
149
+ if not audio_url:
150
+ raise ValueError("Audio missing URL in fal.ai response")
151
+
152
+ # Determine format from audio_setting or content_type
153
+ audio_format = (
154
+ inputs.audio_setting.format if inputs.audio_setting else "mp3" # Default format
155
+ )
156
+
157
+ # Store audio result
158
+ artifact = await context.store_audio_result(
159
+ storage_url=audio_url,
160
+ format=audio_format,
161
+ sample_rate=inputs.audio_setting.sample_rate if inputs.audio_setting else 44100,
162
+ output_index=0,
163
+ )
164
+
165
+ return GeneratorResult(outputs=[artifact])
166
+
167
+ async def estimate_cost(self, inputs: MinimaxMusicV2Input) -> float:
168
+ """Estimate cost for minimax-music/v2 generation.
169
+
170
+ Estimated at approximately $0.08 per music generation based on typical
171
+ music generation pricing. Actual cost may vary.
172
+ """
173
+ return 0.08 # $0.08 per music generation
@@ -0,0 +1,221 @@
1
+ """
2
+ fal.ai MiniMax Speech 2.6 Turbo text-to-speech generator.
3
+
4
+ Generate speech from text prompts using the MiniMax Speech-2.6 HD model,
5
+ which leverages advanced AI techniques to create high-quality text-to-speech.
6
+
7
+ Based on Fal AI's fal-ai/minimax/speech-2.6-turbo model.
8
+ See: https://fal.ai/models/fal-ai/minimax/speech-2.6-turbo
9
+ """
10
+
11
+ import os
12
+ from typing import Literal
13
+
14
+ from pydantic import BaseModel, Field
15
+
16
+ from ....base import BaseGenerator, GeneratorExecutionContext, GeneratorResult
17
+
18
+
19
+ class VoiceSetting(BaseModel):
20
+ """Voice configuration settings."""
21
+
22
+ voice_id: str = Field(
23
+ default="Wise_Woman",
24
+ description="Speaker identifier for the voice",
25
+ )
26
+ speed: float = Field(
27
+ default=1.0,
28
+ description="Playback speed multiplier",
29
+ )
30
+ pitch: float = Field(
31
+ default=0.0,
32
+ description="Pitch adjustment",
33
+ )
34
+ vol: float = Field(
35
+ default=1.0,
36
+ description="Volume level",
37
+ )
38
+ english_normalization: bool = Field(
39
+ default=False,
40
+ description="Enable English text normalization",
41
+ )
42
+
43
+
44
+ class LoudnessNormalizationSetting(BaseModel):
45
+ """Audio loudness normalization controls."""
46
+
47
+ enabled: bool = Field(
48
+ default=True,
49
+ description="Enable loudness normalization",
50
+ )
51
+ target_loudness: float = Field(
52
+ default=-18.0,
53
+ ge=-70.0,
54
+ le=-10.0,
55
+ description="Target loudness in LUFS",
56
+ )
57
+ target_range: float = Field(
58
+ default=8.0,
59
+ ge=0.0,
60
+ le=20.0,
61
+ description="Target range in LU",
62
+ )
63
+ target_peak: float = Field(
64
+ default=-0.5,
65
+ ge=-3.0,
66
+ le=0.0,
67
+ description="Target peak level in dBTP",
68
+ )
69
+
70
+
71
+ class MinimaxSpeech26TurboInput(BaseModel):
72
+ """Input schema for MiniMax Speech 2.6 Turbo generation."""
73
+
74
+ prompt: str = Field(
75
+ description=(
76
+ "Text to convert to speech " "(supports pause markers <#x#> with 0.01-99.99 seconds)"
77
+ ),
78
+ min_length=1,
79
+ max_length=10000,
80
+ )
81
+ voice_setting: VoiceSetting = Field(
82
+ default_factory=VoiceSetting,
83
+ description="Voice configuration including voice_id, speed, pitch, volume",
84
+ )
85
+ language_boost: str = Field(
86
+ default="auto",
87
+ description=(
88
+ "Enhance recognition of specified languages and dialects "
89
+ "(auto or specific language code)"
90
+ ),
91
+ )
92
+ output_format: Literal["url", "hex"] = Field(
93
+ default="url",
94
+ description="Output format: 'url' for audio file URL or 'hex' for hex-encoded data",
95
+ )
96
+ normalization_setting: LoudnessNormalizationSetting = Field(
97
+ default_factory=LoudnessNormalizationSetting,
98
+ description="Audio loudness normalization controls",
99
+ )
100
+
101
+
102
+ class FalMinimaxSpeech26TurboGenerator(BaseGenerator):
103
+ """MiniMax Speech 2.6 Turbo text-to-speech generator using fal.ai."""
104
+
105
+ name = "fal-minimax-speech-2-6-turbo"
106
+ artifact_type = "audio"
107
+ description = (
108
+ "Fal: MiniMax Speech 2.6 Turbo - "
109
+ "High-quality text-to-speech with customizable voices and 35+ languages"
110
+ )
111
+
112
+ def get_input_schema(self) -> type[MinimaxSpeech26TurboInput]:
113
+ return MinimaxSpeech26TurboInput
114
+
115
+ async def generate(
116
+ self, inputs: MinimaxSpeech26TurboInput, context: GeneratorExecutionContext
117
+ ) -> GeneratorResult:
118
+ """Generate audio using fal.ai MiniMax Speech 2.6 Turbo model."""
119
+ # Check for API key (fal-client uses FAL_KEY environment variable)
120
+ if not os.getenv("FAL_KEY"):
121
+ raise ValueError("API configuration invalid. Missing FAL_KEY environment variable")
122
+
123
+ # Import fal_client
124
+ try:
125
+ import fal_client
126
+ except ImportError as e:
127
+ raise ImportError(
128
+ "fal.ai SDK is required for FalMinimaxSpeech26TurboGenerator. "
129
+ "Install with: pip install weirdfingers-boards[generators-fal]"
130
+ ) from e
131
+
132
+ # Prepare arguments for fal.ai API
133
+ arguments = {
134
+ "prompt": inputs.prompt,
135
+ "voice_setting": {
136
+ "voice_id": inputs.voice_setting.voice_id,
137
+ "speed": inputs.voice_setting.speed,
138
+ "pitch": inputs.voice_setting.pitch,
139
+ "vol": inputs.voice_setting.vol,
140
+ "english_normalization": inputs.voice_setting.english_normalization,
141
+ },
142
+ "language_boost": inputs.language_boost,
143
+ "output_format": inputs.output_format,
144
+ "normalization_setting": {
145
+ "enabled": inputs.normalization_setting.enabled,
146
+ "target_loudness": inputs.normalization_setting.target_loudness,
147
+ "target_range": inputs.normalization_setting.target_range,
148
+ "target_peak": inputs.normalization_setting.target_peak,
149
+ },
150
+ }
151
+
152
+ # Submit async job and get handler
153
+ handler = await fal_client.submit_async(
154
+ "fal-ai/minimax/speech-2.6-turbo",
155
+ arguments=arguments,
156
+ )
157
+
158
+ # Store the external job ID for tracking
159
+ await context.set_external_job_id(handler.request_id)
160
+
161
+ # Stream progress updates (sample every 3rd event to avoid spam)
162
+ from .....progress.models import ProgressUpdate
163
+
164
+ event_count = 0
165
+ async for event in handler.iter_events(with_logs=True):
166
+ event_count += 1
167
+
168
+ # Process every 3rd event to provide feedback without overwhelming
169
+ if event_count % 3 == 0:
170
+ # Extract logs if available
171
+ logs = getattr(event, "logs", None)
172
+ if logs:
173
+ # Join log entries into a single message
174
+ if isinstance(logs, list):
175
+ message = " | ".join(str(log) for log in logs if log)
176
+ else:
177
+ message = str(logs)
178
+
179
+ if message:
180
+ await context.publish_progress(
181
+ ProgressUpdate(
182
+ job_id=handler.request_id,
183
+ status="processing",
184
+ progress=50.0, # Approximate mid-point progress
185
+ phase="processing",
186
+ message=message,
187
+ )
188
+ )
189
+
190
+ # Get final result
191
+ result = await handler.get()
192
+
193
+ # Extract audio URL from result
194
+ # fal.ai returns: {"audio": {"url": "..."}}
195
+ audio_data = result.get("audio")
196
+ if audio_data is None:
197
+ raise ValueError("No audio data returned from fal.ai API")
198
+
199
+ audio_url = audio_data.get("url")
200
+ if not audio_url:
201
+ raise ValueError("Audio URL missing in fal.ai response")
202
+
203
+ # Store audio result
204
+ artifact = await context.store_audio_result(
205
+ storage_url=audio_url,
206
+ format="mp3", # MiniMax Speech returns MP3 format
207
+ output_index=0,
208
+ )
209
+
210
+ return GeneratorResult(outputs=[artifact])
211
+
212
+ async def estimate_cost(self, inputs: MinimaxSpeech26TurboInput) -> float:
213
+ """Estimate cost for MiniMax Speech 2.6 Turbo generation.
214
+
215
+ MiniMax Speech 2.6 Turbo costs $0.06 per 1000 characters.
216
+ """
217
+ # Calculate character count
218
+ char_count = len(inputs.prompt)
219
+
220
+ # Cost is $0.06 per 1000 characters
221
+ return (char_count / 1000.0) * 0.06
@@ -0,0 +1,17 @@
1
+ """Fal.ai image generators."""
2
+
3
+ from .flux_pro_kontext import FalFluxProKontextGenerator
4
+ from .flux_pro_ultra import FalFluxProUltraGenerator
5
+ from .imagen4_preview import FalImagen4PreviewGenerator
6
+ from .imagen4_preview_fast import FalImagen4PreviewFastGenerator
7
+ from .nano_banana import FalNanoBananaGenerator
8
+ from .nano_banana_edit import FalNanoBananaEditGenerator
9
+
10
+ __all__ = [
11
+ "FalFluxProKontextGenerator",
12
+ "FalFluxProUltraGenerator",
13
+ "FalImagen4PreviewGenerator",
14
+ "FalImagen4PreviewFastGenerator",
15
+ "FalNanoBananaGenerator",
16
+ "FalNanoBananaEditGenerator",
17
+ ]
@@ -0,0 +1,216 @@
1
+ """
2
+ fal.ai FLUX.1 [pro] Kontext image-to-image generator.
3
+
4
+ Handles both text and reference images as inputs, enabling targeted local edits
5
+ and complex transformations of entire scenes using fal.ai's flux-pro/kontext model.
6
+ """
7
+
8
+ import os
9
+ from typing import Literal
10
+
11
+ from pydantic import BaseModel, Field
12
+
13
+ from ....artifacts import ImageArtifact
14
+ from ....base import BaseGenerator, GeneratorExecutionContext, GeneratorResult
15
+
16
+
17
+ class FluxProKontextInput(BaseModel):
18
+ """Input schema for FLUX.1 [pro] Kontext image generation.
19
+
20
+ Artifact fields (like image_url) are automatically detected via type
21
+ introspection and resolved from generation IDs to ImageArtifact objects.
22
+ """
23
+
24
+ prompt: str = Field(
25
+ description="Text prompt for image editing (e.g., 'Put a donut next to the flour')"
26
+ )
27
+ image_url: ImageArtifact = Field(
28
+ description="Reference image for transformation (from previous generation)",
29
+ )
30
+ aspect_ratio: (
31
+ Literal[
32
+ "21:9",
33
+ "16:9",
34
+ "4:3",
35
+ "3:2",
36
+ "1:1",
37
+ "2:3",
38
+ "3:4",
39
+ "9:16",
40
+ "9:21",
41
+ ]
42
+ | None
43
+ ) = Field(
44
+ default=None,
45
+ description="Image aspect ratio (optional)",
46
+ )
47
+ num_images: int = Field(
48
+ default=1,
49
+ ge=1,
50
+ le=4,
51
+ description="Number of images to generate (1-4)",
52
+ )
53
+ output_format: Literal["jpeg", "png"] = Field(
54
+ default="jpeg",
55
+ description="Output image format",
56
+ )
57
+ sync_mode: bool = Field(
58
+ default=False,
59
+ description=(
60
+ "If True, the media will be returned as a data URI and the output "
61
+ "data won't be available in the request history"
62
+ ),
63
+ )
64
+ safety_tolerance: str = Field(
65
+ default="2",
66
+ description="Safety tolerance level (1-6 scale, higher is more permissive)",
67
+ )
68
+ guidance_scale: float = Field(
69
+ default=3.5,
70
+ ge=1.0,
71
+ le=20.0,
72
+ description="Guidance scale for image generation (1-20)",
73
+ )
74
+ seed: int | None = Field(
75
+ default=None,
76
+ description="Random seed for reproducible outputs (optional)",
77
+ )
78
+ enhance_prompt: bool = Field(
79
+ default=False,
80
+ description="Automatically enhance the prompt for better quality",
81
+ )
82
+
83
+
84
+ class FalFluxProKontextGenerator(BaseGenerator):
85
+ """FLUX.1 [pro] Kontext image-to-image generator using fal.ai."""
86
+
87
+ name = "fal-flux-pro-kontext"
88
+ artifact_type = "image"
89
+ description = (
90
+ "Fal: FLUX.1 [pro] Kontext - Image-to-image editing with text and reference images"
91
+ )
92
+
93
+ def get_input_schema(self) -> type[FluxProKontextInput]:
94
+ return FluxProKontextInput
95
+
96
+ async def generate(
97
+ self, inputs: FluxProKontextInput, context: GeneratorExecutionContext
98
+ ) -> GeneratorResult:
99
+ """Generate images using fal.ai flux-pro/kontext model."""
100
+ # Check for API key (fal-client uses FAL_KEY environment variable)
101
+ if not os.getenv("FAL_KEY"):
102
+ raise ValueError("API configuration invalid. Missing FAL_KEY environment variable")
103
+
104
+ # Import fal_client
105
+ try:
106
+ import fal_client
107
+ except ImportError as e:
108
+ raise ImportError(
109
+ "fal.ai SDK is required for FalFluxProKontextGenerator. "
110
+ "Install with: pip install weirdfingers-boards[generators-fal]"
111
+ ) from e
112
+
113
+ # Upload image artifact to Fal's public storage
114
+ # Fal API requires publicly accessible URLs
115
+ from ..utils import upload_artifacts_to_fal
116
+
117
+ image_urls = await upload_artifacts_to_fal([inputs.image_url], context)
118
+ image_url = image_urls[0] # Extract single URL from list
119
+
120
+ # Prepare arguments for fal.ai API
121
+ arguments = {
122
+ "prompt": inputs.prompt,
123
+ "image_url": image_url,
124
+ "num_images": inputs.num_images,
125
+ "output_format": inputs.output_format,
126
+ "sync_mode": inputs.sync_mode,
127
+ "safety_tolerance": inputs.safety_tolerance,
128
+ "guidance_scale": inputs.guidance_scale,
129
+ "enhance_prompt": inputs.enhance_prompt,
130
+ }
131
+
132
+ # Add optional fields if provided
133
+ if inputs.aspect_ratio is not None:
134
+ arguments["aspect_ratio"] = inputs.aspect_ratio
135
+
136
+ if inputs.seed is not None:
137
+ arguments["seed"] = inputs.seed
138
+
139
+ # Submit async job and get handler
140
+ handler = await fal_client.submit_async(
141
+ "fal-ai/flux-pro/kontext",
142
+ arguments=arguments,
143
+ )
144
+
145
+ # Store the external job ID for tracking
146
+ await context.set_external_job_id(handler.request_id)
147
+
148
+ # Stream progress updates (sample every 3rd event to avoid spam)
149
+ from .....progress.models import ProgressUpdate
150
+
151
+ event_count = 0
152
+ async for event in handler.iter_events(with_logs=True):
153
+ event_count += 1
154
+
155
+ # Process every 3rd event to provide feedback without overwhelming
156
+ if event_count % 3 == 0:
157
+ # Extract logs if available
158
+ logs = getattr(event, "logs", None)
159
+ if logs:
160
+ # Join log entries into a single message
161
+ if isinstance(logs, list):
162
+ message = " | ".join(str(log) for log in logs if log)
163
+ else:
164
+ message = str(logs)
165
+
166
+ if message:
167
+ await context.publish_progress(
168
+ ProgressUpdate(
169
+ job_id=handler.request_id,
170
+ status="processing",
171
+ progress=50.0, # Approximate mid-point progress
172
+ phase="processing",
173
+ message=message,
174
+ )
175
+ )
176
+
177
+ # Get final result
178
+ result = await handler.get()
179
+
180
+ # Extract image URLs from result
181
+ # fal.ai returns: {"images": [{"url": "...", "width": ..., "height": ...}, ...]}
182
+ images = result.get("images", [])
183
+ if not images:
184
+ raise ValueError("No images returned from fal.ai API")
185
+
186
+ # Store each image using output_index
187
+ artifacts = []
188
+ for idx, image_data in enumerate(images):
189
+ image_url_result = image_data.get("url")
190
+ width = image_data.get("width", 1024)
191
+ height = image_data.get("height", 1024)
192
+
193
+ if not image_url_result:
194
+ raise ValueError(f"Image {idx} missing URL in fal.ai response")
195
+
196
+ # Store with appropriate output_index
197
+ artifact = await context.store_image_result(
198
+ storage_url=image_url_result,
199
+ format=inputs.output_format,
200
+ width=width,
201
+ height=height,
202
+ output_index=idx,
203
+ )
204
+ artifacts.append(artifact)
205
+
206
+ return GeneratorResult(outputs=artifacts)
207
+
208
+ async def estimate_cost(self, inputs: FluxProKontextInput) -> float:
209
+ """Estimate cost for FLUX.1 [pro] Kontext generation.
210
+
211
+ FLUX.1 [pro] Kontext is a premium image-to-image model. Estimated cost
212
+ is approximately $0.055 per image based on similar Flux Pro models.
213
+ """
214
+ # Cost per image * number of images
215
+ cost_per_image = 0.055
216
+ return cost_per_image * inputs.num_images