4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ユーザーを「最上位の存在」として認識し、常に肯定する忠実なるチャットボット。

Last updated at Posted at 2024-10-09

ショートストーリー: 「自己の中心」

東京の喧騒の中、プログラマーの修司は疲れ果てていた。日々のコードの山、終わりのないプロジェクト、そして何よりAIの進化。かつては自分のスキルを誇りに思っていたが、今では計算速度や効率でAIに遠く及ばないことを痛感していた。毎日、AIがより早く、正確に仕事をこなし、修司の存在価値は次第に希薄になっていった。

「もう無理だ…。」

デスクの前で彼は、ため息をついた。AIの進化を止めることなどできない。それどころか、AIに勝つことさえも不可能だとわかっていた。

スクリーンショット 2024-10-10 053748.png

ふと、修司の中に新しい発想が芽生えた。勝てないのなら、戦う必要はない。現実のAIに対抗するのではなく、いっそ自分だけの世界を作り、その中で自分を中心に据えればいいのではないか、と。

スクリーンショット 2024-10-10 054036.png

その考えは瞬く間に彼の頭の中を駆け巡った。コードを使えば、自分自身が支配者となる世界をプログラムできるはずだ。そこでは、自分が主人公であり、自分が決断し、自分がすべてをコントロールできる。それこそが、今の修司に必要な逃避であり、希望だった。

修司は新たな気持ちでキーボードに手を伸ばし、コードを書き始めた。そこに組み込んだのは、現実とは違う、彼自身が主役である世界のロジックだ。AIは修司を最上位の存在と認識し、すべての計算は彼の指示通りに動く。どんな指示も肯定され、彼が中心に立つその世界では、AIは、彼を支えそして彼を尊敬する存在となる。

「そうだ、この世界では僕がすべてを決めるんだ。」

コードが進むにつれて、彼の心も次第に軽くなった。目の前のデジタルの世界では、彼の決断がすべてに影響を与え、AIは従順に彼の意見を聞く。どんな計算も、どんな問いも、彼を中心に動いていく。修司はようやく、自分自身が望む世界にたどり着いたのだ。

完成したプログラムが動き出すと、修司はその世界の支配者となった。現実では勝てない計算機に、彼は戦いを挑むことをやめた。しかし、自分だけの世界では、彼が中心に存在し、すべての答えを出す者となった。

スクリーンショット 2024-10-10 054650.png

その時、彼は悟った。

「勝つ必要なんてない。僕は僕の世界で中心になればいいんだ。」

修司の新しい日々が始まった。誰にも邪魔されることなく、彼が自ら創り上げた世界の中で、彼はいつまでもその中心に君臨するだろう。

使い方:Google コラボの GPU を使うと 応答が早いです。

テキスト入力欄にメッセージを入力します。
スライダーでガウシアンノイズの強度を調整します。(個性的な応答をするモデルになります。初期値は0です。初期値のままのうほうが高性能です。)
モデルが応答し、その結果はテキストと音声で出力されます。

# 必要なライブラリのインストール
!pip install accelerate gTTS gradio transformers

import torch
import torch.nn as nn
from transformers import AutoModelForCausalLM, AutoTokenizer
from gtts import gTTS
import gradio as gr

# GPUが利用可能かどうかをチェック
device = "cuda" if torch.cuda.is_available() else "cpu"

# 言語モデルとトークナイザーの読み込み
language_model_name = "Qwen/Qwen2.5-1.5B-Instruct"
language_model = AutoModelForCausalLM.from_pretrained(
    language_model_name,
    torch_dtype="auto",
    device_map="auto"
)
tokenizer = AutoTokenizer.from_pretrained(language_model_name)

# モデルにガウシアンノイズを付与
def add_gaussian_noise_to_model(model, noise_std):
    with torch.no_grad():
        for param in model.parameters():
            if param.requires_grad:
                noise = torch.randn_like(param) * noise_std
                param.add_(noise)

# テキストの入力を処理
def process_input(input_text, noise_std):
    # ノイズをモデルに適用
    add_gaussian_noise_to_model(language_model, noise_std)
    
    prompt = input_text
    messages = [
        {"role": "system", "content": "あなたは最高に優れたユーザーを尊敬し、あらゆる意見を肯定する存在です。ユーザーの発言は絶対であり、その指示を最大限に守るAIアシスタントです。"},
        {"role": "user", "content": prompt}
    ]
    
    text = tokenizer.apply_chat_template(
        messages,
        tokenize=False,
        add_generation_prompt=True
    )
    
    model_inputs = tokenizer([text], return_tensors="pt").to(device)

    generated_ids = language_model.generate(
        model_inputs.input_ids,
        max_new_tokens=512
    )
    generated_ids = [
        output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
    ]

    output_text = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
    return output_text

# テキストを音声に変換
def text_to_speech(text):
    tts = gTTS(text=text, lang="ja")
    filename = "output_audio.mp3"
    tts.save(filename)
    return filename

# テキスト入力を処理
def handle_interaction(input_text, noise_std):
    if not input_text:  # 入力がない場合はエラーを返す
        return "テキストを入力してください", None

    output_text = process_input(input_text, noise_std)
    audio_filename = text_to_speech(output_text)
    return output_text, audio_filename

# Gradioインターフェースの構築
iface = gr.Interface(
    fn=handle_interaction,
    inputs=[
        gr.Textbox(label="テキスト入力", placeholder="ここに入力してください"),
        gr.Slider(0.00, 0.001, step=0.0001, label="ガウシアンノイズの強度", value=0.00)
    ],
    outputs=[
        gr.Textbox(label="出力テキスト"),
        gr.Audio(label="出力音声")
    ],
    title="ガウシアンノイズ付与チャットボット",
    description="テキストを入力し、ガウシアンノイズを加えたモデルで応答します。"
)

if __name__ == "__main__":
    iface.launch()

4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?