Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,149 +1,33 @@
|
|
| 1 |
-
from fastapi import FastAPI,
|
| 2 |
-
from fastapi.responses import
|
| 3 |
-
from
|
| 4 |
-
import
|
|
|
|
| 5 |
import uuid
|
| 6 |
-
import
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
from moviepy.editor import AudioFileClip
|
| 10 |
-
import whisper
|
| 11 |
-
from googletrans import Translator
|
| 12 |
-
from gtts import gTTS
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
logger = logging.getLogger(__name__)
|
| 17 |
|
| 18 |
-
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
logger.info("Whisper model loaded successfully")
|
| 27 |
-
except Exception as e:
|
| 28 |
-
logger.error(f"Failed to load whisper model: {e}")
|
| 29 |
-
model = None
|
| 30 |
|
| 31 |
-
|
|
|
|
|
|
|
| 32 |
|
| 33 |
-
|
| 34 |
-
url: str
|
| 35 |
-
timestamp: int = 0
|
| 36 |
-
chunk_size: int = 15
|
| 37 |
-
target_language: str = "en"
|
| 38 |
-
|
| 39 |
-
@app.post("/process-chunk/")
|
| 40 |
-
async def process_chunk(request: VideoRequest, background_tasks: BackgroundTasks):
|
| 41 |
-
request_id = str(uuid.uuid4())
|
| 42 |
-
audio_path = os.path.join(YOUTUBE_DIR, f"{request_id}.mp4")
|
| 43 |
-
chunk_path = os.path.join(YOUTUBE_DIR, f"{request_id}_chunk.mp3")
|
| 44 |
-
|
| 45 |
-
try:
|
| 46 |
-
# Download audio using pytube
|
| 47 |
-
yt = YouTube(request.url)
|
| 48 |
-
stream = yt.streams.filter(only_audio=True).first()
|
| 49 |
-
stream.download(output_path=YOUTUBE_DIR, filename=f"{request_id}.mp4")
|
| 50 |
-
|
| 51 |
-
# Extract audio chunk using moviepy
|
| 52 |
-
with AudioFileClip(audio_path) as audio:
|
| 53 |
-
start = request.timestamp
|
| 54 |
-
end = start + request.chunk_size
|
| 55 |
-
audio.subclip(start, end).write_audiofile(chunk_path, codec='mp3')
|
| 56 |
-
|
| 57 |
-
# Process audio chunk in background
|
| 58 |
-
background_tasks.add_task(
|
| 59 |
-
process_audio_chunk,
|
| 60 |
-
chunk_path,
|
| 61 |
-
request.target_language,
|
| 62 |
-
request_id
|
| 63 |
-
)
|
| 64 |
-
|
| 65 |
-
return {"request_id": request_id, "status": "processing"}
|
| 66 |
-
except Exception as e:
|
| 67 |
-
logger.error(f"Error processing chunk: {e}")
|
| 68 |
-
raise HTTPException(status_code=500, detail=str(e))
|
| 69 |
-
finally:
|
| 70 |
-
if os.path.exists(audio_path):
|
| 71 |
-
os.remove(audio_path)
|
| 72 |
-
|
| 73 |
-
async def process_audio_chunk(chunk_path, target_language, request_id):
|
| 74 |
-
try:
|
| 75 |
-
# Step 1: Transcribe
|
| 76 |
-
logger.info(f"Transcribing audio chunk: {chunk_path}")
|
| 77 |
-
result = model.transcribe(chunk_path)
|
| 78 |
-
transcription = result["text"]
|
| 79 |
-
|
| 80 |
-
# Step 2: Translate
|
| 81 |
-
logger.info(f"Translating text to {target_language}")
|
| 82 |
-
translation = translator.translate(transcription, dest=target_language).text
|
| 83 |
-
|
| 84 |
-
# Step 3: TTS
|
| 85 |
-
logger.info(f"Converting translation to speech")
|
| 86 |
-
tts_output_path = os.path.join(YOUTUBE_DIR, f"{request_id}_tts.mp3")
|
| 87 |
-
tts = gTTS(text=translation, lang=target_language)
|
| 88 |
-
tts.save(tts_output_path)
|
| 89 |
-
|
| 90 |
-
# Save translation text
|
| 91 |
-
text_output_path = os.path.join(YOUTUBE_DIR, f"{request_id}_text.txt")
|
| 92 |
-
with open(text_output_path, "w", encoding="utf-8") as f:
|
| 93 |
-
f.write(translation)
|
| 94 |
-
|
| 95 |
-
logger.info(f"Audio processing completed for request {request_id}")
|
| 96 |
-
except Exception as e:
|
| 97 |
-
logger.error(f"Error processing audio chunk: {e}")
|
| 98 |
-
finally:
|
| 99 |
-
if os.path.exists(chunk_path):
|
| 100 |
-
os.remove(chunk_path)
|
| 101 |
-
|
| 102 |
-
@app.get("/get-audio/{request_id}")
|
| 103 |
-
async def get_audio(request_id: str):
|
| 104 |
-
tts_output_path = os.path.join(YOUTUBE_DIR, f"{request_id}_tts.mp3")
|
| 105 |
-
if not os.path.exists(tts_output_path):
|
| 106 |
-
raise HTTPException(status_code=404, detail="Audio processing not completed yet or request ID invalid")
|
| 107 |
-
|
| 108 |
-
def iterfile():
|
| 109 |
-
with open(tts_output_path, "rb") as f:
|
| 110 |
-
yield from f
|
| 111 |
-
|
| 112 |
-
return StreamingResponse(
|
| 113 |
-
iterfile(),
|
| 114 |
-
media_type="audio/mpeg",
|
| 115 |
-
headers={"Content-Disposition": f"attachment; filename={request_id}.mp3"}
|
| 116 |
-
)
|
| 117 |
-
|
| 118 |
-
@app.get("/get-translation/{request_id}")
|
| 119 |
-
async def get_translation(request_id: str):
|
| 120 |
-
text_output_path = os.path.join(YOUTUBE_DIR, f"{request_id}_text.txt")
|
| 121 |
-
if not os.path.exists(text_output_path):
|
| 122 |
-
raise HTTPException(status_code=404, detail="Translation text not found or processing not completed")
|
| 123 |
-
with open(text_output_path, "r", encoding="utf-8") as f:
|
| 124 |
-
translation = f.read()
|
| 125 |
-
return {"request_id": request_id, "translation": translation}
|
| 126 |
-
|
| 127 |
-
@app.get("/status/{request_id}")
|
| 128 |
-
async def check_status(request_id: str):
|
| 129 |
-
tts_output_path = os.path.join(YOUTUBE_DIR, f"{request_id}_tts.mp3")
|
| 130 |
-
if os.path.exists(tts_output_path):
|
| 131 |
-
return {"status": "completed", "request_id": request_id}
|
| 132 |
-
else:
|
| 133 |
-
chunk_path = os.path.join(YOUTUBE_DIR, f"{request_id}_chunk.mp3")
|
| 134 |
-
if os.path.exists(chunk_path):
|
| 135 |
-
return {"status": "processing", "request_id": request_id}
|
| 136 |
-
else:
|
| 137 |
-
raise HTTPException(status_code=404, detail="Request ID not found")
|
| 138 |
-
|
| 139 |
-
@app.get("/")
|
| 140 |
-
async def root():
|
| 141 |
-
return {"message": "YouTube Streaming Translator API (No Cookies, No ffmpeg)"}
|
| 142 |
-
|
| 143 |
-
@app.get("/health")
|
| 144 |
-
async def health_check():
|
| 145 |
-
return {"status": "healthy"}
|
| 146 |
-
|
| 147 |
-
if __name__ == "__main__":
|
| 148 |
-
import uvicorn
|
| 149 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi.staticfiles import StaticFiles
|
| 4 |
+
from ultralytics import YOLO
|
| 5 |
+
import shutil
|
| 6 |
import uuid
|
| 7 |
+
import base64
|
| 8 |
+
import cv2
|
| 9 |
|
| 10 |
+
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
# Mount static frontend
|
| 13 |
+
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
|
|
|
| 14 |
|
| 15 |
+
# Load YOLOv8 model
|
| 16 |
+
model = YOLO("yolov8n.pt") # Use your own trained model if needed
|
| 17 |
|
| 18 |
+
@app.post("/detect")
|
| 19 |
+
async def detect(file: UploadFile = File(...)):
|
| 20 |
+
# Save image to disk
|
| 21 |
+
file_name = f"uploads/{uuid.uuid4()}.jpg"
|
| 22 |
+
with open(file_name, "wb") as buffer:
|
| 23 |
+
shutil.copyfileobj(file.file, buffer)
|
| 24 |
|
| 25 |
+
# Detect objects
|
| 26 |
+
results = model(file_name)
|
| 27 |
+
result_image = results[0].plot() # Get image with boxes
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# Encode image to base64 to send back
|
| 30 |
+
_, buffer = cv2.imencode('.jpg', result_image)
|
| 31 |
+
b64_encoded = base64.b64encode(buffer).decode('utf-8')
|
| 32 |
|
| 33 |
+
return JSONResponse(content={"image": b64_encoded})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|