z2learn commited on
Commit
6be8085
·
verified ·
1 Parent(s): aa40a2c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -15
app.py CHANGED
@@ -3,7 +3,7 @@ import uuid
3
  import shutil
4
  import subprocess
5
  from fastapi import FastAPI, File, UploadFile, Form, HTTPException
6
- from fastapi.responses import FileResponse
7
  from fastapi.middleware.cors import CORSMiddleware
8
  import tempfile
9
 
@@ -12,7 +12,7 @@ app = FastAPI()
12
  # Add CORS middleware to allow requests from your frontend
13
  app.add_middleware(
14
  CORSMiddleware,
15
- allow_origins=["*"], # Set this to your frontend URL in production
16
  allow_credentials=True,
17
  allow_methods=["*"],
18
  allow_headers=["*"],
@@ -54,9 +54,7 @@ async def burn_subtitles(
54
 
55
  # Ensure font color is in the correct format for FFmpeg
56
  if font_color.startswith('#'):
57
- # Convert hex to FFmpeg compatible format
58
  font_color = font_color.lstrip('#')
59
- # Convert RGB to BGR which is what FFmpeg expects
60
  if len(font_color) == 6:
61
  r, g, b = font_color[0:2], font_color[2:4], font_color[4:6]
62
  font_color = f"&H{b}{g}{r}&"
@@ -71,7 +69,6 @@ async def burn_subtitles(
71
  output_path
72
  ]
73
 
74
- # Execute FFmpeg command
75
  process = subprocess.run(command, capture_output=True, text=True)
76
 
77
  if process.returncode != 0:
@@ -85,13 +82,11 @@ async def burn_subtitles(
85
  )
86
 
87
  except Exception as e:
88
- # Clean up in case of errors
89
  if os.path.exists(job_dir):
90
  shutil.rmtree(job_dir)
91
  raise HTTPException(status_code=500, detail=str(e))
92
 
93
  finally:
94
- # Clean up temporary files (you might want to do this after a delay in production)
95
  if os.path.exists(job_dir):
96
  shutil.rmtree(job_dir)
97
 
@@ -105,14 +100,11 @@ async def convert_srt(
105
  os.makedirs(job_dir, exist_ok=True)
106
 
107
  try:
108
- # Save uploaded file
109
  subtitle_path = os.path.join(job_dir, "subtitles.srt")
110
 
111
- # Write the file to disk
112
  with open(subtitle_path, "wb") as subtitle_file:
113
  shutil.copyfileobj(subtitle.file, subtitle_file)
114
 
115
- # Return the SRT file
116
  return FileResponse(
117
  subtitle_path,
118
  media_type="text/plain",
@@ -123,15 +115,13 @@ async def convert_srt(
123
  raise HTTPException(status_code=500, detail=str(e))
124
 
125
  finally:
126
- # Clean up temporary files
127
  if os.path.exists(job_dir):
128
  shutil.rmtree(job_dir)
129
 
130
- # Add a health check endpoint
131
  @app.get("/health")
132
  def health_check():
133
  return {"status": "healthy"}
134
 
135
- @app.get("/home")
136
- def home():
137
- return {"message": "running!"}
 
3
  import shutil
4
  import subprocess
5
  from fastapi import FastAPI, File, UploadFile, Form, HTTPException
6
+ from fastapi.responses import FileResponse, RedirectResponse
7
  from fastapi.middleware.cors import CORSMiddleware
8
  import tempfile
9
 
 
12
  # Add CORS middleware to allow requests from your frontend
13
  app.add_middleware(
14
  CORSMiddleware,
15
+ allow_origins=["*"], # In production, set this to your frontend URL
16
  allow_credentials=True,
17
  allow_methods=["*"],
18
  allow_headers=["*"],
 
54
 
55
  # Ensure font color is in the correct format for FFmpeg
56
  if font_color.startswith('#'):
 
57
  font_color = font_color.lstrip('#')
 
58
  if len(font_color) == 6:
59
  r, g, b = font_color[0:2], font_color[2:4], font_color[4:6]
60
  font_color = f"&H{b}{g}{r}&"
 
69
  output_path
70
  ]
71
 
 
72
  process = subprocess.run(command, capture_output=True, text=True)
73
 
74
  if process.returncode != 0:
 
82
  )
83
 
84
  except Exception as e:
 
85
  if os.path.exists(job_dir):
86
  shutil.rmtree(job_dir)
87
  raise HTTPException(status_code=500, detail=str(e))
88
 
89
  finally:
 
90
  if os.path.exists(job_dir):
91
  shutil.rmtree(job_dir)
92
 
 
100
  os.makedirs(job_dir, exist_ok=True)
101
 
102
  try:
 
103
  subtitle_path = os.path.join(job_dir, "subtitles.srt")
104
 
 
105
  with open(subtitle_path, "wb") as subtitle_file:
106
  shutil.copyfileobj(subtitle.file, subtitle_file)
107
 
 
108
  return FileResponse(
109
  subtitle_path,
110
  media_type="text/plain",
 
115
  raise HTTPException(status_code=500, detail=str(e))
116
 
117
  finally:
 
118
  if os.path.exists(job_dir):
119
  shutil.rmtree(job_dir)
120
 
 
121
  @app.get("/health")
122
  def health_check():
123
  return {"status": "healthy"}
124
 
125
+ @app.get("/doc")
126
+ def redirect_to_docs():
127
+ return RedirectResponse(url="/docs")