Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
import yt_dlp
|
| 3 |
+
import whisper
|
| 4 |
+
import tempfile
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
app = FastAPI(title="Silent Tech Utils API")
|
| 8 |
+
|
| 9 |
+
# Load the AI Voice model into RAM (Using 'base' so it's super fast on a CPU)
|
| 10 |
+
print("Loading Whisper AI...")
|
| 11 |
+
model = whisper.load_model("base")
|
| 12 |
+
print("Whisper ready!")
|
| 13 |
+
|
| 14 |
+
@app.get("/")
|
| 15 |
+
def read_root():
|
| 16 |
+
return {"status": "Silent Utils API is ONLINE"}
|
| 17 |
+
|
| 18 |
+
@app.get("/api/download")
|
| 19 |
+
def download_media(url: str):
|
| 20 |
+
"""Bypasses protections and gets the direct raw MP4/MP3 link."""
|
| 21 |
+
ydl_opts = {
|
| 22 |
+
'format': 'best',
|
| 23 |
+
'quiet': True,
|
| 24 |
+
'no_warnings': True,
|
| 25 |
+
'skip_download': True # We just want the URL to give to the WhatsApp bot
|
| 26 |
+
}
|
| 27 |
+
try:
|
| 28 |
+
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 29 |
+
info = ydl.extract_info(url, download=False)
|
| 30 |
+
return {
|
| 31 |
+
"success": True,
|
| 32 |
+
"title": info.get('title'),
|
| 33 |
+
"duration": info.get('duration'),
|
| 34 |
+
"thumbnail": info.get('thumbnail'),
|
| 35 |
+
"download_url": info.get('url') # The direct raw video/audio link!
|
| 36 |
+
}
|
| 37 |
+
except Exception as e:
|
| 38 |
+
raise HTTPException(status_code=400, detail=str(e))
|
| 39 |
+
|
| 40 |
+
@app.post("/api/transcribe")
|
| 41 |
+
async def transcribe_audio(file: UploadFile = File(...)):
|
| 42 |
+
"""Converts WhatsApp voice notes to text."""
|
| 43 |
+
try:
|
| 44 |
+
# Save the uploaded WhatsApp audio temporarily
|
| 45 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".ogg") as temp_audio:
|
| 46 |
+
temp_audio.write(await file.read())
|
| 47 |
+
temp_audio_path = temp_audio.name
|
| 48 |
+
|
| 49 |
+
# Whisper AI transcribes it to text
|
| 50 |
+
result = model.transcribe(temp_audio_path)
|
| 51 |
+
|
| 52 |
+
# Clean up
|
| 53 |
+
os.remove(temp_audio_path)
|
| 54 |
+
|
| 55 |
+
return {"success": True, "text": result["text"].strip()}
|
| 56 |
+
except Exception as e:
|
| 57 |
+
raise HTTPException(status_code=500, detail=str(e))
|