import gradio as gr import torch from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig import spaces import warnings warnings.filterwarnings("ignore") # モデルとトークナイザーの設定 MODEL_NAME = "YUGOROU/TeenEmo-Reasoning-v2" DEVICE = "cuda" if torch.cuda.is_available() else "cpu" # デフォルト設定 DEFAULT_SYSTEM_PROMPT = """あなたは思いやりのあるカウンセラーです。10代の若者の感情や悩みに寄り添い、親身になって話を聞いてください。適切なアドバイスを提供し、ポジティブな視点を提供してください。""" # Global variables for model and tokenizer model = None tokenizer = None def load_model(): """モデルとトークナイザーの読み込み""" global model, tokenizer try: # トークナイザーの読み込み tokenizer = AutoTokenizer.from_pretrained( MODEL_NAME, trust_remote_code=True, use_fast=True ) # モデルの読み込み(GPU使用時は量子化を使用) if DEVICE == "cuda": quantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_quant_type="nf4", bnb_4bit_compute_dtype=torch.float16, bnb_4bit_use_double_quant=True, ) model = AutoModelForCausalLM.from_pretrained( MODEL_NAME, quantization_config=quantization_config, device_map="auto", trust_remote_code=True, torch_dtype=torch.float16, low_cpu_mem_usage=True, ) else: model = AutoModelForCausalLM.from_pretrained( MODEL_NAME, trust_remote_code=True, torch_dtype=torch.float16, low_cpu_mem_usage=True, ) print(f"Model loaded successfully on {DEVICE}") return True except Exception as e: print(f"Error loading model: {e}") return False # モデルの初期化 print("Loading model...") model_loaded = load_model() @spaces.GPU(duration=120) # 最大120秒のGPU使用 def generate_response( message, history, system_prompt=DEFAULT_SYSTEM_PROMPT, temperature=1.3, max_new_tokens=512, top_k=50, top_p=0.9, repetition_penalty=1.1 ): """メッセージに対する応答を生成""" if not model_loaded: return "モデルの読み込みに失敗しました。" try: # 会話履歴を構築 conversation = [] # システムプロンプトを追加 if system_prompt.strip(): conversation.append({"role": "system", "content": system_prompt}) # 履歴を追加 for user_msg, assistant_msg in history: conversation.append({"role": "user", "content": user_msg}) if assistant_msg: conversation.append({"role": "assistant", "content": assistant_msg}) # 現在のメッセージを追加 conversation.append({"role": "user", "content": message}) # トークナイザーでテキストを変換 input_text = tokenizer.apply_chat_template( conversation, tokenize=False, add_generation_prompt=True ) # トークン化 inputs = tokenizer(input_text, return_tensors="pt").to(DEVICE) # 生成設定 generation_kwargs = { "input_ids": inputs["input_ids"], "attention_mask": inputs["attention_mask"], "max_new_tokens": max_new_tokens, "temperature": temperature, "top_k": top_k, "top_p": top_p, "repetition_penalty": repetition_penalty, "do_sample": True, "pad_token_id": tokenizer.eos_token_id, "eos_token_id": tokenizer.eos_token_id, } # 応答生成 with torch.no_grad(): outputs = model.generate(**generation_kwargs) # 応答をデコード response = tokenizer.decode( outputs[0][len(inputs["input_ids"][0]):], skip_special_tokens=True ).strip() return response except Exception as e: return f"エラーが発生しました: {str(e)}" def clear_chat(): """チャット履歴をクリア""" return [], "" # Gradio インターフェースの作成 with gr.Blocks( title="TeenEmo Reasoning v2 - 10代向けカウンセリングAI", theme=gr.themes.Soft(), css=""".gradio-container {max-width: 1000px; margin: auto;}""" ) as demo: gr.HTML("""
このAIは、10代の若者の感情的な悩みや課題に寄り添い、思いやりのあるサポートを提供するように設計されています。
機能: 感情理解、共感的対話、建設的なアドバイス
※ このAIは補助的なツールです。深刻な問題については専門家にご相談ください。