-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest3.py
51 lines (38 loc) · 1.22 KB
/
test3.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, File, UploadFile, HTTPException
from sqlalchemy import create_engine, Column, Integer, LargeBinary, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from pydantic import BaseModel
# SQLAlchemy setup
DATABASE_URL = "sqlite:///./test.db"
engine = create_engine(DATABASE_URL)
Base = declarative_base()
class Audio(Base):
__tablename__ = "audios"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, index=True)
data = Column(LargeBinary)
Base.metadata.create_all(bind=engine)
app = FastAPI()
class AudioUpload(BaseModel):
name: str
audio: UploadFile
@app.post("/upload/")
async def upload_audio(audio_data: AudioUpload):
db = SessionLocal()
try:
audio_content = audio_data.audio.file.read()
db_audio = Audio(name=audio_data.name, data=audio_content)
db.add(db_audio)
db.commit()
return {"message": "Audio uploaded successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail="Internal Server Error")
finally:
db.close()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()