File size: 11,782 Bytes
957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 982cefe 8bacbab b99fda7 957256e b99fda7 957256e b99fda7 957256e b99fda7 957256e 982cefe 957256e b99fda7 957256e b99fda7 957256e b99fda7 957256e 8bacbab 957256e 8bacbab 957256e ed5dd8e 957256e 8bacbab 957256e 8424f66 957256e 8bacbab 957256e 8bacbab 957256e 8bacbab 957256e 8424f66 957256e 8bacbab 957256e 8bacbab 957256e ed5dd8e 957256e 8bacbab 957256e 8424f66 957256e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 |
"""
Event Handlers
Handlers for all Gradio UI events.
Connects UI to services with error handling.
"""
import asyncio
from typing import Optional, Tuple
from ..services.music import MusicService
from ..services.image import ImageService
from ..services.video import VideoService
from ..api.client import StackNetClient
def format_error(error: Exception) -> str:
"""Format exception as user-friendly message."""
msg = str(error)
# Translate common errors
if "timeout" in msg.lower():
return "The operation timed out. Please try again with a simpler prompt."
if "network" in msg.lower() or "connection" in msg.lower():
return "Network error. Please check your connection and try again."
if "rate limit" in msg.lower() or "429" in msg.lower():
return "Too many requests. Please wait a moment and try again."
return f"Error: {msg}"
class Handlers:
"""
Collection of event handlers for the Gradio UI.
All handlers hide API complexity and provide clean feedback.
"""
@staticmethod
def generate_music(
prompt: str,
tags: str,
instrumental: bool,
lyrics: str,
title: str,
api_key: str = ""
) -> Tuple[Optional[str], str]:
"""Handle text-to-music generation."""
if not prompt.strip():
return None, "Please enter a description for your music."
if not api_key.strip():
return None, "Please enter your API key in the Settings section."
client = StackNetClient(api_key=api_key.strip())
service = MusicService(client=client)
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
clips = loop.run_until_complete(
service.generate_music(
prompt=prompt,
title=title if title.strip() else None,
tags=tags if tags.strip() else None,
lyrics=lyrics if lyrics.strip() and not instrumental else None,
instrumental=instrumental
)
)
if clips:
audio_path = loop.run_until_complete(
service.download_clip(clips[0])
)
return audio_path, "Music generation complete!"
else:
return None, "No music was generated. Please try a different prompt."
finally:
loop.close()
except Exception as e:
return None, format_error(e)
finally:
service.cleanup()
@staticmethod
def create_cover(
audio_file: str,
style_prompt: str,
tags: str,
title: str,
api_key: str = ""
) -> Tuple[Optional[str], str]:
"""Handle cover song creation."""
if not audio_file:
return None, "Please upload an audio file."
if not style_prompt.strip():
return None, "Please describe the style for your cover."
if not api_key.strip():
return None, "Please enter your API key in the Settings section."
client = StackNetClient(api_key=api_key.strip())
service = MusicService(client=client)
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
audio_url = f"file://{audio_file}"
clips = loop.run_until_complete(
service.create_cover(
audio_url=audio_url,
style_prompt=style_prompt,
title=title if title.strip() else None,
tags=tags if tags.strip() else None
)
)
if clips:
audio_path = loop.run_until_complete(
service.download_clip(clips[0])
)
return audio_path, "Cover created successfully!"
else:
return None, "No cover was generated. Please try again."
finally:
loop.close()
except Exception as e:
return None, format_error(e)
finally:
service.cleanup()
@staticmethod
def extract_stems(
audio_file: str,
api_key: str = ""
) -> Tuple[Optional[str], Optional[str], Optional[str], Optional[str], str]:
"""Handle stem extraction."""
if not audio_file:
return None, None, None, None, "Please upload an audio file."
if not api_key.strip():
return None, None, None, None, "Please enter your API key in the Settings section."
client = StackNetClient(api_key=api_key.strip())
service = MusicService(client=client)
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
audio_url = f"file://{audio_file}"
stems = loop.run_until_complete(
service.extract_stems(audio_url=audio_url)
)
return (
stems.vocals_path,
stems.drums_path,
stems.bass_path,
stems.other_path,
"Stems extracted successfully!"
)
finally:
loop.close()
except Exception as e:
return None, None, None, None, format_error(e)
finally:
service.cleanup()
@staticmethod
def generate_image(
prompt: str,
format_type: str,
api_key: str = ""
) -> Tuple[Optional[str], Optional[str], str]:
"""Handle text-to-image generation.
Returns:
Tuple of (image_url, model_url, status)
- For image/multi: (url, None, status)
- For 3d: (None, url, status)
"""
if not prompt.strip():
return None, None, "Please enter a description for your image."
if not api_key.strip():
return None, None, "Please enter your API key in the Settings section."
client = StackNetClient(api_key=api_key.strip())
service = ImageService(client=client)
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
results = loop.run_until_complete(
service.generate_image(
prompt=prompt,
format_type=format_type
)
)
if results:
url = results[0].image_url
if format_type == "3d":
# Return as 3D model
return None, url, "3D model generated successfully!"
else:
# Return as image
return url, None, "Image generated successfully!"
else:
return None, None, "No result was generated. Please try a different prompt."
finally:
loop.close()
except Exception as e:
return None, None, format_error(e)
finally:
service.cleanup()
@staticmethod
def edit_image(
input_image: str,
edit_prompt: str,
strength: float,
api_key: str = ""
) -> Tuple[Optional[str], str]:
"""Handle image-to-image editing."""
if not input_image:
return None, "Please upload an image."
if not edit_prompt.strip():
return None, "Please describe how you want to edit the image."
if not api_key.strip():
return None, "Please enter your API key in the Settings section."
client = StackNetClient(api_key=api_key.strip())
service = ImageService(client=client)
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
image_url = f"file://{input_image}"
images = loop.run_until_complete(
service.edit_image(
image_url=image_url,
edit_prompt=edit_prompt,
strength=strength
)
)
if images:
# Return URL directly - Gradio can display remote images
return images[0].image_url, "Image edited successfully!"
else:
return None, "No edited image was generated. Please try again."
finally:
loop.close()
except Exception as e:
return None, format_error(e)
finally:
service.cleanup()
@staticmethod
def generate_video(
prompt: str,
duration: int,
style: str,
api_key: str = ""
) -> Tuple[Optional[str], str]:
"""Handle text-to-video generation."""
if not prompt.strip():
return None, "Please enter a description for your video."
if not api_key.strip():
return None, "Please enter your API key in the Settings section."
client = StackNetClient(api_key=api_key.strip())
service = VideoService(client=client)
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
videos = loop.run_until_complete(
service.generate_video(
prompt=prompt,
duration=int(duration),
style=style
)
)
if videos:
# Return URL directly - Gradio can display remote videos
return videos[0].video_url, "Video generated successfully!"
else:
return None, "No video was generated. Please try a different prompt."
finally:
loop.close()
except Exception as e:
return None, format_error(e)
finally:
service.cleanup()
@staticmethod
def animate_image(
input_image: str,
motion_prompt: str,
duration: int,
api_key: str = ""
) -> Tuple[Optional[str], str]:
"""Handle image-to-video animation."""
if not input_image:
return None, "Please upload an image."
if not motion_prompt.strip():
return None, "Please describe the motion you want."
if not api_key.strip():
return None, "Please enter your API key in the Settings section."
client = StackNetClient(api_key=api_key.strip())
service = VideoService(client=client)
try:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
try:
image_url = f"file://{input_image}"
videos = loop.run_until_complete(
service.animate_image(
image_url=image_url,
motion_prompt=motion_prompt,
duration=int(duration)
)
)
if videos:
# Return URL directly - Gradio can display remote videos
return videos[0].video_url, "Image animated successfully!"
else:
return None, "No video was generated. Please try again."
finally:
loop.close()
except Exception as e:
return None, format_error(e)
finally:
service.cleanup()
|