File size: 5,198 Bytes
0157229
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from app_utils import *
from state import State

class StateManager:
    """
    Manages the state of both single and dual chatbot interfaces.
    
    Attributes:
        single_bot (State): State object for the single chatbot interface
        dual_bot1 (State): State object for the first bot in dual interface
        dual_bot2 (State): State object for the second bot in dual interface
        single_current_category (str): Current category selected in single interface
        dual_current_category (str): Current category selected in dual interface
    """
    def __init__(self, database):
        self.single_bot = State(MODELS[0], DEFAULT_TEMPERATURE_1, database, SINGLE_MODEL_BOT_EXAMPLE_SETTING, "single_model_bot")
        self.dual_bot1 = State(MODELS[0], DEFAULT_TEMPERATURE_1, database, DUAL_MODEL_BOT_1_EXAMPLE_SETTING, "dual_model_bot_1")
        self.dual_bot2 = State(MODELS[1], DEFAULT_TEMPERATURE_2, database, DUAL_MODEL_BOT_2_EXAMPLE_SETTING, "dual_model_bot_2")
        self.single_current_category = CATEGORIES[0]
        self.dual_current_category = CATEGORIES[0]
        self.initialize()
        
    def initialize(self):
        self.single_bot.initialize(self.single_current_category)
        self.dual_bot1.initialize(self.dual_current_category)
        self.dual_bot2.initialize(self.dual_current_category)
    
    def add_message_helper(self, message, bot):
        for x in message["files"]:
            bot.history.append({"role": "user", "content": {"path": x}})
        if message["text"] is not None:
            bot.history.append({"role": "user", "content": message["text"]})
            bot.test_entry["question"] = [{"role": "user", "content": message["text"]}]
    
    def add_message(self, message, target=None):
        if target is None:
            self.add_message_helper(message, self.single_bot)
            return self.single_bot.history, gr.MultimodalTextbox(value=None, interactive=False)
        if target in ["Model 1", "Both"]:
            print("Adding message to bot1: {message}")
            self.add_message_helper(message, self.dual_bot1)
        if target in ["Model 2", "Both"]:
            print("Adding message to bot2: {message}")
            self.add_message_helper(message, self.dual_bot2)
        return self.dual_bot1.history, self.dual_bot2.history, gr.MultimodalTextbox(value=None, interactive=False)
    
    def get_reponse_single(self):
        bot_generation = self.single_bot.response()
        while True:
            stop = True
            try:
                generation_history = next(bot_generation)
                stop = False
                yield generation_history
            except StopIteration:
                pass
            if stop:
                break
            
    def get_reponse_dual(self, target):
        if target != "Both":
            bot_generation = self.dual_bot1.response() if target == "Model 1" else self.dual_bot2.response()
            while True:
                stop = True
                try:
                    generation_history = next(bot_generation)
                    stop = False
                    if target == "Model 1":
                        yield generation_history, self.dual_bot2.history
                    else:
                        yield self.dual_bot1.history, generation_history
                except StopIteration:
                    pass
                if stop:
                    break
        else:
            bot1_generation = self.dual_bot1.response()
            bot2_generation = self.dual_bot2.response()
            while True:
                stop = True
                try:
                    generation_history_1 = next(bot1_generation)
                    stop = False
                except StopIteration:
                    pass
                try:
                    generation_history_2 = next(bot2_generation)
                    stop = False
                except StopIteration:
                    pass
                yield generation_history_1, generation_history_2
                if stop:
                    break
                
    def single_update_category_and_load_config(self, category):
        self.single_current_category = category
        self.single_bot.update_category_and_load_config(category)
        return category
    
    def dual_update_category_and_load_config(self, category):
        self.dual_current_category = category
        self.dual_bot1.update_category_and_load_config(category)
        self.dual_bot2.update_category_and_load_config(category)
        return category
    
    def single_load_example_and_update(self, example):
        model, temp, category, message = self.single_bot.load_example_and_update(example)
        return model, temp, category, message
    
    def dual_load_example_and_update(self, example):
        model_1, temp_1, category_1, message_1 = self.dual_bot1.load_example_and_update(example)
        model_2, temp_2, category_2, message_2 = self.dual_bot2.load_example_and_update(example)
        assert category_1 == category_2
        assert message_1 == message_2
        return model_1, temp_1, model_2, temp_2, category_1, message_1