音声解析をバックエンドからレスポンスする処理

@app.route("/avatar", methods=["POST"])
def avator_response():
    data = request.json
    user_text = data.get("text", "")

    # 1. Creating a reply using ChatGPT etc. (This is a simple example)
    client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
    completion = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=[
            {"role": "system", "content": "You are a helpful avatar assistant."},
            {"role": "user", "content": user_text}
        ]
    )
    reply_text = completion.choices[0].message.content

    # 2. TTS speech generation
    tts = gTTS(text=reply_text, lang='ja')
    audio_filename = os.path.join(AUDIO_DIR, f"output_{uuid.uuid4().hex}.mp3")
    tts.save(audio_filename)

    # 3. Convert MP3 to WAV using pydub
    wav_filename = audio_filename.replace(".mp3", ".wav")
    sound = AudioSegment.from_mp3(audio_filename)
    sound.export(wav_filename, format="wav")

    # 4. Audio data analysis (amplitude calculation for lip-syncing)
    audio = AudioSegment.from_wav(wav_filename)
    samples = np.array(audio.get_array_of_samples())
    if audio.channels == 2:
        samples = samples.reshape((-1, 2)).mean(axis=1)
    samples = samples / np.max(np.abs(samples))  # Normalize

    # Frame-by-frame amplitude sampling
    fps = 30
    samples_per_frame = int(audio.frame_rate / fps)
    lip_sync = []
    for i in range(0, len(samples), samples_per_frame):
        frame = samples[i:i + samples_per_frame]
        lip_sync.append(float(np.abs(frame).mean()))

    # 5. Return to client
    response_data = {
        "text": reply_text,
        "audio_url": url_for("get_audio", filename=os.path.basename(wav_filename), _external=True),
        "lip_sync": lip_sync
    }

    return Response(json.dumps(response_data, ensure_ascii=False), mimetype="application/json")

@app.route("/audio/<filename>", methods=["GET"])
def get_audio(filename):
    file_path = os.path.join(AUDIO_DIR, filename)
    return send_file(file_path, mimetype="audio/wav")

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000, debug=True)

$ curl -X POST http://127.0.0.1:5000/avatar \
-H “Content-Type: application/json” \
-d ‘{“text”: “おはよう!”}’
{“text”: “おはようございます!お困りのことはありますか?”, “audio_url”: “http://127.0.0.1:5000/audio/output_8b9b617f9ea44b4e9382d99069279c2b.wav”, “lip_sync”: [0.0, 5.401201227152919e-08, 0.008150034567687852, 0.1152054616946809, 0.12006854124357258, 0.08367767791556842, 0.026896253726828846, 0.01888351769433522, 0.07841339713952383, 0.2201013265350214, 0.2508166616255455, 0.2270837834334356, 0.18286134036209653, 0.12693546644773795, 0.19306745020092467, 0.2823540595428423, 0.26987355787927236, 0.30742827204770345, 0.33021129499200624, 0.3036520222097394, 0.13783822322084432, 0.053725370522404184, 0.23884381886531564, 0.26545121635051633, 0.16945415460398394, 0.04699428552910167, 0.037515015339411484, 0.22347993993864235, 0.2646327183165536, 0.22138405781445794, 0.19320739532472023, 0.20940100678390874, 0.23490348053407076, 0.22536436503478374, 0.21555653977444586, 0.14586462429244265, 0.14904603983926024, 0.13877635786198853, 0.08746219159140994, 0.02229572656958908, 0.01466869031672644, 0.010831244868858834, 0.008575973296461132, 0.0059669770556971865, 0.002284438059024327, 0.0007382901957395324, 0.0006873028561552089, 0.00023819297411744372, 0.00011202091345115153, 0.00011088666119344939, 0.00011083264918117787, 0.00014950524996759277, 0.054377403534546086, 0.15464892192023505, 0.11003192109925247, 0.01149159573089055, 0.0017083999481484681, 0.01060147776865575, 0.21648749081795793, 0.2837956941623817, 0.2357313766581688, 0.295791492027827, 0.24570480274813122, 0.2426950913883248, 0.22178412478935314, 0.1279997191375362, 0.1125969515620274, 0.18296455731754743, 0.2677368966858229, 0.30668674113122757, 0.252059802099987, 0.22629239942963317, 0.24090750983018622, 0.0999186038975068, 0.005041211165363177, 0.01174237350386726, 0.16595396016073974, 0.22518860994685216, 0.04122672082271097, 0.002156267553903988, 0.054671390917340024, 0.2686635267683533, 0.24548022080110612, 0.2177663332325109, 0.16169052197208658, 0.25034897161128633, 0.2103575595212375, 0.17521005271572399, 0.15601337337423843, 0.12766689711791904, 0.1107986756254591, 0.047134932809056736, 0.08557376960636046, 0.11917485848852785, 0.14922184893920407, 0.17545402497515447, 0.15926343818865316, 0.14388913494361147, 0.15382718316553604, 0.08909978179147043, 0.019018493713001773, 0.022057209523398003, 0.019663235103487015, 0.0030874346454651514, 0.0014317504212936955, 0.009182042086159962, 0.0501337337423843, 0.07244177505077129, 0.0849778010629564, 0.06556064468737847, 0.044696560515058555, 0.017215464719353583, 0.0009286285269844013, 0.0002996046320701724, 0.00023268374886574773, 0.00010975240893574729, 0.00011115672125480706, 0.00011115672125480704, 0.00011056258911982022, 0.00011158881735297929, 0.00011050857710754869, 0.00011126474527935009, 0.00011148079332843622, 0.00011083264918117787, 0.00011083264918117786, 7.388843278745191e-05]}

chatバックエンド側(Python (Flask + PostgreSQL + OpenAI))の作り込み1

chat() の中で  
認証チェックを追加  
DBにユーザーの入力を保存  
OpenAI API や HuggingFace API を呼んで応答を生成  
生成した応答をDBに保存して返却

### step.0 テーブル作成

CREATE TABLE chat_messages (
    id SERIAL PRIMARY KEY,
    user_id VARCHAR(50) NOT NULL,
    message TEXT NOT NULL,
    reply TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

### step.1 パッケージインストール
$ install flask psycopg2-binary openai

### step.2 Python (Flask + PostgreSQL + OpenAI)

from flask import Flask, request, Response
import json
import psycopg2
import os
from openai import OpenAI

app = Flask(__name__)

DB_CONFIG = {
    "dbname": os.getenv("DB_NAME"),
    "user": os.getenv("DB_USER"),
    "password": os.getenv("DB_PASSWORD"),
    "host": os.getenv("DB_HOST", "localhost"),
    "port": os.getenv("DB_PORT", 5432)
}


def get_db_connection():
    try:
        conn = psycopg2.connect(**DB_CONFIG)
        return conn
    except Exception as e:
        print(f"Database connection error: {e}")
        return None

@app.route("/chat", methods=["POST"])
def chat():
    try:
        data = request.get_json()

        user_id = data.get("user_id")
        message = data.get("message")

        if not user_id or not message:
            return Response(json.dumps({"error": "user_id and message are required"}), status=400, content_type="application/json; charset=utf-8")

        conn = get_db_connection()
        cur = conn.cursor()

        cur.execute(
            "INSERT INTO chat_messages (user_id, message) VALUES (%s, %s) RETURNING id;",
            (user_id, message)
        )
        chat_id = cur.fetchone()[0]

        client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
        completion = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages = [
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": message}
            ]
        )
        api_reply = completion.choices[0].message.content

        cur.execute(
            "UPDATE chat_messages SET reply = %s WHERE id = %s;",
            (api_reply, chat_id)
        )
        conn.commit()
        cur.close()
        conn.close()
        

        response_json = json.dumps({"reply": api_reply}, ensure_ascii=False)
        return Response(response_json, content_type="application/json; charset=utf-8")

    except Exception as e:
        error_json = json.dumps({"error": str(e)}, ensure_ascii=False)
        return Response(error_json, status=400, content_type="application/json; charset=utf-8")


if __name__ == "__main__":
    app.run(debug=True)

$ curl -X POST http://127.0.0.1:5000/chat -H “Content-Type: application/json” -d ‘{“user_id”: “12345”, “message”: “おはよう!”}’
{“reply”: “おはようございます!元気ですか?何かお手伝いできることがありますか?”}

おおお

flaskでバックエンドを構築する

フロントエンドからデータを受け取るところを記述する
jsonをpostすると、jsonのレスポンスが返ってくる

from flask import Flask, request, Response
import json

app = Flask(__name__)

@app.route("/chat", methods=["POST"])
def chat():
    try:
        data = request.get_json()

        user_id = data.get("user_id")
        message = data.get("message")

        # ここで認証チェック、DBアクセス、AIモデルの呼び出しなどを行う
        # ここではダミーの応答を返す

        ai_reply = f"ユーザ({user_id})のメッセージを受け取りました: {message}"

        response_json = json.dumps({"reply": ai_reply}, ensure_ascii=False)
        return Response(response_json, content_type="application/json; charset=utf-8")

    except Exception as e:
        error_json = json.dumps({"error": str(e)}, ensure_ascii=False)
        return Response(error_json, status=400, content_type="application/json; charset=utf-8")


if __name__ == "__main__":
    app.run(debug=True)

$ curl -X POST http://127.0.0.1:5000/chat -H “Content-Type: application/json” -d ‘{“user_id”: “12345”, “message”: “おはよう!”}’
{“reply”: “ユーザ(12345)のメッセージを受け取りました: おはよう!”}

この chat() の中で 以下も書いていく。
– 認証チェックを追加  
– DBにユーザーの入力を保存  
– OpenAI API や HuggingFace API を呼んで応答を生成  
– 生成した応答をDBに保存して返却

ChatGPT-4oとGPT5の違い

ChatGTP-4oがマルチモーダルAI実用化であったのに対し、ChatGPT5はは「思考するAI」「動けるAI」へ進化

#### ChatGPT-4o
– モデル構造 : テキスト・音声・画像を統合した「マルチモーダルモデル」
– モデルの選択 : 手動選択(GPT-4o, o3など)
– 応答速度(音声) : 平均320ms, 自然な会話速度
– 記憶機能(メモリ) : 一部記憶あり
– 外部連携機能 : なし

#### ChatGPT-5
– モデル構造 : 複数の思考エンジンを自動で切り替える「統合システム+動的推論」
– モデルの選択 : 自動判定(Base/Thinking/Proにルーティング)
– 応答速度(音声) : 音声は4oベース、今後統合予定
– 記憶機能(メモリ) : 永続メモリ。過去の会話やユーザの好みを保持
– 外部連携機能 : Gmail、カレンダー、Drive等と直接連携可能

プロンプトの内容や難易度を自動解析し、最適な推論モデル(高速応答/深層思考)を選択
AIの性格を選べる

ソフトウェア開発、数学、多言語コード編集、マルチモーダル理解などあらゆる指標で賢くなった
プランはFree, Plus, Pro, Enterpriseなどあり

思考の質が自動最適化され、過去を覚える

Pythonで入力中の続きのコード生成AIをAPIで試す

フィボナッチ関数について、途中まで書いていて、その続きのコードをレスポンスとして返します。
ソースコードをapiに私、”complete this code”と指示を出しています。

from openai import OpenAI

client = OpenAI(api_key="sk-hoge****")

partial_code = """
def fibonacci(n):
    \"\"\"n番目までのフィボナッチ数列を返す関数.\"\"\"
    sequence = [0, 1]
"""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are an expert Python programmer. Complete the given code without explanations, only code."},
        {"role": "user", "content": f"Complete this Python code:\n\n{partial_code}"}
    ],
    temperature=0.2,
)

print(response.choices[0].message.content)

$ python3 codegen_input.py
“`python
if n <= 0: return [] elif n == 1: return [0] elif n == 2: return sequence for i in range(2, n): next_value = sequence[-1] + sequence[-2] sequence.append(next_value) return sequence ``` Github Copilotではコメントからコード生成はグレーアウトされたコードで即時表示している。 これは特定の実行ボタンなどを押下しなくても表示しているので、Ajaxなど非同期で実行していると思われる。 入力中の続きのコード生成も同様 コメントを書き終えた時や、コードを打ち終えた時などにトリガーが発動される。

Pythonでコード型の生成AIをAPIで試す

from openai import OpenAI

client = OpenAI(api_key="sk-hoge****")

prompt = """
# Pythonで、与えられた文字列を逆順にして返す関数を作ってください
"""

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are a helpful assistant that writes clean Python code.."},
        {"role": "user", "content": prompt}
    ],
    temperature=0.2,
)

print(response.choices[0].message.content)

$ python3 codegen.py
もちろんです!以下は、与えられた文字列を逆順にして返すPythonの関数です。

“`python
def reverse_string(s):
return s[::-1]

# 使用例
input_string = “こんにちは”
reversed_string = reverse_string(input_string)
print(reversed_string) # 出力: はこんに
“`

この関数 `reverse_string` は、スライスを使って文字列を逆順にしています。`s[::-1]` は、文字列 `s` の全ての文字を逆順に取得します。

model, message, tempratureを指定してリクエストしていることがわかります。
なるほど

【Azure】BlobStorageへPythonでアップロードする

$ install azure-storage-blob

Azureのストレージアカウントのセキュリティ&ネットワークのアクセスキーから接続文字列を取得する(キーの方ではない)

from azure.storage.blob import BlobServiceClient, ContentSettings

connection_string = "***"

blob_service_client = BlobServiceClient.from_connection_string(connection_string)

container_name = "images"
blob_name = "cat.jpg"
local_file_path = "cat.jpg"

container_cliient = blob_service_client.get_container_client(container_name)

with open(local_file_path, "rb") as data:
    container_cliient.upload_blob(
        name=blob_name,
        data=data,
        overwrite=True,
        content_settings=ContentSettings(content_type="image/jpeg")
    )

print(f"Blob '{blob_name}' uploaded to container '{container_name}' successfully.")

おおお、アップロードできるようになると、一段別のステージに行った感がありますね。
次はAzureSQLにも触っておきたい。

RDS

テーブルとカラムを表現するには、hashmap< Vector > となるだろうか?

class Table:
    def __init__(self, name, columns):
        self.name = name
        self.columns = columns
        self.rows = []

    def insert(self, values):
        if len(values) != len(self.columns):
            raise ValueError("列の数と値の数が一致しません。")
        row = dict(zip(self.columns, values))
        self.rows.append(row)

    def select_all(self):
        return self.rows

    def __str__(self):
        lines = [" | ".join(self.columns)]
        lines.append("-" * len(lines[0]))
        for row in self.rows:
            lines.append(" | ".join(str(row[col]) for col in self.columns))
        return "\n".join(lines)

class Database:
    def __init__(self):
        self.tables = {}

    def create_table(self, name, columns):
        if name in self.tables:
            raise ValueError(f"テーブル '{name}' は既に存在します。")
        self.tables[name] = Table(name, columns)

    def insert_into(self, table_name, values):
        self.tables[table_name].insert(values)

    def select_all(self, table_name):
        return self.tables[table_name].select_all()

    def show_table(self, table_name):
        print(self.tables[table_name])

if __name__ == "__main__":
    db = Database()
    db.create_table("users", ["id", "name", "email"])
    db.insert_into("users", [1, "Alice", "alice@example.com"])
    db.insert_into("users", [2, "Bob", "bob@example.com"])

    print("📄 users テーブル内容:")
    db.show_table("users")

    print("\n📋 SELECT * FROM users:")
    for row in db.select_all("users"):
        print(row)

$ python3 app.py
📄 users テーブル内容:
id | name | email
—————–
1 | Alice | alice@example.com
2 | Bob | bob@example.com

📋 SELECT * FROM users:
{‘id’: 1, ‘name’: ‘Alice’, ’email’: ‘alice@example.com’}
{‘id’: 2, ‘name’: ‘Bob’, ’email’: ‘bob@example.com’}

【Python】NoSQL【Rust】

import json
import os
from typing import List, Dict, Any

class SimpleNoSQL:
    def __init__(self, db_path: str= "db.json"):
        self.db_path = db_path
        if os.path.exists(self.db_path):
            with open(self.db_path, "r") as f:
                self.data = json.load(f)
        else:
            self.data = []

    def save(self):
        with open(self.db_path, "w") as f:
            json.dump(self.data, f, indent=2)

    def insert(self, document: Dict[str, Any]):
        self.data.append(document)
        self.save()

    def find(self, query: Dict[str, Any]) -> List[Dict[str, Any]]:
        def match(doc):
            return all(doc.get(k) == v for k, v in query.items())
        return [doc for doc in self.data if match(doc)]

    def update(self, query: Dict [str, Any], update_fields: Dict[str, Any]):
        for doc in self.data:
            if all(doc.get(k) == v for k, v in query.items()):
                 doc.update(update_fields)
        self.save()

    def delete(self, query: Dict[str, Any]):
        self.data = [doc for doc in self.data if not all(doc.get(k) == v for k, v in query.items())]
        self.save

db = SimpleNoSQL()

db.insert({
    "session_id": "abc123",
    "code": "fn main() { println!(\"Hello\"); }",
    "output": "Hello",
    "status": "success"
})

results = db.find({"session_id": "abc123"})
print("検索結果:", results)

db.update({"session_id": "abc123"}, {"status": "reviewed"})

[
{
“session_id”: “abc123”,
“code”: “fn main() { println!(\”Hello\”); }”,
“output”: “Hello”,
“status”: “reviewed”
}
]

これをRustでやりたい

### Rustでのjsonの保存

use std::collections::HashMap;
use std::fs::File;
use std::io::Write;
use serde::{Serialize, Deserialize};

fn json_w() -> std::io::Result<()> {
    let mut map = HashMap::new();
    map.insert("name", "Alice");
    map.insert("city", "Tokyo");
    map.insert("language", "Rust");

    let json = serde_json::to_string_pretty(&map).expect("Failed to serialize");
    let mut file = File::create("output.json")?;
    file.write_all(json.as_bytes())?;
    
    println!("{:?}", json);
    Ok(())
}

fn main() {
    let _ = json_w();
}

### jsonの読み取り

fn json_r() {
    let data = fs::read_to_string("output.json").expect("Failed to read file");
    let map: HashMap<String, String> = serde_json::from_str(&data).expect("Failed to deserialize");

    println!("{:#?}", map);    
}

{
“name”: “Alice”,
“language”: “Rust”,
“city”: “Tokyo”,
}

### 改修
レコードごとに保存する必要があるので HashMap から Vec> に変更

fn json_w() -> std::io::Result<()> {
    let mut data: Vec<HashMap<String, String>> = match File::open("output.json") {
        Ok(mut file) => {
            let mut contents = String::new();
            file.read_to_string(&mut contents)?;
            serde_json::from_str(&contents).unwrap_or_else(|_| Vec::new())
        },
        Err(_) => Vec::new(),
    };

    let mut map = HashMap::new();
    // map.insert("name".to_string(), "Alice".to_string());
    // map.insert("city".to_string(), "Tokyo".to_string());
    // map.insert("language".to_string(), "Rust".to_string());

    map.insert("name".to_string(), "Bob".to_string());
    map.insert("city".to_string(), "Osaka".to_string());
    map.insert("language".to_string(), "Go".to_string());

    data.push(map);

    let json = serde_json::to_string_pretty(&data).expect("Failed to serialize");
    let mut file = File::create("output.json")?;
    file.write_all(json.as_bytes())?;

    println!("{:?}", json);
    Ok(())
}

fn json_r() {
    let data = fs::read_to_string("output.json").expect("Failed to read file");
    let v: Vec<HashMap<String, String>> = serde_json::from_str(&data).expect("Failed to deserialize");

    println!("{:#?}", v);    
}

fn main() {
    // 
    // let _ = json_w();
    let _ = json_r();
}

[
{
“language”: “Rust”,
“name”: “Alice”,
“city”: “Tokyo”,
},
{
“city”: “Osaka”,
“language”: “Go”,
“name”: “Bob”,
},
]

### implで表現する
関数をinsert, selectとする

use std::collections::HashMap;
use std::fs;
use std::fs::{File};
use std::io::{Read, Write};
use serde::{Serialize, Deserialize};

pub static FILE_NAME : &'static str = "output.json";

struct NoSQL {
    data: Vec<HashMap<String, String>>,
}

impl NoSQL {
    fn insert(&mut self, map: HashMap<String, String>) -> std::io::Result<()> {
        self.data = match File::open(FILE_NAME) {
            Ok(mut file) => {
                let mut contents = String::new();
                file.read_to_string(&mut contents)?;
                serde_json::from_str(&contents).unwrap_or_else(|_| Vec::new())
            },
            Err(_) => Vec::new(),
        };        
        self.data.push(map);

        let json = serde_json::to_string_pretty(&self.data).expect("Failed to serialize");
        let mut file = File::create(FILE_NAME)?;
        file.write_all(json.as_bytes())?;

        println!("{:?}", json);
        Ok(())
    }

    fn select(&self) {
        let data = fs::read_to_string(FILE_NAME).expect("Failed to read file");
        let v: Vec<HashMap<String, String>> = serde_json::from_str(&data).expect("Failed to deserialize");
    
        println!("{:#?}", v);    
    }
}

fn main() {
    let mut sql = NoSQL{ data: Vec::new() };

    let mut map = HashMap::new();
    map.insert("name".to_string(), "Bob".to_string());
    map.insert("city".to_string(), "Osaka".to_string());
    map.insert("language".to_string(), "Go".to_string());

    sql.insert(map);
    sql.select();
}

こうすると、nosqlは単純にVec>,と言える。
インメモリデータベースの場合は、メモリに保存するので、jsonではなく、単純にデータとして持っておくだけになる。その場合、サイズが動的なので、スタックではなく、ヒープに保存される。

【Python】editor

import curses

def main(stdscr):
    curses.curs_set(1)
    stdscr.clear()
    stdscr.refresh()

    text = []
    row = 0

    while True:
        stdscr.move(row, 0)
        stdscr.clrtoeol()
        stdscr.addstr(row, 0, ''.join(text))
        stdscr.refresh()
        ch = stdscr.getch()

        if ch == 27:  # ESCキーで終了
            break
        elif ch == 10:  # Enter
            text.append('\n')
            row += 1
        elif ch == 127 or ch == curses.KEY_BACKSPACE:
            if text:
                text.pop()
                if text[-1] == '\n':
                    row -= 1
        else:
            text.append(chr(ch))

    with open("output.txt", "w") as f:
        f.write(''.join(text))
    

curses.wrapper(main)

Rustで書くと

use crossterm::{
    cursor,
    event::{read, Event, KeyCode},
    terminal::{enable_raw_mode, disable_raw_mode, ClearType},
    ExecutableCommand,
};
use std::io::{stdout, Write};
use std::fs::File;

fn main() -> std::io::Result<()> {
    let mut stdout = stdout();
    let mut buffer = String::new();

    enable_raw_mode()?;
    stdout.execute(crossterm::terminal::Clear(ClearType::All))?;
    stdout.execute(cursor::MoveTo(0, 0))?;

    loop {
        match read()? {
            Event::Key(event) => match event.code {
                KeyCode::Char(c) => {
                    buffer.push(c);
                    print!("{}", c);
                    stdout.flush()?;
                }
                KeyCode::Enter => {
                    buffer.push('\n');
                    print!("\r\n");
                    stdout.flush()?;
                }
                KeyCode::Backspace => {
                    buffer.pop();
                    stdout.execute(cursor::MoveLeft(1))?;
                    print!(" ");
                    stdout.execute(cursor::MoveLeft(1))?;
                    stdout.flush()?;
                }
                KeyCode::Esc => {
                    break;
                }
                _ => {}
            },
            _ => {}
        }
    }

    disable_raw_mode()?;

    let mut file = File::create("output.txt")?;
    write!(file, "{}", buffer)?;

    Ok(())
}

inputファイルを読み込んで、出力

use crossterm::{
    cursor,
    event::{read, Event, KeyCode},
    terminal::{enable_raw_mode, disable_raw_mode, ClearType},
    ExecutableCommand,
};
use std::io::{stdout, Write};
use std::fs::{File, read_to_string};

fn main() -> std::io::Result<()> {
    let mut stdout = stdout();
    let mut buffer = String::new();

    let initial_text = match read_to_string("input.txt") {
        Ok(content) => content,
        Err(_) => String::new(),
    };
    buffer.push_str(&initial_text);

    enable_raw_mode().expect("Failed to enable raw mode");
    stdout.execute(crossterm::terminal::Clear(ClearType::All))?;
    stdout.execute(cursor::MoveTo(0, 0))?;

    let mut row: u16 = 0;
    let mut col: u16 = 0;
    for ch in buffer.chars() {
        match ch {
            '\n' => {
                print!("\r\n");
                row += 1;
                col = 0;
            }
            _ => {
                print!("{}", ch);
                col += 1;
            }
        }
    }
    stdout.flush()?;

    stdout.execute(cursor::MoveTo(col, row))?;

    loop {
        match read()? {
            Event::Key(event) => match event.code {
                KeyCode::Char(c) => {
                    buffer.push(c);
                    print!("{}", c);
                    stdout.flush()?;
                }
                KeyCode::Enter => {
                    buffer.push('\n');
                    row += 1;
                    print!("\r\n");
                    stdout.flush()?;
                }
                KeyCode::Backspace => {
                    buffer.pop();
                    stdout.execute(cursor::MoveLeft(1))?;
                    print!(" ");
                    stdout.execute(cursor::MoveLeft(1))?;
                    stdout.flush()?;
                }
                KeyCode::Esc => {
                    break;
                }
                _ => {}
            },
            _ => {}
        }
    }

    disable_raw_mode()?;

    let mut file = File::create("output.txt")?;
    write!(file, "{}", buffer)?;

    Ok(())
}

input.txtを行単位で読み込んで出力するなどの工夫が必要