tranquilTrill commited on
Commit
c9d8b75
·
verified ·
1 Parent(s): 8dcab93

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)
16
+ result = transcription["text"]
17
+ print("whisper result:")
18
+ print(result)
19
+ return result
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"}
27
+
28
+ try:
29
+ file_location = f"newfile.wav"
30
+ with open(file_location, "wb") as buffer:
31
+ shutil.copyfileobj(file.file, buffer)
32
+ result = transcribe_with_whisper(file_location)
33
+ os.remove(file_location)
34
+ return {"text": result}
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)