Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,21 @@
|
|
| 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 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
print("
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
@app.get("/")
|
| 15 |
def read_root():
|
|
@@ -18,21 +24,14 @@ def read_root():
|
|
| 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 |
-
"
|
| 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))
|
|
@@ -41,17 +40,28 @@ def download_media(url: str):
|
|
| 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 |
-
|
| 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))
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
from fastapi.responses import Response
|
| 3 |
import yt_dlp
|
| 4 |
import whisper
|
| 5 |
import tempfile
|
| 6 |
import os
|
| 7 |
+
import io
|
| 8 |
+
from diffusers import StableDiffusionPipeline
|
| 9 |
|
| 10 |
app = FastAPI(title="Silent Tech Utils API")
|
| 11 |
|
| 12 |
+
print("Loading Whisper Voice AI...")
|
| 13 |
+
voice_model = whisper.load_model("base")
|
| 14 |
+
|
| 15 |
+
print("Loading Uncensored Image AI...")
|
| 16 |
+
# safety_checker=None completely disables the NSFW and violence filters!
|
| 17 |
+
image_model = StableDiffusionPipeline.from_pretrained("prompthero/openjourney", safety_checker=None)
|
| 18 |
+
image_model.to("cpu")
|
| 19 |
|
| 20 |
@app.get("/")
|
| 21 |
def read_root():
|
|
|
|
| 24 |
@app.get("/api/download")
|
| 25 |
def download_media(url: str):
|
| 26 |
"""Bypasses protections and gets the direct raw MP4/MP3 link."""
|
| 27 |
+
ydl_opts = {'format': 'best', 'quiet': True, 'no_warnings': True, 'skip_download': True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
try:
|
| 29 |
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
|
| 30 |
info = ydl.extract_info(url, download=False)
|
| 31 |
return {
|
| 32 |
"success": True,
|
| 33 |
"title": info.get('title'),
|
| 34 |
+
"download_url": info.get('url') # The direct raw link!
|
|
|
|
|
|
|
| 35 |
}
|
| 36 |
except Exception as e:
|
| 37 |
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
| 40 |
async def transcribe_audio(file: UploadFile = File(...)):
|
| 41 |
"""Converts WhatsApp voice notes to text."""
|
| 42 |
try:
|
|
|
|
| 43 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".ogg") as temp_audio:
|
| 44 |
temp_audio.write(await file.read())
|
| 45 |
temp_audio_path = temp_audio.name
|
| 46 |
+
|
| 47 |
+
result = voice_model.transcribe(temp_audio_path)
|
|
|
|
|
|
|
|
|
|
| 48 |
os.remove(temp_audio_path)
|
|
|
|
| 49 |
return {"success": True, "text": result["text"].strip()}
|
| 50 |
+
except Exception as e:
|
| 51 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 52 |
+
|
| 53 |
+
@app.get("/api/image")
|
| 54 |
+
def generate_image(prompt: str):
|
| 55 |
+
"""Generates an uncensored image and returns it directly as a PNG."""
|
| 56 |
+
try:
|
| 57 |
+
# Generate the image (15 steps is faster for CPU)
|
| 58 |
+
image = image_model(prompt, num_inference_steps=15).images[0]
|
| 59 |
+
|
| 60 |
+
# Convert it to a raw PNG image file
|
| 61 |
+
img_bytes = io.BytesIO()
|
| 62 |
+
image.save(img_bytes, format="PNG")
|
| 63 |
+
|
| 64 |
+
# Send the actual image picture directly to the bot, not just text!
|
| 65 |
+
return Response(content=img_bytes.getvalue(), media_type="image/png")
|
| 66 |
except Exception as e:
|
| 67 |
raise HTTPException(status_code=500, detail=str(e))
|