-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (58 loc) · 1.85 KB
/
main.py
File metadata and controls
69 lines (58 loc) · 1.85 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI, UploadFile, Form, Depends, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse
from sqlalchemy.orm import Session
import models
import database
import shutil
import os
from typing import List
from pydantic import BaseModel
app = FastAPI()
# Enable CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["http://localhost:3000"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Create tables
models.Base.metadata.create_all(bind=database.engine)
print("Database tables created successfully!")
UPLOAD_DIR = "uploads"
if not os.path.exists(UPLOAD_DIR):
os.makedirs(UPLOAD_DIR)
class SaveResponse(BaseModel):
status: str
message: str
@app.post("/save")
async def save_audio_transcript(
audio: UploadFile,
transcript: str = Form(...),
db: Session = Depends(database.get_db)
):
try:
# Validate file type
if not audio.filename.endswith(('.mp3', '.wav', '.m4a')):
raise HTTPException(status_code=400, detail="Invalid audio file format")
# Save audio file
file_location = f"{UPLOAD_DIR}/{audio.filename}"
with open(file_location, "wb+") as file_object:
shutil.copyfileobj(audio.file, file_object)
# Save to database
db_record = models.AudioTranscript(
audio_filename=audio.filename,
transcript=transcript
)
db.add(db_record)
db.commit()
return {
"status": "success",
"message": "Recording saved successfully"
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def root():
return {"message": "API is working"}