gcharanteja commited on
Commit ·
6514091
1
Parent(s): 9d7ff8b
qwen url fix
Browse files
main.py
CHANGED
|
@@ -3,6 +3,7 @@ from fastapi import FastAPI, Query, HTTPException, UploadFile, File, Form, Reque
|
|
| 3 |
from fastapi.responses import JSONResponse, RedirectResponse, StreamingResponse
|
| 4 |
import gradio as gr
|
| 5 |
from app import gradio_app
|
|
|
|
| 6 |
from database import (
|
| 7 |
init_database,
|
| 8 |
# Songs
|
|
@@ -213,7 +214,7 @@ async def create_song(
|
|
| 213 |
):
|
| 214 |
"""Add a new song with optional MP3 file upload to MinIO."""
|
| 215 |
song = add_song(title, artist, album, duration, bpm, energy_level)
|
| 216 |
-
|
| 217 |
# Handle MP3 file upload to MinIO
|
| 218 |
minio_info = None
|
| 219 |
if audio_file and audio_file.filename:
|
|
@@ -225,25 +226,38 @@ async def create_song(
|
|
| 225 |
song_id=song["id"],
|
| 226 |
filename=audio_file.filename,
|
| 227 |
)
|
| 228 |
-
|
| 229 |
if minio_info:
|
| 230 |
# Update song with file reference
|
| 231 |
update_song_file(song["id"], minio_info["object_name"], minio_info["size"])
|
| 232 |
-
|
| 233 |
# Add to semantic search index if lyrics provided
|
| 234 |
if lyrics:
|
| 235 |
add_song_vibe(song["id"], title, artist, lyrics)
|
| 236 |
-
|
| 237 |
# Invalidate songs list cache
|
| 238 |
await invalidate_list_cache("all_songs")
|
| 239 |
-
|
| 240 |
# Log activity to MongoDB
|
| 241 |
await track_analytics_event("song_created", {"song_id": song["id"], "title": title})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 242 |
|
| 243 |
response = {"status": "success", "song": song}
|
| 244 |
if minio_info:
|
| 245 |
-
public_stream_url = f"{
|
| 246 |
-
public_download_url = f"{
|
| 247 |
response["audio"] = {
|
| 248 |
"uploaded": True,
|
| 249 |
"size": minio_info["size"],
|
|
@@ -319,39 +333,49 @@ async def upload_audio(request: Request, song_id: int, audio_file: UploadFile =
|
|
| 319 |
song = get_song_by_id(song_id)
|
| 320 |
if not song:
|
| 321 |
raise HTTPException(status_code=404, detail="Song not found")
|
| 322 |
-
|
| 323 |
# Validate file type
|
| 324 |
if not audio_file.filename.lower().endswith('.mp3'):
|
| 325 |
raise HTTPException(status_code=400, detail="Only MP3 files are supported")
|
| 326 |
-
|
| 327 |
# Read and upload file
|
| 328 |
contents = await audio_file.read()
|
| 329 |
file_stream = io.BytesIO(contents)
|
| 330 |
-
|
| 331 |
minio_info = upload_mp3_file(
|
| 332 |
file_data=file_stream,
|
| 333 |
file_size=len(contents),
|
| 334 |
song_id=song_id,
|
| 335 |
filename=audio_file.filename,
|
| 336 |
)
|
| 337 |
-
|
| 338 |
if not minio_info:
|
| 339 |
raise HTTPException(status_code=500, detail="Failed to upload file to MinIO")
|
| 340 |
-
|
| 341 |
# Update song with file reference
|
| 342 |
update_song_file(song_id, minio_info["object_name"], minio_info["size"])
|
| 343 |
-
|
| 344 |
# Invalidate cache
|
| 345 |
await invalidate_song_cache(song_id)
|
| 346 |
await invalidate_list_cache("all_songs")
|
| 347 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 348 |
return {
|
| 349 |
"status": "success",
|
| 350 |
"audio": {
|
| 351 |
"uploaded": True,
|
| 352 |
"size": minio_info["size"],
|
| 353 |
-
"stream_url": f"{
|
| 354 |
-
"download_url": f"{
|
| 355 |
"minio_presigned_url": minio_info["presigned_url"],
|
| 356 |
},
|
| 357 |
}
|
|
|
|
| 3 |
from fastapi.responses import JSONResponse, RedirectResponse, StreamingResponse
|
| 4 |
import gradio as gr
|
| 5 |
from app import gradio_app
|
| 6 |
+
import os
|
| 7 |
from database import (
|
| 8 |
init_database,
|
| 9 |
# Songs
|
|
|
|
| 214 |
):
|
| 215 |
"""Add a new song with optional MP3 file upload to MinIO."""
|
| 216 |
song = add_song(title, artist, album, duration, bpm, energy_level)
|
| 217 |
+
|
| 218 |
# Handle MP3 file upload to MinIO
|
| 219 |
minio_info = None
|
| 220 |
if audio_file and audio_file.filename:
|
|
|
|
| 226 |
song_id=song["id"],
|
| 227 |
filename=audio_file.filename,
|
| 228 |
)
|
| 229 |
+
|
| 230 |
if minio_info:
|
| 231 |
# Update song with file reference
|
| 232 |
update_song_file(song["id"], minio_info["object_name"], minio_info["size"])
|
| 233 |
+
|
| 234 |
# Add to semantic search index if lyrics provided
|
| 235 |
if lyrics:
|
| 236 |
add_song_vibe(song["id"], title, artist, lyrics)
|
| 237 |
+
|
| 238 |
# Invalidate songs list cache
|
| 239 |
await invalidate_list_cache("all_songs")
|
| 240 |
+
|
| 241 |
# Log activity to MongoDB
|
| 242 |
await track_analytics_event("song_created", {"song_id": song["id"], "title": title})
|
| 243 |
+
|
| 244 |
+
# Determine the public base URL for the hosted environment
|
| 245 |
+
# Use environment variable or derive from request headers
|
| 246 |
+
public_base = os.getenv("PUBLIC_BASE_URL")
|
| 247 |
+
if not public_base:
|
| 248 |
+
# Try to derive from request headers (works with proxies like HF Spaces)
|
| 249 |
+
forwarded_proto = request.headers.get("x-forwarded-proto")
|
| 250 |
+
forwarded_host = request.headers.get("x-forwarded-host")
|
| 251 |
+
if forwarded_proto and forwarded_host:
|
| 252 |
+
public_base = f"{forwarded_proto}://{forwarded_host}"
|
| 253 |
+
else:
|
| 254 |
+
# Fallback to request base URL
|
| 255 |
+
public_base = str(request.base_url).rstrip("/")
|
| 256 |
|
| 257 |
response = {"status": "success", "song": song}
|
| 258 |
if minio_info:
|
| 259 |
+
public_stream_url = f"{public_base}/songs/{song['id']}/stream"
|
| 260 |
+
public_download_url = f"{public_base}/songs/{song['id']}/download"
|
| 261 |
response["audio"] = {
|
| 262 |
"uploaded": True,
|
| 263 |
"size": minio_info["size"],
|
|
|
|
| 333 |
song = get_song_by_id(song_id)
|
| 334 |
if not song:
|
| 335 |
raise HTTPException(status_code=404, detail="Song not found")
|
| 336 |
+
|
| 337 |
# Validate file type
|
| 338 |
if not audio_file.filename.lower().endswith('.mp3'):
|
| 339 |
raise HTTPException(status_code=400, detail="Only MP3 files are supported")
|
| 340 |
+
|
| 341 |
# Read and upload file
|
| 342 |
contents = await audio_file.read()
|
| 343 |
file_stream = io.BytesIO(contents)
|
| 344 |
+
|
| 345 |
minio_info = upload_mp3_file(
|
| 346 |
file_data=file_stream,
|
| 347 |
file_size=len(contents),
|
| 348 |
song_id=song_id,
|
| 349 |
filename=audio_file.filename,
|
| 350 |
)
|
| 351 |
+
|
| 352 |
if not minio_info:
|
| 353 |
raise HTTPException(status_code=500, detail="Failed to upload file to MinIO")
|
| 354 |
+
|
| 355 |
# Update song with file reference
|
| 356 |
update_song_file(song_id, minio_info["object_name"], minio_info["size"])
|
| 357 |
+
|
| 358 |
# Invalidate cache
|
| 359 |
await invalidate_song_cache(song_id)
|
| 360 |
await invalidate_list_cache("all_songs")
|
| 361 |
+
|
| 362 |
+
# Determine the public base URL for the hosted environment
|
| 363 |
+
public_base = os.getenv("PUBLIC_BASE_URL")
|
| 364 |
+
if not public_base:
|
| 365 |
+
forwarded_proto = request.headers.get("x-forwarded-proto")
|
| 366 |
+
forwarded_host = request.headers.get("x-forwarded-host")
|
| 367 |
+
if forwarded_proto and forwarded_host:
|
| 368 |
+
public_base = f"{forwarded_proto}://{forwarded_host}"
|
| 369 |
+
else:
|
| 370 |
+
public_base = str(request.base_url).rstrip("/")
|
| 371 |
+
|
| 372 |
return {
|
| 373 |
"status": "success",
|
| 374 |
"audio": {
|
| 375 |
"uploaded": True,
|
| 376 |
"size": minio_info["size"],
|
| 377 |
+
"stream_url": f"{public_base}/songs/{song_id}/stream",
|
| 378 |
+
"download_url": f"{public_base}/songs/{song_id}/download",
|
| 379 |
"minio_presigned_url": minio_info["presigned_url"],
|
| 380 |
},
|
| 381 |
}
|