Akwbw commited on
Commit
6727b6c
·
verified ·
1 Parent(s): d79b34d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -81
app.py CHANGED
@@ -1,90 +1,31 @@
1
- import os
2
- import torch
3
- import scipy.io.wavfile
4
  import uuid
5
- from fastapi import FastAPI, HTTPException, Header
6
- from fastapi.responses import FileResponse, JSONResponse
7
- from fastapi.middleware.cors import CORSMiddleware
8
- from diffusers import AudioLDM2Pipeline
9
-
10
- app = FastAPI()
11
-
12
- # --- CORS Permissions ---
13
- app.add_middleware(
14
- CORSMiddleware,
15
- allow_origins=["*"],
16
- allow_credentials=True,
17
- allow_methods=["*"],
18
- allow_headers=["*"],
19
- )
20
-
21
- OUTPUT_DIR = "/tmp"
22
- os.makedirs(OUTPUT_DIR, exist_ok=True)
23
- API_KEY = os.getenv("API_KEY", "MySecretPassword123")
24
-
25
- # --- MODEL LOADING (Startup par 1-2 min lega) ---
26
- print("⏳ Loading Music AI Model (AudioLDM-2 Lite)...")
27
- try:
28
- pipe = AudioLDM2Pipeline.from_pretrained(
29
- "cvssp/audioldm2-lite", # Lite version (CPU friendly)
30
- torch_dtype=torch.float32 # CPU par float32 best hai
31
- )
32
- # Speed Optimization: CPU Offload na karein, direct CPU use karein
33
- pipe.to("cpu")
34
- print("✅ Music Model Loaded Successfully!")
35
- except Exception as e:
36
- print(f"❌ Error Loading Model: {e}")
37
- pipe = None
38
-
39
- @app.get("/")
40
- def home():
41
- if pipe is None:
42
- return {"status": "Error", "message": "Model failed to load. Check Logs."}
43
- return {"status": "Online", "message": "AI Music Generator Ready"}
44
-
45
- @app.post("/generate")
46
- async def generate_music(
47
- prompt: str,
48
- duration: int = 5, # Seconds (Free tier ke liye 5-10s best hai)
49
- x_api_key: str = Header(None)
50
- ):
51
- # 1. Security Check
52
- if x_api_key != API_KEY:
53
- raise HTTPException(status_code=401, detail="Invalid API Key")
54
 
55
- if pipe is None:
56
- raise HTTPException(status_code=500, detail="Model is not loaded")
57
 
58
- # Limit Duration to avoid crashing free tier
59
- if duration > 10:
60
- duration = 10
61
 
62
- filename = f"{uuid.uuid4()}.wav"
63
- filepath = os.path.join(OUTPUT_DIR, filename)
 
 
 
 
 
 
64
 
65
- try:
66
- print(f"🎵 Generating Music: '{prompt}' ({duration}s)...")
67
-
68
- # 2. GENERATION LOGIC
69
- # num_inference_steps=10 rakha hai taakay jaldi banay (Quality thori kam hogi par speed achi hogi)
70
- # Agar quality achi chahiye to 20 kar dein (Lekin time double lagega)
71
- audio = pipe(
72
- prompt,
73
- num_inference_steps=10,
74
- audio_length_in_s=duration,
75
- negative_prompt="low quality, average quality, noise"
76
- ).audios[0]
77
 
78
- # 3. Save File
79
- scipy.io.wavfile.write(filepath, rate=16000, data=audio)
80
-
81
- print("✅ Music Saved!")
82
- return FileResponse(filepath, media_type="audio/wav", filename="music.wav")
83
 
84
- except Exception as e:
85
- print(f"❌ Error: {e}")
86
- return JSONResponse(status_code=500, content={"error": str(e)})
87
 
88
  if __name__ == "__main__":
89
- import uvicorn
90
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ from flask import Flask, request, jsonify
2
+ from api import UrduWhisper
 
3
  import uuid
4
+ import os
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
+ app = Flask(__name__)
7
+ model = UrduWhisper()
8
 
9
+ UPLOAD = "uploads"
10
+ os.makedirs(UPLOAD, exist_ok=True)
 
11
 
12
+ @app.route("/transcribe", methods=["POST"])
13
+ def transcribe_audio():
14
+ if "file" not in request.files:
15
+ return jsonify({"error": "No file uploaded"}), 400
16
+
17
+ file = request.files["file"]
18
+ filename = f"{UPLOAD}/{uuid.uuid4()}.wav"
19
+ file.save(filename)
20
 
21
+ text = model.transcribe(filename)
22
+ os.remove(filename)
 
 
 
 
 
 
 
 
 
 
23
 
24
+ return jsonify({"text": text})
 
 
 
 
25
 
26
+ @app.route("/")
27
+ def home():
28
+ return {"message": "Custom Urdu Whisper API Running!"}
29
 
30
  if __name__ == "__main__":
31
+ app.run(host="0.0.0.0", port=5000)