Skip to content

Commit

Permalink
moved sqlite code back to a single example
Browse files Browse the repository at this point in the history
  • Loading branch information
chadbailey59 committed Jan 31, 2025
1 parent bc98c2e commit d236973
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 205 deletions.
Binary file removed example.db
Binary file not shown.
30 changes: 25 additions & 5 deletions examples/foundational/28c-transcription-processor-gemini.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import asyncio
import os
import sqlite3
import sys
from typing import List, Optional

Expand Down Expand Up @@ -48,22 +49,33 @@ class TranscriptHandler:
output_file: Optional path to file where transcript is saved. If None, outputs to log only.
"""

def __init__(self, output_file: Optional[str] = None):
"""Initialize handler with optional file output.
def __init__(self, output_file: Optional[str] = None, output_db: Optional[str] = None):
"""Initialize handler with optional file or database output.
Args:
output_file: Path to output file. If None, outputs to log only.
"""
self.messages: List[TranscriptionMessage] = []
self.output_file: Optional[str] = output_file
self.output_db: Optional[str] = output_db

if self.output_db:
self.con = sqlite3.connect("example.db")
self.db = self.con.cursor()

table = self.db.execute("SELECT name FROM sqlite_master WHERE name='messages'")
if not (table.fetchone()):
self.db.execute(
"CREATE TABLE messages(role TEXT, content TEXT, timestamp DATETIME DEFAULT CURRENT_TIMESTAMP )"
)
logger.debug(
f"TranscriptHandler initialized {'with output_file=' + output_file if output_file else 'with log output only'}"
f"TranscriptHandler initialized; output file: {output_file}, output DB: {output_db}"
)

async def save_message(self, message: TranscriptionMessage):
"""Save a single transcript message.
Outputs the message to the log and optionally to a file.
Outputs the message to the log and optionally to a SQLite database or file.
Args:
message: The message to save
Expand All @@ -82,6 +94,14 @@ async def save_message(self, message: TranscriptionMessage):
except Exception as e:
logger.error(f"Error saving transcript message to file: {e}")

# and/or to a SQLite database
if self.output_db:
self.db.execute(
"INSERT INTO messages VALUES (?, ?, ?)",
(message.role, message.content, message.timestamp),
)
self.con.commit()

async def on_transcript_update(
self, processor: TranscriptProcessor, frame: TranscriptionUpdateFrame
):
Expand Down Expand Up @@ -140,7 +160,7 @@ async def main():

# Create transcript processor and handler
transcript = TranscriptProcessor()
transcript_handler = TranscriptHandler() # Output to log only
transcript_handler = TranscriptHandler(output_db="example.db") # Output to log only
# transcript_handler = TranscriptHandler(output_file="transcript.txt") # Output to file and log

pipeline = Pipeline(
Expand Down
200 changes: 0 additions & 200 deletions examples/foundational/28d-transcription-processor-gemini-sqlite.py

This file was deleted.

0 comments on commit d236973

Please sign in to comment.