File size: 3,067 Bytes
3184961
 
 
 
 
 
6a8cbaa
3184961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a8cbaa
 
3184961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os

import gradio as gr

from indiebot_arena.config import LOCAL_TESTING
from indiebot_arena.service.arena_service import ArenaService
from indiebot_arena.ui.battle import generate, remove_chat_tokens
from indiebot_arena.util.cache_manager import get_free_space_gb, clear_hf_cache

DESCRIPTION = "### 💬 Playground"

base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "..", ".."))
docs_path = os.path.join(base_dir, "docs", "battle_header.md")


def update_user_message(user_message, history_a):
  if not LOCAL_TESTING:
    total, _, free = get_free_space_gb("/data")
    print(f"空きディスク容量: {free:.2f} GB / {total:.2f} GB")
    if free < (total * 0.2):
      clear_hf_cache()

  new_history_a = history_a + [{"role": "user", "content": user_message}]
  return "", new_history_a


def bot1_response(history, model_id):
  history = history.copy()
  history.append({"role": "assistant", "content": ""})
  conv_history = history[:-1]
  for text in generate(conv_history, model_id):
    cleaned_text = remove_chat_tokens(text)
    history[-1]["content"] = cleaned_text
    yield history


def playground_content(dao, language):
  arena_service = ArenaService(dao)
  default_weight = "U-5GB"
  initial_models = arena_service.get_model_dropdown_list(language, default_weight)
  initial_choices = [m["label"] for m in initial_models]
  initial_value = initial_choices[0]

  def fetch_model_dropdown(weight_class):
    models = arena_service.get_model_dropdown_list(language, weight_class)
    model_labels = [m["label"] for m in models]
    update_obj_a = gr.update(choices=model_labels, value=model_labels[0])
    return update_obj_a

  with gr.Blocks(css="style.css") as battle_ui:
    gr.Markdown(DESCRIPTION)
    with open(docs_path, "r", encoding="utf-8") as f:
      markdown_content = f.read()
    gr.HTML(markdown_content)
    weight_class_radio = gr.Radio(
      choices=["U-5GB", "U-10GB"],
      label="階級",
      value=default_weight
    )
    model_dropdown_a = gr.Dropdown(
      choices=initial_choices,
      label="モデルを選択",
      value=initial_value,
      visible=True
    )
    chatbot_a = gr.Chatbot(label="Chatbot A", type="messages")
    user_input = gr.Textbox(
      placeholder="日本語でメッセージを入力...",
      submit_btn=True,
      show_label=False
    )
    user_event = user_input.submit(
      update_user_message,
      inputs=[user_input, chatbot_a],
      outputs=[user_input, chatbot_a],
      queue=False
    )
    user_event.then(
      bot1_response,
      inputs=[chatbot_a, model_dropdown_a],
      outputs=[chatbot_a],
      queue=True
    )
    weight_class_radio.change(
      fn=fetch_model_dropdown,
      inputs=weight_class_radio,
      outputs=[model_dropdown_a]
    )
    weight_class_radio.change(
      fn=lambda _: [],
      inputs=weight_class_radio,
      outputs=chatbot_a,
      queue=False
    )
    model_dropdown_a.change(
      fn=lambda _: [],
      inputs=model_dropdown_a,
      outputs=chatbot_a,
      queue=False
    )
  return battle_ui