Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from fastapi.responses import StreamingResponse
|
| 3 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
import io
|
| 5 |
+
import wave
|
| 6 |
+
import math
|
| 7 |
+
import struct
|
| 8 |
+
|
| 9 |
+
# Initialize the FastAPI app
|
| 10 |
+
app = FastAPI()
|
| 11 |
+
|
| 12 |
+
# IMPORTANT: Add CORS middleware to allow requests from your frontend
|
| 13 |
+
# This is crucial for connecting the two parts.
|
| 14 |
+
app.add_middleware(
|
| 15 |
+
CORSMiddleware,
|
| 16 |
+
allow_origins=["*"], # Allows all origins
|
| 17 |
+
allow_credentials=True,
|
| 18 |
+
allow_methods=["*"], # Allows all methods
|
| 19 |
+
allow_headers=["*"], # Allows all headers
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
def create_mock_wav_in_memory(prompt: str) -> io.BytesIO:
|
| 23 |
+
"""Generates a 1-second, 440Hz sine wave WAV file in memory."""
|
| 24 |
+
sample_rate = 44100
|
| 25 |
+
duration_seconds = 1
|
| 26 |
+
frequency = 440.0 # A4 note
|
| 27 |
+
num_samples = duration_seconds * sample_rate
|
| 28 |
+
|
| 29 |
+
# Use io.BytesIO to build the WAV file in memory
|
| 30 |
+
wav_file_in_memory = io.BytesIO()
|
| 31 |
+
|
| 32 |
+
with wave.open(wav_file_in_memory, 'wb') as w:
|
| 33 |
+
w.setnchannels(1) # Mono
|
| 34 |
+
w.setsampwidth(2) # 16-bit PCM
|
| 35 |
+
w.setframerate(sample_rate)
|
| 36 |
+
|
| 37 |
+
for i in range(num_samples):
|
| 38 |
+
# Calculate the sample value for the sine wave
|
| 39 |
+
value = int(32767.0 * math.sin(2 * math.pi * frequency * i / sample_rate))
|
| 40 |
+
# Pack the value as a 16-bit signed integer
|
| 41 |
+
data = struct.pack('<h', value)
|
| 42 |
+
w.writeframesraw(data)
|
| 43 |
+
|
| 44 |
+
# Go back to the beginning of the in-memory file so it can be read
|
| 45 |
+
wav_file_in_memory.seek(0)
|
| 46 |
+
return wav_file_in_memory
|
| 47 |
+
|
| 48 |
+
@app.post("/generate-audio")
|
| 49 |
+
async def generate_audio_endpoint(payload: dict):
|
| 50 |
+
"""
|
| 51 |
+
This endpoint receives a text prompt and returns a generated audio file.
|
| 52 |
+
"""
|
| 53 |
+
prompt = payload.get("prompt")
|
| 54 |
+
if not prompt:
|
| 55 |
+
raise HTTPException(status_code=400, detail="A 'prompt' is required in the request body.")
|
| 56 |
+
|
| 57 |
+
# Generate the mock audio data
|
| 58 |
+
audio_data_stream = create_mock_wav_in_memory(prompt)
|
| 59 |
+
|
| 60 |
+
# Create a safe filename from the prompt
|
| 61 |
+
safe_filename = "".join(c for c in prompt if c.isalnum() or c in (' ', '_')).rstrip()[:50] + ".wav"
|
| 62 |
+
|
| 63 |
+
# Return the in-memory WAV file as a streaming response
|
| 64 |
+
return StreamingResponse(
|
| 65 |
+
audio_data_stream,
|
| 66 |
+
media_type="audio/wav",
|
| 67 |
+
headers={"Content-Disposition": f"attachment; filename=\"{safe_filename}\""}
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
@app.get("/")
|
| 71 |
+
def read_root():
|
| 72 |
+
"""A simple root endpoint to confirm the API is running."""
|
| 73 |
+
return {"message": "Mock Audio Generation API is running."}
|