2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

備忘録として、MongoDBに音声ファイルをBase64に変換して保存する方法を残します。

GitHub

https://github.com/Nyamadamadamada/animal_voice
動物の鳴き声をあつめたページを作りたかったのですが、未完です。
音声ファイルをMongoDBに保存するくだりは参考になるかと思います。

メインのコード


from fastapi import FastAPI, HTTPException, UploadFile, Form, status
from fastapi.responses import HTMLResponse, RedirectResponse
from typing import Optional
from pymongo import MongoClient
import traceback
import base64

app = FastAPI()

@app.get("/", response_class=HTMLResponse)
async def read_items(message: Optional[str] = None):
    alert_script = ""
    if message:
        alert_script = f"<script>alert('{message}')</script>"

    return f"""
    <html>
        <head>
            <title>FAST API</title>
        </head>
        <body>
            <h1>FAST API</h1>
            <form method="post" action="/input/audio/" enctype="multipart/form-data">
            <p><input type="file" id="v_file" name="v_file" accept="audio/*" /></p>
            <button type="submit">送信</button>
            </form>
            {alert_script}
        </body>
    </html>
    """

@app.post("/input/audio/", response_class=HTMLResponse)
async def inputAudio(v_file: UploadFile = Form(...)):
    try:
      # MongoDBへの接続
      client = MongoClient('mongodb://root:example@mongo:27017/')
      db = client.fileData

      file_content = await v_file.read()
      # Base64でエンコード
      encoded_file = base64.b64encode(file_content)
      post = {
            "filename": v_file.filename,
            "file": encoded_file.decode()
        }
      db.audio.insert_one(post)
      return RedirectResponse("http://localhost:8000?message=アップロード完了", status_code=status.HTTP_303_SEE_OTHER)
    except Exception as e:
      # 例外情報をログに記録
      traceback.print_exc()
      # HTTPレスポンスとしてエラーメッセージを返す
      raise HTTPException(status_code=500, detail=str(e))

初めの画面

docker compsoe up -d 

Docker Composeコマンドを実行し、サーバーをたちあげたあと
http://localhost:8000/にアクセスすると次の画面が表示されます。

スクリーンショット 2023-12-19 22.40.48.png

終わりに

メモでした。

参考

base64 エンコード
https://cloud.google.com/speech-to-text/docs/base64-encoding?hl=ja#using_client_libraries

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?