Spaces:
Bagda
/
Build error

File size: 9,848 Bytes
2ac06dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
303bd7e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae82ef0
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from fastapi import FastAPI, Request, Query, BackgroundTasks
from fastapi.responses import FileResponse, JSONResponse, StreamingResponse
from fastapi.middleware.cors import CORSMiddleware
import yt_dlp
import os
import asyncio
from datetime import datetime, timedelta
from urllib.parse import quote
import io
import logging
import json
import time
from collections import defaultdict
import uvicorn
import gradio as gr

OUTPUT_DIR = "output"
os.makedirs(OUTPUT_DIR, exist_ok=True)
BANNED_IP_FILE = 'bannedip.json'
MAX_REQUESTS_PER_MINUTE = 35

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

server_start_time = time.time()
total_request_count = 0
request_counter = defaultdict(list)

def load_banned_ips():
    try:
        with open(BANNED_IP_FILE, 'r') as f:
            return set(json.load(f))
    except (FileNotFoundError, json.JSONDecodeError):
        return set()

def save_banned_ips(banned_ips):
    with open(BANNED_IP_FILE, 'w') as f:
        json.dump(list(banned_ips), f)

async def delete_file_after_delay(file_path: str, delay: int = 600):
    await asyncio.sleep(delay)
    try:
        os.remove(file_path)
        logger.info(f"Deleted file {file_path} after {delay} seconds.")
    except FileNotFoundError:
        logger.warning(f"File {file_path} not found during deletion.")
    except Exception as e:
        logger.error(f"Error deleting file {file_path}: {e}", exc_info=True)

app = FastAPI(
    title="YouTube Downloader API",
    description="API to download video/audio from YouTube.",
    version="1.0.0"
)

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.middleware("http")
async def log_requests(request: Request, call_next):
    global total_request_count
    total_request_count += 1

    client_ip = request.headers.get('X-Forwarded-For', request.client.host).split(',')[0].strip()
    banned_ips = load_banned_ips()

    if client_ip in banned_ips:
        return JSONResponse(status_code=403, content={"message": "Blocked due to excessive requests."})

    now = datetime.now()
    request_counter[client_ip].append(now)
    request_counter[client_ip] = [t for t in request_counter[client_ip] if t > now - timedelta(minutes=1)]

    if len(request_counter[client_ip]) > MAX_REQUESTS_PER_MINUTE:
        logger.warning(f"IP {client_ip} is blocked due to rate limiting.")
        banned_ips.add(client_ip)
        save_banned_ips(banned_ips)
        request_counter[client_ip] = []

    logger.info(f"{client_ip} requested {request.method} {request.url}")
    response = await call_next(request)
    return response

@app.get("/", summary="Root Endpoint")
async def root():
    return JSONResponse(status_code=200, content={"status": "Server Running"})

@app.get("/ytv/", summary="Download YouTube Video")
async def download_video(
    background_tasks: BackgroundTasks,
    url: str = Query(...),
    quality: int = Query(720),
    mode: str = Query("url")
):
    if mode not in ["url", "buffer"]:
        return JSONResponse(status_code=400, content={"error": "Invalid mode. Use 'url' or 'buffer'."})

    try:
        resx = f"_{quality}p"
        ydl_opts = {
            'format': f'bestvideo[height<={quality}]+bestaudio/best',
            'outtmpl': os.path.join(OUTPUT_DIR, '%(title)s' + resx + '.%(ext)s'),
            'merge_output_format': 'mp4',
            'cookiefile': 'yt.txt',
            'postprocessors': [{
                'key': 'FFmpegVideoConvertor',
                'preferedformat': 'mp4'
            }]
        }

        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=True)
            file_path = ydl.prepare_filename(info)

        if not os.path.exists(file_path):
            raise FileNotFoundError(f"File not found after download: {file_path}")

        background_tasks.add_task(delete_file_after_delay, file_path)

        if mode == "url":
            return {
                "status": "success",
                "title": info['title'],
                "thumb": info.get("thumbnail"),
                "url": f"https://lordxdd-ytdlp-py.hf.space/cdn/video/{quote(os.path.basename(file_path))}"
            }

        media_type = "video/mp4"
        with open(file_path, "rb") as f:
            return StreamingResponse(
                io.BytesIO(f.read()),
                media_type=media_type,
                headers={"Content-Disposition": f"attachment; filename={os.path.basename(file_path)}"}
            )

    except Exception as e:
        logger.error(f"Download error: {e}", exc_info=True)
        return JSONResponse(status_code=500, content={"error": str(e)})

@app.get("/cdn/video/{filename}", summary="Serve Downloaded File")
async def download_file(filename: str):
    file_path = os.path.join(OUTPUT_DIR, filename)
    if os.path.exists(file_path):
        return FileResponse(file_path, filename=filename)
    return JSONResponse(status_code=404, content={"error": "File not found"})

@app.get("/yta/", summary="Download YouTube Audio (.webm)")
async def download_audio(
    background_tasks: BackgroundTasks,
    url: str = Query(..., description="YouTube video URL"),
    mode: str = Query("url", description="Response mode: 'url' or 'buffer'")
):
    if mode not in ["url", "buffer"]:
        return JSONResponse(status_code=400, content={"error": "Invalid mode. Use 'url' or 'buffer'."})
    try:
        ydl_opts = {
            'format': 'bestaudio/best',
            'outtmpl': os.path.join(OUTPUT_DIR, '%(title)s.%(ext)s'),
            'cookiefile': 'yt.txt',
        }
        with yt_dlp.YoutubeDL(ydl_opts) as ydl:
            info = ydl.extract_info(url, download=True)
            file_path = ydl.prepare_filename(info)
        if not os.path.exists(file_path):
            raise FileNotFoundError(f"File not found after download: {file_path}")
        background_tasks.add_task(delete_file_after_delay, file_path)
        if mode == "url":
            return {
                "status": "success",
                "title": info.get("title"),
                "thumb": info.get("thumbnail"),
                "url": f"https://lordxdd-ytdlp-py.hf.space/cdn/audio/{quote(os.path.basename(file_path))}"
            }

        media_type = "audio/webm"
        with open(file_path, "rb") as f:
            return StreamingResponse(
                io.BytesIO(f.read()),
                media_type=media_type,
                headers={"Content-Disposition": f"attachment; filename={os.path.basename(file_path)}"}
            )

    except Exception as e:
        logger.error(f"Download error: {e}", exc_info=True)
        return JSONResponse(status_code=500, content={"error": str(e)})

@app.get("/cdn/audio/{filename}", summary="Serve Downloaded Audio File")
async def serve_audio_file(filename: str):
    file_path = os.path.join(OUTPUT_DIR, filename)
    if os.path.exists(file_path):
        ext = filename.split(".")[-1]
        return FileResponse(file_path, filename=filename, media_type=f"audio/{ext}")
    return JSONResponse(status_code=404, content={"error": "File not found"})

def run_gradio_ui():
    def download_gradio(url, resolution):
        try:
            quality = int(resolution)
            ydl_opts = {
                'format': f'bestvideo[height<={quality}]+bestaudio/best',
                'outtmpl': os.path.join(OUTPUT_DIR, '%(title)s' + f'_{quality}p' + '.%(ext)s'),
                'merge_output_format': 'mp4',
                'postprocessors': [{
                    'key': 'FFmpegVideoConvertor',
                    'preferedformat': 'mp4'
                }]
            }
            with yt_dlp.YoutubeDL(ydl_opts) as ydl:
                info = ydl.extract_info(url, download=True)
                file_path = ydl.prepare_filename(info)
            return {
                "status": "success",
                "title": info['title'],
                "url": f"/cdn/video/{quote(os.path.basename(file_path))}"
            }
        except Exception as e:
            return {"error": str(e)}

    interface = gr.Interface(
        fn=download_gradio,
        inputs=["text", gr.Slider(240, 1080)],
        outputs="json",
        title="YouTube Video Downloader"
    )
    interface.launch()

if __name__ == "__main__":
    import sys
    if "gradio" in sys.argv:
        run_gradio_ui()
    else:
        uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True)
        import whisper
from transformers import pipeline
from gtts import gTTS
import gradio as gr
import os

# Load Whisper model
whisper_model = whisper.load_model("small")

# Load translation pipeline (English to Hindi)
translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")

def transcribe_translate_dub(audio_path):
    # Step 1: Transcribe audio using Whisper
    result = whisper_model.transcribe(audio_path)
    english_text = result["text"]

    # Step 2: Translate English to Hindi
    translated = translator(english_text)[0]["translation_text"]

    # Step 3: Text-to-Speech (Hindi)
    tts = gTTS(translated, lang='hi')
    tts.save("dubbed_audio.mp3")

    return translated, "dubbed_audio.mp3"

# Gradio UI
gr.Interface(
    fn=transcribe_translate_dub,
    inputs=gr.Audio(source="upload", type="filepath", label="Upload English Audio"),
    outputs=[
        gr.Textbox(label="Hindi Translation"),
        gr.Audio(label="Hindi Dubbed Audio")
    ],
    title="🎙️ English to Hindi Video Dubbing AI",
    description="Upload English audio, get Hindi translation and dubbed voice."
).launch()
from transformers import pipeline

translator = pipeline("translation", model="Helsinki-NLP/opus-mt-en-hi")
translated_text = translator("Hello, how are you?")[0]['translation_text']