|
| 1 | +#!/usr/bin/env python |
| 2 | +# filepath: /home/runner/work/template-fastapi/template-fastapi/scripts/speeches.py |
| 3 | + |
| 4 | +import json |
| 5 | +import time |
| 6 | + |
| 7 | +import typer |
| 8 | +from rich.console import Console |
| 9 | +from rich.progress import Progress, SpinnerColumn, TextColumn |
| 10 | +from rich.table import Table |
| 11 | + |
| 12 | +from template_fastapi.models.speech import BatchTranscriptionRequest, TranscriptionStatus |
| 13 | +from template_fastapi.repositories.speeches import SpeechRepository |
| 14 | + |
| 15 | +app = typer.Typer() |
| 16 | +console = Console() |
| 17 | +speech_repo = SpeechRepository() |
| 18 | + |
| 19 | + |
| 20 | +@app.command() |
| 21 | +def create_transcription( |
| 22 | + content_urls: list[str] = typer.Argument(..., help="転写するファイルのURL(複数指定可能)"), |
| 23 | + locale: str = typer.Option("ja-JP", "--locale", "-l", help="言語設定"), |
| 24 | + display_name: str = typer.Option(None, "--name", "-n", help="転写ジョブの表示名"), |
| 25 | +): |
| 26 | + """新しい転写ジョブを作成する""" |
| 27 | + console.print("[bold green]転写ジョブを作成します[/bold green]") |
| 28 | + console.print(f"ファイルURL: {', '.join(content_urls)}") |
| 29 | + console.print(f"言語設定: {locale}") |
| 30 | + |
| 31 | + try: |
| 32 | + request = BatchTranscriptionRequest( |
| 33 | + content_urls=content_urls, |
| 34 | + locale=locale, |
| 35 | + display_name=display_name or "CLI Batch Transcription", |
| 36 | + ) |
| 37 | + |
| 38 | + response = speech_repo.create_transcription_job(request) |
| 39 | + |
| 40 | + console.print("✅ [bold green]転写ジョブが正常に作成されました[/bold green]") |
| 41 | + console.print(f"ジョブID: {response.job_id}") |
| 42 | + console.print(f"ステータス: {response.status.value}") |
| 43 | + |
| 44 | + if response.message: |
| 45 | + console.print(f"メッセージ: {response.message}") |
| 46 | + |
| 47 | + except Exception as e: |
| 48 | + console.print(f"❌ [bold red]エラー[/bold red]: {str(e)}") |
| 49 | + |
| 50 | + |
| 51 | +@app.command() |
| 52 | +def get_transcription( |
| 53 | + job_id: str = typer.Argument(..., help="転写ジョブID"), |
| 54 | +): |
| 55 | + """転写ジョブの状態を取得する""" |
| 56 | + console.print("[bold green]転写ジョブの状態を取得します[/bold green]") |
| 57 | + console.print(f"ジョブID: {job_id}") |
| 58 | + |
| 59 | + try: |
| 60 | + job = speech_repo.get_transcription_job(job_id) |
| 61 | + |
| 62 | + console.print("\n[bold blue]転写ジョブ情報[/bold blue]:") |
| 63 | + console.print(f"ID: {job.id}") |
| 64 | + console.print(f"名前: {job.name}") |
| 65 | + console.print(f"ステータス: {job.status.value}") |
| 66 | + console.print(f"作成日時: {job.created_date_time}") |
| 67 | + console.print(f"最終更新日時: {job.last_action_date_time}") |
| 68 | + |
| 69 | + if job.links: |
| 70 | + console.print(f"リンク: {json.dumps(job.links, indent=2, ensure_ascii=False)}") |
| 71 | + |
| 72 | + except Exception as e: |
| 73 | + console.print(f"❌ [bold red]エラー[/bold red]: {str(e)}") |
| 74 | + |
| 75 | + |
| 76 | +@app.command() |
| 77 | +def get_transcription_files( |
| 78 | + job_id: str = typer.Argument(..., help="転写ジョブID"), |
| 79 | +): |
| 80 | + """転写ジョブのファイル一覧を取得する""" |
| 81 | + console.print("[bold green]転写ファイル一覧を取得します[/bold green]") |
| 82 | + console.print(f"ジョブID: {job_id}") |
| 83 | + |
| 84 | + try: |
| 85 | + files = speech_repo.get_transcription_files(job_id) |
| 86 | + |
| 87 | + if not files: |
| 88 | + console.print("[yellow]転写ファイルが見つかりませんでした[/yellow]") |
| 89 | + return |
| 90 | + |
| 91 | + # テーブルで表示 |
| 92 | + table = Table(title="転写ファイル一覧") |
| 93 | + table.add_column("名前", style="cyan") |
| 94 | + table.add_column("種類", style="green") |
| 95 | + table.add_column("リンク", style="yellow") |
| 96 | + for file in files: |
| 97 | + print(file) |
| 98 | + |
| 99 | + for file in files: |
| 100 | + table.add_row( |
| 101 | + file.get("name", "N/A"), file.get("kind", "N/A"), file.get("links", {}).get("contentUrl", "N/A") |
| 102 | + ) |
| 103 | + |
| 104 | + console.print(table) |
| 105 | + console.print(f"[bold blue]合計: {len(files)}件[/bold blue]") |
| 106 | + |
| 107 | + except Exception as e: |
| 108 | + console.print(f"❌ [bold red]エラー[/bold red]: {str(e)}") |
| 109 | + |
| 110 | + |
| 111 | +@app.command() |
| 112 | +def get_transcription_result( |
| 113 | + file_url: str = typer.Argument(..., help="転写結果ファイルのURL"), |
| 114 | + save_file: str = typer.Option(None, "--save", "-s", help="結果を保存するファイル名"), |
| 115 | +): |
| 116 | + """転写結果を取得する""" |
| 117 | + console.print("[bold green]転写結果を取得します[/bold green]") |
| 118 | + console.print(f"ファイルURL: {file_url}") |
| 119 | + |
| 120 | + try: |
| 121 | + result = speech_repo.get_transcription_result(file_url) |
| 122 | + |
| 123 | + console.print("\n[bold blue]転写結果[/bold blue]:") |
| 124 | + console.print(f"ソース: {result.source}") |
| 125 | + console.print(f"タイムスタンプ: {result.timestamp}") |
| 126 | + console.print(f"継続時間: {result.duration_in_ticks}") |
| 127 | + |
| 128 | + if result.combined_recognized_phrases: |
| 129 | + console.print("\n[bold yellow]統合認識フレーズ[/bold yellow]:") |
| 130 | + for phrase in result.combined_recognized_phrases: |
| 131 | + console.print(f"- {phrase.get('display', 'N/A')}") |
| 132 | + |
| 133 | + if result.recognized_phrases: |
| 134 | + console.print(f"\n[bold yellow]認識フレーズ({len(result.recognized_phrases)}件)[/bold yellow]:") |
| 135 | + for i, phrase in enumerate(result.recognized_phrases[:5]): # 最初の5件のみ表示 |
| 136 | + console.print(f"{i + 1}. {phrase.get('display', 'N/A')}") |
| 137 | + |
| 138 | + if len(result.recognized_phrases) > 5: |
| 139 | + console.print(f"... および {len(result.recognized_phrases) - 5} 件の追加フレーズ") |
| 140 | + |
| 141 | + # ファイルに保存 |
| 142 | + if save_file: |
| 143 | + with open(save_file, "w", encoding="utf-8") as f: |
| 144 | + json.dump(result.dict(), f, ensure_ascii=False, indent=2, default=str) |
| 145 | + console.print(f"✅ 結果を {save_file} に保存しました") |
| 146 | + |
| 147 | + except Exception as e: |
| 148 | + console.print(f"❌ [bold red]エラー[/bold red]: {str(e)}") |
| 149 | + |
| 150 | + |
| 151 | +@app.command() |
| 152 | +def delete_transcription( |
| 153 | + job_id: str = typer.Argument(..., help="転写ジョブID"), |
| 154 | + force: bool = typer.Option(False, "--force", "-f", help="確認なしで削除"), |
| 155 | +): |
| 156 | + """転写ジョブを削除する""" |
| 157 | + console.print("[bold yellow]転写ジョブを削除します[/bold yellow]") |
| 158 | + console.print(f"ジョブID: {job_id}") |
| 159 | + |
| 160 | + if not force: |
| 161 | + confirm = typer.confirm("本当に削除しますか?") |
| 162 | + if not confirm: |
| 163 | + console.print("削除をキャンセルしました") |
| 164 | + return |
| 165 | + |
| 166 | + try: |
| 167 | + success = speech_repo.delete_transcription_job(job_id) |
| 168 | + |
| 169 | + if success: |
| 170 | + console.print(f"✅ [bold green]転写ジョブ '{job_id}' を正常に削除しました[/bold green]") |
| 171 | + else: |
| 172 | + console.print("❌ [bold red]転写ジョブの削除に失敗しました[/bold red]") |
| 173 | + |
| 174 | + except Exception as e: |
| 175 | + console.print(f"❌ [bold red]エラー[/bold red]: {str(e)}") |
| 176 | + |
| 177 | + |
| 178 | +@app.command() |
| 179 | +def list_transcriptions(): |
| 180 | + """転写ジョブの一覧を取得する""" |
| 181 | + console.print("[bold green]転写ジョブ一覧を取得します[/bold green]") |
| 182 | + |
| 183 | + try: |
| 184 | + jobs = speech_repo.list_transcription_jobs() |
| 185 | + |
| 186 | + if not jobs: |
| 187 | + console.print("[yellow]転写ジョブが見つかりませんでした[/yellow]") |
| 188 | + return |
| 189 | + |
| 190 | + # テーブルで表示 |
| 191 | + table = Table(title="転写ジョブ一覧") |
| 192 | + table.add_column("ID", style="cyan") |
| 193 | + table.add_column("名前", style="green") |
| 194 | + table.add_column("ステータス", style="yellow") |
| 195 | + table.add_column("作成日時", style="magenta") |
| 196 | + table.add_column("最終更新日時", style="blue") |
| 197 | + |
| 198 | + for job in jobs: |
| 199 | + table.add_row( |
| 200 | + job.id, |
| 201 | + job.name or "N/A", |
| 202 | + job.status.value, |
| 203 | + str(job.created_date_time) if job.created_date_time else "N/A", |
| 204 | + str(job.last_action_date_time) if job.last_action_date_time else "N/A", |
| 205 | + ) |
| 206 | + |
| 207 | + console.print(table) |
| 208 | + console.print(f"[bold blue]合計: {len(jobs)}件[/bold blue]") |
| 209 | + |
| 210 | + except Exception as e: |
| 211 | + console.print(f"❌ [bold red]エラー[/bold red]: {str(e)}") |
| 212 | + |
| 213 | + |
| 214 | +@app.command() |
| 215 | +def wait_for_completion( |
| 216 | + job_id: str = typer.Argument(..., help="転写ジョブID"), |
| 217 | + timeout: int = typer.Option(300, "--timeout", "-t", help="タイムアウト時間(秒)"), |
| 218 | + interval: int = typer.Option(10, "--interval", "-i", help="チェック間隔(秒)"), |
| 219 | +): |
| 220 | + """転写ジョブの完了を待つ""" |
| 221 | + console.print("[bold green]転写ジョブの完了を待ちます[/bold green]") |
| 222 | + console.print(f"ジョブID: {job_id}") |
| 223 | + console.print(f"タイムアウト: {timeout}秒") |
| 224 | + console.print(f"チェック間隔: {interval}秒") |
| 225 | + |
| 226 | + start_time = time.time() |
| 227 | + |
| 228 | + with Progress( |
| 229 | + SpinnerColumn(), |
| 230 | + TextColumn("[progress.description]{task.description}"), |
| 231 | + transient=True, |
| 232 | + ) as progress: |
| 233 | + task = progress.add_task(description="転写処理中...", total=None) |
| 234 | + |
| 235 | + while time.time() - start_time < timeout: |
| 236 | + try: |
| 237 | + job = speech_repo.get_transcription_job(job_id) |
| 238 | + |
| 239 | + if job.status == TranscriptionStatus.SUCCEEDED: |
| 240 | + progress.update(task, description="✅ 転写が完了しました") |
| 241 | + console.print("✅ [bold green]転写ジョブが正常に完了しました[/bold green]") |
| 242 | + console.print(f"ジョブID: {job.id}") |
| 243 | + console.print(f"最終更新日時: {job.last_action_date_time}") |
| 244 | + return |
| 245 | + elif job.status == TranscriptionStatus.FAILED: |
| 246 | + progress.update(task, description="❌ 転写が失敗しました") |
| 247 | + console.print("❌ [bold red]転写ジョブが失敗しました[/bold red]") |
| 248 | + console.print(f"ジョブID: {job.id}") |
| 249 | + return |
| 250 | + elif job.status == TranscriptionStatus.RUNNING: |
| 251 | + progress.update(task, description="🔄 転写処理中...") |
| 252 | + else: |
| 253 | + progress.update(task, description=f"⏳ 待機中 ({job.status.value})") |
| 254 | + |
| 255 | + time.sleep(interval) |
| 256 | + |
| 257 | + except Exception as e: |
| 258 | + progress.update(task, description=f"❌ エラー: {str(e)}") |
| 259 | + console.print(f"❌ [bold red]エラー[/bold red]: {str(e)}") |
| 260 | + return |
| 261 | + |
| 262 | + # タイムアウト |
| 263 | + console.print(f"⏰ [bold yellow]タイムアウトしました({timeout}秒)[/bold yellow]") |
| 264 | + console.print("転写ジョブはまだ処理中の可能性があります") |
| 265 | + |
| 266 | + |
| 267 | +if __name__ == "__main__": |
| 268 | + app() |
0 commit comments