-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
51 lines (39 loc) · 1.56 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
from fastapi import FastAPI, Request
from router import upload_audio
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from fastapi import File, UploadFile
import os
from pydub import AudioSegment
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(upload_audio.router)
# templates= Jinja2Templates(directory="templates")
# app.mount('/static', StaticFiles(directory='static'), name='static')
# @app.get('/')
# async def name(request: Request):
# return templates.TemplateResponse("index.html", {"request":request,"name":"codingwithkashif"})
# Define the folder for storing recorded audio
recorded_audio_folder = "audios"
# @app.get("/")
# async def get_index(request: Request):
# return templates.TemplateResponse("test.html", {"request": request})
@app.post("/upload/")
async def upload_audio(file: UploadFile):
if file.content_type.startswith("audio/"):
filename = os.path.join(recorded_audio_folder, file.filename)
# Save the audio file
with open(filename, "wb") as audio_file:
audio_file.write(file.file.read())
if file.content_type != "audio/wav":
audio = AudioSegment.from_file(filename, format=file.content_type.split('/')[1])
audio.export(filename, format="wav")
return {"message": "Audio uploaded successfully."}
else:
return {"error": "Invalid audio file format."}