Skip to content

Commit d622df3

Browse files
committed
Changed db schema for more accuracy
1 parent 386bf4e commit d622df3

5 files changed

Lines changed: 109 additions & 9 deletions

File tree

app/api/v1/multimodal.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ async def multimodal_chat(
140140

141141
# Step 3: Audio Output
142142
if audio_output:
143-
# Convert text to audio and Upload on S3
143+
# Convert text to audio and upload to S3
144144
audio_output_service = AudioOutput()
145145
audio_s3_url = await audio_output_service.convert_text_into_audio(
146146
assistant_content=assistant_content,
@@ -149,6 +149,18 @@ async def multimodal_chat(
149149

150150
response_payload["audio_output_url"] = audio_s3_url
151151

152+
# Save assistant audio as an attachment in DB
153+
await create_attachment(
154+
db=db,
155+
session_id=session.id,
156+
message_id=assistant_msg.id,
157+
url=file_url,
158+
media_type=media_type,
159+
metadata_={"voice_style": voice_style.value},
160+
audio_url=audio_s3_url
161+
)
162+
163+
152164
# Add media file link if uploaded
153165
if file_url:
154166
response_payload["uploaded_file_url"] = file_url

app/crud/attachments.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
1+
from app.models.attachment import Attachment
12
from sqlalchemy.ext.asyncio import AsyncSession
2-
from app.models.attachment import Attachment, MediaType
33

4-
async def create_attachment(db: AsyncSession, session_id, message_id, url: str, media_type: MediaType, metadata: dict = None) -> Attachment:
5-
attach = Attachment(session_id=session_id, message_id=message_id, url=url, media_type=media_type, metadata=metadata)
6-
db.add(attach)
4+
5+
async def create_attachment(
6+
db: AsyncSession,
7+
session_id,
8+
message_id,
9+
url,
10+
media_type,
11+
metadata_=None,
12+
audio_url=None,
13+
):
14+
attachment = Attachment(
15+
session_id=session_id,
16+
message_id=message_id,
17+
url=url,
18+
media_type=media_type,
19+
metadata_=metadata_,
20+
audio_url=audio_url,
21+
)
22+
db.add(attachment)
723
await db.commit()
8-
await db.refresh(attach)
9-
return attach
24+
await db.refresh(attachment)
25+
return attachment

app/models/attachment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ class Attachment(Base):
2828
nullable=True,
2929
)
3030

31-
url = Column(String, nullable=False)
32-
media_type = Column(Enum(MediaType), nullable=False)
31+
url = Column(String, nullable=True)
32+
media_type = Column(Enum(MediaType), nullable=True)
3333
metadata_ = Column(JSONB, nullable=True)
3434

3535
# To store generated assistant audio responses
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Make media_type nullable
2+
3+
Revision ID: 0adeb6494274
4+
Revises: f8e5c60d6d7c
5+
Create Date: 2025-10-05 17:50:45.936151
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
from sqlalchemy.dialects import postgresql
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = '0adeb6494274'
16+
down_revision: Union[str, Sequence[str], None] = 'f8e5c60d6d7c'
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
"""Upgrade schema."""
23+
# ### commands auto generated by Alembic - please adjust! ###
24+
op.alter_column('attachments', 'media_type',
25+
existing_type=postgresql.ENUM('image', 'audio', name='mediatype'),
26+
nullable=True)
27+
# ### end Alembic commands ###
28+
29+
30+
def downgrade() -> None:
31+
"""Downgrade schema."""
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.alter_column('attachments', 'media_type',
34+
existing_type=postgresql.ENUM('image', 'audio', name='mediatype'),
35+
nullable=False)
36+
# ### end Alembic commands ###
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Make media_type nullable
2+
3+
Revision ID: fa1dbf5dd801
4+
Revises: 0adeb6494274
5+
Create Date: 2025-10-05 17:55:01.776342
6+
7+
"""
8+
from typing import Sequence, Union
9+
10+
from alembic import op
11+
import sqlalchemy as sa
12+
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = 'fa1dbf5dd801'
16+
down_revision: Union[str, Sequence[str], None] = '0adeb6494274'
17+
branch_labels: Union[str, Sequence[str], None] = None
18+
depends_on: Union[str, Sequence[str], None] = None
19+
20+
21+
def upgrade() -> None:
22+
"""Upgrade schema."""
23+
# ### commands auto generated by Alembic - please adjust! ###
24+
op.alter_column('attachments', 'url',
25+
existing_type=sa.VARCHAR(),
26+
nullable=True)
27+
# ### end Alembic commands ###
28+
29+
30+
def downgrade() -> None:
31+
"""Downgrade schema."""
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.alter_column('attachments', 'url',
34+
existing_type=sa.VARCHAR(),
35+
nullable=False)
36+
# ### end Alembic commands ###

0 commit comments

Comments
 (0)