Fdgg55's picture
Update app.py
2373b18 verified
Raw
History Blame Contribute Delete
9.05 kB
from fastapi import FastAPI, UploadFile, File, HTTPException
from fastapi.responses import Response, HTMLResponse
import yt_dlp
import whisper
import tempfile
import os
import io
import urllib.request
import urllib.parse
import json
from diffusers import StableDiffusionPipeline
# --- 1. METADATA & NEON UI ---
api_description = """
<div style="text-align: center; margin-top: 20px; border-bottom: 1px solid #333; padding-bottom: 20px;">
<img src="https://i.ibb.co/C5nyyyXH/cccfe44a8d63663a60eed6f0300a8b44.jpg" width="150" style="border-radius: 15px; box-shadow: 0 0 20px rgba(0, 255, 204, 0.6); border: 2px solid #00ffcc;">
<h2 style="color: #00ffcc; text-shadow: 0 0 10px #00ffcc; margin-top: 15px; font-family: monospace;">SILENT TECH UTILITY ENGINE</h2>
</div>
<br>
<div style="color: #a0aec0; font-size: 15px; text-align: center;">
This is the <b>Private Backend API</b> for the Silent Bot network.<br>
Completely independent, uncensored, and running on a dedicated 16GB RAM Linux container.
</div>
"""
tags_metadata = [
{"name": "System", "description": "Server health status."},
{"name": "Media Downloader", "description": "Extract raw media URLs from social platforms."},
{"name": "Voice AI", "description": "Transcribe audio files perfectly."},
{"name": "Image AI", "description": "Generate uncensored images."}
]
app = FastAPI(
title="SILENT TECH API",
description=api_description,
version="2.0.0",
openapi_tags=tags_metadata,
docs_url=None,
redoc_url=None
)
@app.get("/docs", include_in_schema=False)
async def neon_swagger_ui():
html = """
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Silent Tech API - NEON UI</title>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui.css" />
<link rel="icon" type="image/png" href="https://i.ibb.co/C5nyyyXH/cccfe44a8d63663a60eed6f0300a8b44.jpg" />
<style>
body { background-color: #050505 !important; color: #00ffcc !important; font-family: 'Segoe UI', Tahoma, sans-serif; }
.swagger-ui .info .title { color: #00ffcc !important; text-shadow: 0 0 10px rgba(0, 255, 204, 0.7); }
.swagger-ui .info p { color: #a0aec0 !important; }
.swagger-ui .info h1, .swagger-ui .info h2, .swagger-ui .info h3, .swagger-ui .info h4, .swagger-ui .info h5 { color: #00ffcc !important; }
.swagger-ui .info a { color: #bd00ff !important; text-shadow: 0 0 8px rgba(189, 0, 255, 0.7); }
.swagger-ui .scheme-container { background-color: #0a0a0a !important; box-shadow: 0 0 15px rgba(0, 255, 204, 0.1); border-bottom: 1px solid #00ffcc; }
.swagger-ui .opblock.opblock-get { background: rgba(0, 255, 204, 0.05) !important; border: 1px solid #00ffcc !important; box-shadow: 0 0 10px rgba(0, 255, 204, 0.2); border-radius: 8px; }
.swagger-ui .opblock.opblock-get .opblock-summary-method { background: #00ffcc !important; color: #000 !important; font-weight: bold; }
.swagger-ui .opblock.opblock-post { background: rgba(189, 0, 255, 0.05) !important; border: 1px solid #bd00ff !important; box-shadow: 0 0 10px rgba(189, 0, 255, 0.2); border-radius: 8px; }
.swagger-ui .opblock.opblock-post .opblock-summary-method { background: #bd00ff !important; color: #fff !important; font-weight: bold; }
.swagger-ui .btn.execute { background-color: #00ffcc !important; color: #000 !important; border: none !important; box-shadow: 0 0 15px rgba(0, 255, 204, 0.6) !important; font-weight: bold; transition: 0.3s; }
.swagger-ui .btn.execute:hover { box-shadow: 0 0 25px rgba(0, 255, 204, 1) !important; }
.swagger-ui .btn { color: #00ffcc !important; border-color: #00ffcc !important; }
.swagger-ui .opblock-body pre.microlight { background-color: #000 !important; border: 1px solid #333 !important; border-radius: 8px; color: #fff !important; }
.swagger-ui input[type=text], .swagger-ui input[type=file] { background: #000 !important; color: #00ffcc !important; border: 1px solid #bd00ff !important; border-radius: 4px; padding: 5px; }
.swagger-ui .responses-inner h4, .swagger-ui .responses-inner h5 { color: #00ffcc !important; }
.swagger-ui svg { fill: #00ffcc !important; }
.swagger-ui section.models { border: 1px solid #333 !important; background: #0a0a0a !important; border-radius: 8px;}
.swagger-ui section.models h4 { color: #00ffcc !important; border-bottom: 1px solid #333; }
</style>
</head>
<body>
<div id="swagger-ui"></div>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js"></script>
<script src="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-standalone-preset.js"></script>
<script>
window.onload = function() {
window.ui = SwaggerUIBundle({
url: "/openapi.json",
dom_id: '#swagger-ui',
deepLinking: true,
presets: [ SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset ],
layout: "BaseLayout"
});
};
</script>
</body>
</html>
"""
return HTMLResponse(html)
# --- 2. LOAD AI MODELS ---
print("Loading Whisper Voice AI...")
voice_model = whisper.load_model("base")
print("Loading Uncensored Image AI...")
image_model = StableDiffusionPipeline.from_pretrained("prompthero/openjourney", safety_checker=None)
image_model.to("cpu")
# --- 3. API ENDPOINTS ---
@app.get("/", tags=["System"])
def read_root():
return {"status": "Silent Utils API is ONLINE", "version": "2.0.0"}
@app.get("/api/download", tags=["Media Downloader"])
def download_media(url: str):
"""Bypasses protections and gets the direct raw MP4/MP3 link."""
clean_url = url.strip()
# 💥 FIREWALL BYPASS: If it's YouTube, route it through an external proxy API!
if "youtube.com" in clean_url or "youtu.be" in clean_url:
try:
bypass_url = f"https://api.bk9.site/yt/mp4?url={urllib.parse.quote(clean_url)}"
req = urllib.request.Request(bypass_url, headers={'User-Agent': 'Mozilla/5.0'})
with urllib.request.urlopen(req) as response:
data = json.loads(response.read().decode())
if data.get("status") and "BK9" in data:
return {
"success": True,
"title": data["BK9"].get("title", "YouTube Video"),
"download_url": data["BK9"].get("url")
}
except Exception as bypass_e:
print("Bypass failed:", str(bypass_e))
# If bypass fails, fall back to yt-dlp just in case
# Standard yt-dlp for TikTok, Instagram, Twitter, etc.
ydl_opts = {
'format': 'best',
'quiet': True,
'no_warnings': True,
'skip_download': True,
'nocheckcertificate': True
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(clean_url, download=False)
if 'entries' in info:
info = info['entries'][0]
final_url = info.get('url')
if not final_url and 'requested_downloads' in info:
final_url = info['requested_downloads'][0].get('url')
if not final_url:
raise Exception("Could not extract the raw video URL.")
return {
"success": True,
"title": info.get('title', 'Silent Media'),
"download_url": final_url
}
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.post("/api/transcribe", tags=["Voice AI"])
async def transcribe_audio(file: UploadFile = File(...)):
"""Converts WhatsApp voice notes (or any audio) to text."""
try:
with tempfile.NamedTemporaryFile(delete=False, suffix=".ogg") as temp_audio:
temp_audio.write(await file.read())
temp_audio_path = temp_audio.name
result = voice_model.transcribe(temp_audio_path)
os.remove(temp_audio_path)
return {"success": True, "text": result["text"].strip()}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/image", tags=["Image AI"])
def generate_image(prompt: str):
"""Generates an uncensored image and returns it directly as a PNG."""
try:
image = image_model(prompt, num_inference_steps=8, height=384, width=384).images[0]
img_bytes = io.BytesIO()
image.save(img_bytes, format="PNG")
return Response(content=img_bytes.getvalue(), media_type="image/png")
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))