Skip to content

Commit

Permalink
Added Endpoint for audio Converation
Browse files Browse the repository at this point in the history
  • Loading branch information
lukas-holzner committed Oct 1, 2024
1 parent f05e3a7 commit 8bfaa98
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
26 changes: 26 additions & 0 deletions backend/app/ai_manager.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import os
from dotenv import load_dotenv
from fastapi import UploadFile
from openai import OpenAI
from typing import List, Dict
import json
import io

# Load environment variables from .env file
load_dotenv()
Expand Down Expand Up @@ -134,3 +136,27 @@ def process_user_message(system_message: str, messages: List[str]) -> Dict[str,
break

print("Conversation ended.")

async def convert_audio_to_text(audio_file: UploadFile):
"""
Convert an uploaded audio file to text using OpenAI's Whisper model.
"""
try:
# Read the file content
file_content = await audio_file.read()

# Create a file-like object from the content
audio_file_obj = io.BytesIO(file_content)
audio_file_obj.name = audio_file.filename # Set the filename

# Call the OpenAI API to transcribe the audio
transcript = client.audio.transcriptions.create(
model="whisper-1",
file=audio_file_obj,
response_format="text"
)

return transcript
except Exception as e:
print(f"Error in audio transcription: {str(e)}")
return None
4 changes: 2 additions & 2 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
)

# Include the router with the /api prefix
app.include_router(meetings.router, tags=["meetings"])
app.include_router(users.router, tags=["users"])
app.include_router(meetings.router, tags=["Meetings"])
app.include_router(users.router, tags=["Users"])

@app.get("/", include_in_schema=False)
async def serve_index():
Expand Down
31 changes: 29 additions & 2 deletions backend/app/routers/meetings.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from datetime import datetime
from fastapi import APIRouter, Depends, HTTPException
from fastapi import APIRouter, Depends, HTTPException, UploadFile, File
from sqlalchemy.orm import Session
from typing import List
import os

from app.routers.users import get_user
from app.ai_manager import generate_initial_prompt, process_user_message
from app.ai_manager import generate_initial_prompt, process_user_message, convert_audio_to_text
from .. import schemas
from ..database import get_db
from ..models import Conversation, Meeting, ChatMessage, MeetingAgenda
Expand All @@ -17,6 +18,9 @@

router = APIRouter()

AUDIO_MIME_TYPES = ["audio/mpeg", "audio/mp3", "audio/wav", "audio/ogg"]
MAX_FILE_SIZE = 5 * 1024 * 1024 # 5 MB in bytes

# Meeting CRUD operations
def get_meeting(db: Session, meeting_id: int):
return db.query(Meeting).filter(Meeting.id == meeting_id).first()
Expand Down Expand Up @@ -192,6 +196,29 @@ def router_add_message(meeting_id: int, user_id: int, message: str, db: Session
ret.chat_messages = extract_and_format_agenda(ret.chat_messages)
return ret

@router.post("/meetings/{meeting_id}/{user_id}/conversation/message_audio", response_model=schemas.Conversation)
async def router_add_message_audio(
meeting_id: int,
user_id: int,
audio_file: UploadFile = File(...),
db: Session = Depends(get_db)
):
# Check file type
if audio_file.content_type not in AUDIO_MIME_TYPES:
raise HTTPException(status_code=400, detail="Invalid file type. Only audio files are allowed.")

# Check file size
file_size = await audio_file.read()
await audio_file.seek(0) # Reset file pointer to the beginning
if len(file_size) > MAX_FILE_SIZE:
raise HTTPException(status_code=400, detail="File size exceeds the 5 MB limit.")

audio_text = await convert_audio_to_text(audio_file)
if audio_text is None:
raise HTTPException(status_code=500, detail="Failed to transcribe audio file.")

return router_add_message(meeting_id, user_id, audio_text, db)

@router.delete("/meetings/{meeting_id}/{user_id}/conversation", response_model=schemas.Conversation)
def delete_conversation(meeting_id: int, user_id: int, db: Session = Depends(get_db)):
db_conversation = get_conversation(db, meeting_id=meeting_id, user_id=user_id)
Expand Down
8 changes: 8 additions & 0 deletions backend/app/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,11 @@ def delete_user_route(user_id: int, db: Session = Depends(get_db)):
if db_user is None:
raise HTTPException(status_code=404, detail="User not found")
return db_user

@router.get("/users/{user_id}/meetings", response_model=List[schemas.Meeting])
def read_user_meetings(user_id: int, db: Session = Depends(get_db)):
db_user = get_user(db, user_id=user_id)
if db_user is None:
raise HTTPException(status_code=404, detail="User not found")
return db_user.meetings

2 changes: 1 addition & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ sqlalchemy
pydantic
python-dotenv
openai

python-multipart

0 comments on commit 8bfaa98

Please sign in to comment.