tranquilTrill commited on
Commit
778ce83
·
verified ·
1 Parent(s): 6f48e41

transcribe video added

Browse files
Files changed (1) hide show
  1. app.py +53 -4
app.py CHANGED
@@ -1,15 +1,19 @@
1
 
2
  from fastapi import FastAPI, UploadFile, File
 
3
  import uvicorn
4
  from fastapi.middleware.cors import CORSMiddleware
5
  import whisper
6
  import shutil
7
  import os
 
 
8
 
9
  app = FastAPI(swagger_ui_parameters={"syntaxHighlight": {"theme": "obsidian"}})
10
- origins = [ "http://localhost", "http://localhost:3000", "http://localhost:3006"]
11
  app.add_middleware(CORSMiddleware, allow_origins=origins,allow_credentials=True,allow_methods=["*"], allow_headers=["*"])
12
  model = whisper.load_model("base")
 
13
  def transcribe_with_whisper(fpath):
14
  try:
15
  transcription = model.transcribe(fpath)
@@ -20,7 +24,7 @@ def transcribe_with_whisper(fpath):
20
  except Exception as e:
21
  return str(e)
22
 
23
- @app.post("/transcribe")
24
  async def transcribe(file: UploadFile = File(...)):
25
  if not file:
26
  return {"text": "No file sent"}
@@ -35,7 +39,52 @@ async def transcribe(file: UploadFile = File(...)):
35
 
36
  except Exception as e:
37
  return {"text" : str(e)}
38
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
  if __name__ == "__main__":
41
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
 
2
  from fastapi import FastAPI, UploadFile, File
3
+ from fastapi.responses import JSONResponse
4
  import uvicorn
5
  from fastapi.middleware.cors import CORSMiddleware
6
  import whisper
7
  import shutil
8
  import os
9
+ import moviepy.editor as mp
10
+ import uuid
11
 
12
  app = FastAPI(swagger_ui_parameters={"syntaxHighlight": {"theme": "obsidian"}})
13
+ origins = [ "*"]
14
  app.add_middleware(CORSMiddleware, allow_origins=origins,allow_credentials=True,allow_methods=["*"], allow_headers=["*"])
15
  model = whisper.load_model("base")
16
+
17
  def transcribe_with_whisper(fpath):
18
  try:
19
  transcription = model.transcribe(fpath)
 
24
  except Exception as e:
25
  return str(e)
26
 
27
+ @app.post("/transcribe_audio")
28
  async def transcribe(file: UploadFile = File(...)):
29
  if not file:
30
  return {"text": "No file sent"}
 
39
 
40
  except Exception as e:
41
  return {"text" : str(e)}
42
+
43
+
44
+ #region transcribe video
45
+
46
+ @app.post("/transcribe_video")
47
+ async def transcribe_video(file: UploadFile = File(...)):
48
+ # Create temporary paths
49
+ temp_video_path = f"/tmp/{uuid.uuid4()}_{file.filename}"
50
+ temp_audio_path = temp_video_path.rsplit(".", 1)[0] + ".wav"
51
+
52
+ # Save uploaded file
53
+ with open(temp_video_path, "wb") as f:
54
+ content = await file.read()
55
+ f.write(content)
56
+
57
+ try:
58
+ # Extract and transcribe
59
+ extract_audio_from_video(temp_video_path, temp_audio_path)
60
+ transcript = transcribe_audio_to_text(temp_audio_path)
61
+
62
+ return JSONResponse(content={
63
+ "video": file.filename,
64
+ "transcript": transcript
65
+ })
66
+
67
+ except Exception as e:
68
+ return JSONResponse(status_code=500, content={"error": str(e)})
69
+
70
+ finally:
71
+ # Cleanup
72
+ if os.path.exists(temp_video_path):
73
+ os.remove(temp_video_path)
74
+ if os.path.exists(temp_audio_path):
75
+ os.remove(temp_audio_path)
76
+
77
+ def extract_audio_from_video(video_path: str, audio_path: str):
78
+ clip = mp.VideoFileClip(video_path)
79
+ clip.audio.write_audiofile(audio_path)
80
+
81
+ def transcribe_audio_to_text(audio_path: str, model_size: str = "base") -> str:
82
+ model = whisper.load_model(model_size)
83
+ result = model.transcribe(audio_path)
84
+ transcript = "\n".join([seg["text"].strip() for seg in result["segments"]])
85
+ return transcript
86
+ #endregion transcribe video
87
+
88
 
89
  if __name__ == "__main__":
90
+ uvicorn.run(app, host="127.0.0.1", port=7860)