-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
66 lines (55 loc) · 2.04 KB
/
main.py
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
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from pydantic import BaseModel
import os
import tempfile
from youtube_transcriber import process_youtube_url
import logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s | %(levelname)8s | %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
logger = logging.getLogger(__name__)
app = FastAPI()
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:8000", "http://127.0.0.1:8000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
class TranscriptionRequest(BaseModel):
url: str
api_key: str
@app.post("/api/transcribe")
async def transcribe_video(request: TranscriptionRequest):
try:
os.environ["GEMINI_API_KEY"] = request.api_key
with tempfile.TemporaryDirectory() as temp_dir:
os.environ["AUDIO_OUTPUT_DIR"] = temp_dir
success, transcript_content = process_youtube_url(request.url)
if not success:
raise HTTPException(status_code=500, detail="Failed to process video")
# Clean up audio files
audio_dir = "/tmp/audio"
if os.path.exists(audio_dir):
for file in os.listdir(audio_dir):
file_path = os.path.join(audio_dir, file)
try:
if os.path.isfile(file_path):
os.remove(file_path)
except Exception as e:
logger.error(f"Error deleting file {file_path}: {str(e)}")
return JSONResponse({
"status": "success",
"message": "Video processed successfully",
"transcript": transcript_content
})
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/api/health")
async def health_check():
return {"status": "healthy"}