Arafath10 commited on
Commit
d385323
1 Parent(s): daee448

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +24 -16
main.py CHANGED
@@ -1,16 +1,17 @@
1
  from fastapi import FastAPI, File, UploadFile
2
  from fastapi.responses import StreamingResponse
3
- from fastapi.middleware.cors import CORSMiddleware
4
  import os
5
  import io
6
 
7
  app = FastAPI()
8
 
9
- app.add_middleware(
10
- CORSMiddleware,
11
- allow_origins=['*']
12
- )
13
 
 
 
 
 
14
 
15
  @app.get("/")
16
  def read_root():
@@ -18,14 +19,21 @@ def read_root():
18
 
19
  @app.post("/uploadfile/")
20
  async def create_upload_file(file: UploadFile = File(...)):
21
- # Save the file with a specific name
22
- file_path = "inputvoice.mp3"
23
- with open(file_path, "wb") as f:
24
- f.write(file.file.read())
25
-
26
- # Read the content of the saved file
27
- with open(file_path, "rb") as f:
28
- file_content = f.read()
29
-
30
- # Return the content as a streaming response
31
- return StreamingResponse(io.BytesIO(file_content), media_type="audio/mpeg", headers={"Content-Disposition": "inline; filename=inputvoice.mp3"})
 
 
 
 
 
 
 
 
1
  from fastapi import FastAPI, File, UploadFile
2
  from fastapi.responses import StreamingResponse
 
3
  import os
4
  import io
5
 
6
  app = FastAPI()
7
 
8
+ # Define the directory to store uploaded files
9
+ UPLOAD_DIR = "/content/"
 
 
10
 
11
+ @app.on_event("startup")
12
+ async def startup_event():
13
+ # Check and create the directory if it doesn't exist
14
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
15
 
16
  @app.get("/")
17
  def read_root():
 
19
 
20
  @app.post("/uploadfile/")
21
  async def create_upload_file(file: UploadFile = File(...)):
22
+ try:
23
+ # Save the file with a specific name
24
+ file_path = os.path.join(UPLOAD_DIR, "inputvoice.mp3")
25
+
26
+ with open(file_path, "wb") as f:
27
+ f.write(file.file.read())
28
+
29
+ # Read the content of the saved file
30
+ with open(file_path, "rb") as f:
31
+ file_content = f.read()
32
+
33
+ # Return the content as a streaming response
34
+ return StreamingResponse(io.BytesIO(file_content), media_type="audio/mpeg", headers={"Content-Disposition": "inline; filename=inputvoice.mp3"})
35
+
36
+ except PermissionError as e:
37
+ return {"error": f"PermissionError: {str(e)}"}
38
+
39
+