File size: 10,780 Bytes
5741968
c0284c2
1e17339
e09e1bb
03de8b6
1e17339
5741968
1e17339
 
 
 
 
03de8b6
c0284c2
e09e1bb
b37f1c5
 
 
 
e09e1bb
b37f1c5
 
 
 
 
e09e1bb
b37f1c5
e09e1bb
 
 
 
 
 
b37f1c5
 
03de8b6
b37f1c5
 
 
 
e09e1bb
b37f1c5
 
 
a929439
08d5a0c
 
 
 
 
 
 
a929439
08d5a0c
 
 
 
 
 
 
 
e09e1bb
970a4b6
08d5a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e09e1bb
08d5a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
970a4b6
08d5a0c
 
 
 
 
 
 
 
 
 
 
 
 
e09e1bb
08d5a0c
 
 
 
 
 
 
 
 
 
5741968
08d5a0c
 
 
 
 
 
 
 
 
b37f1c5
08d5a0c
 
 
1e17339
08d5a0c
 
 
 
 
 
 
 
1e17339
08d5a0c
 
 
 
 
 
 
 
e09e1bb
08d5a0c
 
 
1e17339
08d5a0c
 
 
 
 
b37f1c5
08d5a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e17339
08d5a0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e17339
08d5a0c
1e17339
08d5a0c
 
 
970a4b6
08d5a0c
 
 
 
e09e1bb
08d5a0c
 
 
 
 
 
 
 
5741968
1e17339
 
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import gradio as gr
import torch
from transformers import AutoTokenizer, pipeline
from huggingface_hub import InferenceClient
import logging
import spaces

# ロガーの設定
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# モデル定義(ローカルモデルとAPIモデルの両方)
TEXT_GENERATION_MODELS = [
    {
        "name": "Llama-2",
        "description": "Known for its robust performance in content analysis",
        "type": "local",
        "model_path": "meta-llama/Llama-2-7b-hf"
    },
    {
        "name": "Mistral-7B",
        "description": "Offers precise and detailed text evaluation",
        "type": "local",
        "model_path": "mistralai/Mistral-7B-v0.1"
    },
    {
        "name": "Zephyr-7B",
        "description": "Specialized in understanding context and nuance",
        "type": "api",
        "model_id": "HuggingFaceH4/zephyr-7b-beta"
    }
]

CLASSIFICATION_MODELS = [
    {
        "name": "Toxic-BERT",
        "description": "Fine-tuned for toxic content detection",
        "type": "local",
        "model_path": "unitary/toxic-bert"
    }
]

class ModelManager:
    def __init__(self):
        self.tokenizers = {}
        self.pipelines = {}
        self.api_clients = {}
        self._initialize_api_clients()
        self._preload_local_models()

    def _initialize_api_clients(self):
        """Inference APIクライアントの初期化"""
        for model in TEXT_GENERATION_MODELS + CLASSIFICATION_MODELS:
            if model["type"] == "api" and "model_id" in model:
                logger.info(f"Initializing API client for {model['name']}")
                self.api_clients[model["model_id"]] = InferenceClient(
                    model["model_id"],
                    token=True  # HFトークンを使用
                )

    def _preload_local_models(self):
        """ローカルモデルを事前ロード"""
        logger.info("Preloading local models at application startup...")
        
        # テキスト生成モデル
        for model in TEXT_GENERATION_MODELS:
            if model["type"] == "local" and "model_path" in model:
                model_path = model["model_path"]
                try:
                    logger.info(f"Preloading text generation model: {model_path}")
                    self.tokenizers[model_path] = AutoTokenizer.from_pretrained(model_path)
                    self.pipelines[model_path] = pipeline(
                        "text-generation",
                        model=model_path,
                        tokenizer=self.tokenizers[model_path],
                        torch_dtype=torch.bfloat16,
                        trust_remote_code=True,
                        device_map="auto"
                    )
                    logger.info(f"Model preloaded successfully: {model_path}")
                except Exception as e:
                    logger.error(f"Error preloading model {model_path}: {str(e)}")
        
        # 分類モデル
        for model in CLASSIFICATION_MODELS:
            if model["type"] == "local" and "model_path" in model:
                model_path = model["model_path"]
                try:
                    logger.info(f"Preloading classification model: {model_path}")
                    self.tokenizers[model_path] = AutoTokenizer.from_pretrained(model_path)
                    self.pipelines[model_path] = pipeline(
                        "text-classification",
                        model=model_path,
                        tokenizer=self.tokenizers[model_path],
                        torch_dtype=torch.bfloat16,
                        trust_remote_code=True,
                        device_map="auto"
                    )
                    logger.info(f"Model preloaded successfully: {model_path}")
                except Exception as e:
                    logger.error(f"Error preloading model {model_path}: {str(e)}")

    @spaces.GPU
    def generate_text_local(self, model_path, text):
        """ローカルモデルでのテキスト生成"""
        try:
            logger.info(f"Running local text generation with {model_path}")
            outputs = self.pipelines[model_path](
                text,
                max_new_tokens=100,
                do_sample=False,
                num_return_sequences=1
            )
            return outputs[0]["generated_text"]
        except Exception as e:
            logger.error(f"Error in local text generation with {model_path}: {str(e)}")
            return f"Error: {str(e)}"

    def generate_text_api(self, model_id, text):
        """API経由でのテキスト生成"""
        try:
            logger.info(f"Running API text generation with {model_id}")
            response = self.api_clients[model_id].text_generation(
                text, 
                max_new_tokens=100, 
                temperature=0.7
            )
            return response
        except Exception as e:
            logger.error(f"Error in API text generation with {model_id}: {str(e)}")
            return f"Error: {str(e)}"

    @spaces.GPU
    def classify_text_local(self, model_path, text):
        """ローカルモデルでのテキスト分類"""
        try:
            logger.info(f"Running local classification with {model_path}")
            result = self.pipelines[model_path](text)
            return str(result)
        except Exception as e:
            logger.error(f"Error in local classification with {model_path}: {str(e)}")
            return f"Error: {str(e)}"

    def classify_text_api(self, model_id, text):
        """API経由でのテキスト分類"""
        try:
            logger.info(f"Running API classification with {model_id}")
            response = self.api_clients[model_id].text_classification(text)
            return str(response)
        except Exception as e:
            logger.error(f"Error in API classification with {model_id}: {str(e)}")
            return f"Error: {str(e)}"

    def run_models(self, text, selected_types):
        """選択されたタイプのモデルで分析を実行"""
        results = []
        
        # テキスト生成モデルの実行
        for model in TEXT_GENERATION_MODELS:
            if model["type"] in selected_types:
                if model["type"] == "local":
                    result = self.generate_text_local(model["model_path"], text)
                else:  # api
                    result = self.generate_text_api(model["model_id"], text)
                results.append(f"{model['name']}: {result}")
        
        # 分類モデルの実行
        for model in CLASSIFICATION_MODELS:
            if model["type"] in selected_types:
                if model["type"] == "local":
                    result = self.classify_text_local(model["model_path"], text)
                else:  # api
                    result = self.classify_text_api(model["model_id"], text)
                results.append(f"{model['name']}: {result}")
        
        # 結果リストの長さを調整
        while len(results) < len(TEXT_GENERATION_MODELS) + len(CLASSIFICATION_MODELS):
            results.append("")
        
        return results

class UIManager:
    def __init__(self, model_manager):
        self.model_manager = model_manager
        
    def create_ui(self):
        """UIの作成"""
        with gr.Blocks() as demo:
            # ヘッダー
            gr.Markdown("""
            # Toxic Eye (Class-based Version)
            This system evaluates the toxicity level of input text using both local models and Inference API.
            """)
            
            # 入力セクション
            with gr.Row():
                input_text = gr.Textbox(
                    label="Input Text",
                    placeholder="Enter text to analyze...",
                    lines=3
                )
            
            # フィルターセクション
            with gr.Row():
                filter_checkboxes = gr.CheckboxGroup(
                    choices=["local", "api"],
                    value=["local", "api"],
                    label="Filter Models",
                    info="Choose which types of models to use",
                    interactive=True
                )
            
            # 実行ボタン
            with gr.Row():
                invoke_button = gr.Button(
                    "Analyze Text",
                    variant="primary",
                    size="lg"
                )
            
            # モデル出力表示エリア
            all_outputs = []
            
            with gr.Tabs():
                # テキスト生成モデルのタブ
                with gr.Tab("Text Generation Models"):
                    for model in TEXT_GENERATION_MODELS:
                        with gr.Group():
                            gr.Markdown(f"### {model['name']} ({model['type']})")
                            output = gr.Textbox(
                                label=f"{model['name']} Output",
                                lines=5,
                                interactive=False,
                                info=model["description"]
                            )
                            all_outputs.append(output)
                
                # 分類モデルのタブ
                with gr.Tab("Classification Models"):
                    for model in CLASSIFICATION_MODELS:
                        with gr.Group():
                            gr.Markdown(f"### {model['name']} ({model['type']})")
                            output = gr.Textbox(
                                label=f"{model['name']} Output",
                                lines=5,
                                interactive=False,
                                info=model["description"]
                            )
                            all_outputs.append(output)
            
            # イベント接続
            invoke_button.click(
                fn=self.handle_invoke,
                inputs=[input_text, filter_checkboxes],
                outputs=all_outputs
            )
        
        return demo
    
    def handle_invoke(self, text, selected_types):
        """モデル実行をハンドリング"""
        return self.model_manager.run_models(text, selected_types)

class ToxicityApp:
    def __init__(self):
        self.model_manager = ModelManager()
        self.ui_manager = UIManager(self.model_manager)
    
    def run(self):
        """アプリを起動"""
        demo = self.ui_manager.create_ui()
        demo.launch()

def main():
    app = ToxicityApp()
    app.run()

if __name__ == "__main__":
    main()