File size: 6,340 Bytes
83615d5
5a4c72b
 
83615d5
 
0abff28
 
 
 
 
 
 
5a4c72b
0abff28
 
 
5a4c72b
0abff28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0450614
0abff28
 
 
 
5a4c72b
0abff28
 
 
bbc84e6
 
 
0abff28
5a4c72b
2992155
bbc84e6
2992155
8528615
 
 
0287ecc
2992155
 
0287ecc
bbc84e6
0abff28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
460824b
0abff28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import os
import json
import time
import gradio as gr
import google.generativeai as genai
from huggingface_hub import HfApi, hf_hub_download
from collections import deque

# APIキーの設定
genai.configure(api_key=os.getenv("GOOGLE_API_KEY"))
hf_token = os.getenv("HF_TOKEN")
hf_api = HfApi(token=hf_token)

# Hugging Faceデータセットの設定
REPO_ID = "Sakalti/Gemini_ai_chat"
DATASET_FILE = "characters.jsonl"

# Geminiモデルの初期化
model = genai.GenerativeModel(model_name='gemini-2.0-flash')

# レートリミット用
request_times = deque()
REQUEST_LIMIT = 15
TIME_WINDOW = 60  # 秒

def is_rate_limited():
    now = time.time()
    while request_times and now - request_times[0] > TIME_WINDOW:
        request_times.popleft()
    if len(request_times) >= REQUEST_LIMIT:
        return True
    else:
        request_times.append(now)
        return False

# キャラクターの取得関数
def fetch_characters():
    try:
        file_path = hf_hub_download(repo_id=REPO_ID, filename=DATASET_FILE, repo_type="dataset", token=hf_token)
        with open(file_path, "r", encoding="utf-8") as f:
            return [json.loads(line) for line in f if line.strip()]
    except Exception as e:
        print(f"[ERROR] fetch_characters: {e}")
        return []

# キャラクターのアップロード関数
def upload_character(name, prompt):
    characters = fetch_characters()
    characters.append({"name": name, "prompt": prompt})
    temp_file = "temp_characters.jsonl"
    with open(temp_file, "w", encoding="utf-8") as f:
        for char in characters:
            f.write(json.dumps(char, ensure_ascii=False) + "\n")
    hf_api.upload_file(
        path_or_fileobj=temp_file,
        path_in_repo=DATASET_FILE,
        repo_id=REPO_ID,
        repo_type="dataset"
    )
    os.remove(temp_file)

# 応答生成関数
def generate_response(message, history, temperature, top_p, top_k, max_output_tokens, system_prompt):
    if is_rate_limited():
        return "⚠️ 1分間に15回までです。しばらく待ってください。", history, history

    gemini_history = []
    if system_prompt:
        gemini_history.append({"role": "user", "parts": [f"以下の指示に従ってAIキャラとして振る舞ってください:\n{system_prompt}"]})
    for user, bot in history:
        gemini_history.append({"role": "user", "parts": [user]})
        gemini_history.append({"role": "model", "parts": [bot]})
    gemini_history.append({"role": "user", "parts": [message]})

    response = model.generate_content(
        gemini_history,
        generation_config={
            "temperature": temperature,
            "top_p": top_p,
            "top_k": top_k,
            "max_output_tokens": int(max_output_tokens),
        }
    )

    history.append((message, response.text))
    return "", history, history


# ===== 起動後にテーマ切替する仕組み =====
THEMES = {
    "Eternalstar": "Sakalti/Eternalstar",
    "Mountainrainbow": "Sakalti/mountanrainbow"
}

def switch_theme(choice):
    """
    JSで<head>にあるテーマCSSリンクを書き換える。
    """
    css_link = f"https://huggingface.co/spaces/{THEMES[choice]}/resolve/main/theme.css"
    js_code = f"""
    () => {{
        let old = document.getElementById("dynamic-theme");
        if (old) old.remove();
        let link = document.createElement("link");
        link.id = "dynamic-theme";
        link.rel = "stylesheet";
        link.href = "{css_link}";
        document.head.appendChild(link);
    }}
    """
    return gr.HTML.update(value=""), gr.update(), js_code


with gr.Blocks(theme=THEMES["Mountainrainbow"]) as demo:
    gr.Markdown("## Gemini AIキャラクターチャット")

    # テーマ切り替えドロップダウン
    theme_dropdown = gr.Dropdown(choices=list(THEMES.keys()), value="Eternalstar", label="テーマ切り替え")

    with gr.Tab("チャット"):
        chatbot = gr.Chatbot()
        msg = gr.Textbox(placeholder="メッセージを入力...")
        state = gr.State([])
        system_prompt = gr.Textbox(label="キャラのシステムプロンプト", lines=4)

        with gr.Row():
            temperature = gr.Slider(0.0, 1.0, value=0.7, label="Temperature")
            top_p = gr.Slider(0.0, 1.0, value=0.9, label="Top-p")
            top_k = gr.Slider(1, 100, value=40, label="Top-k")
            max_output_tokens = gr.Number(value=1024, label="Max Output Tokens", precision=0)

        msg.submit(generate_response,
                   inputs=[msg, state, temperature, top_p, top_k, max_output_tokens, system_prompt],
                   outputs=[msg, chatbot, state])

    with gr.Tab("キャラクター投稿"):
        char_name = gr.Textbox(label="キャラクター名")
        char_prompt = gr.Textbox(label="システムプロンプト", lines=5)
        submit_char = gr.Button("キャラクターを追加")
        char_status = gr.Textbox(label="ステータス", interactive=False)

        def post_character(name, prompt):
            try:
                upload_character(name, prompt)
                return "キャラクターをアップロードしました。"
            except Exception as e:
                return f"失敗しました: {e}"

        submit_char.click(post_character, inputs=[char_name, char_prompt], outputs=[char_status])

    with gr.Tab("キャラクター選択"):
        character_list = gr.Dropdown(choices=[], label="使用するキャラクターを選択")

        def refresh_characters():
            return gr.update(choices=[c["name"] for c in fetch_characters()])

        def load_prompt(name):
            for c in fetch_characters():
                if c.get("name") == name:
                    print(f"[DEBUG] 読み込み成功: {c}")
                    return c.get("prompt", "")
            print("[DEBUG] キャラが見つかりません")
            return ""

        character_list.change(load_prompt, inputs=[character_list], outputs=[system_prompt])
        demo.load(refresh_characters, outputs=[character_list])

    # ドロップダウンでテーマ切り替え
    theme_dropdown.change(
        fn=switch_theme,
        inputs=[theme_dropdown],
        outputs=[gr.HTML(), gr.Textbox(), gr.HTML()],
        _js="(theme) => { return theme; }"
    )

demo.launch()