File size: 10,308 Bytes
29b6bc4
7eb1fb9
 
 
29b6bc4
7eb1fb9
f565471
 
5bf6ac7
653b1c0
 
0f46d61
befce47
481e164
426dc72
ad4d321
481e164
653b1c0
b7531ef
653b1c0
481e164
ff8ac04
481e164
602b5a2
ad4d321
b7531ef
602b5a2
481e164
 
 
ff8ac04
3411c78
 
f565471
83bfc19
f565471
 
 
befce47
 
f565471
3411c78
ff8ac04
481e164
ff8ac04
481e164
602b5a2
 
 
 
 
 
481e164
 
653b1c0
2827a07
602b5a2
befce47
f565471
 
 
5bf6ac7
7eb1fb9
2827a07
bfc9a54
 
 
 
 
2827a07
bfc9a54
 
 
 
 
 
2827a07
bfc9a54
29b6bc4
7eb1fb9
 
7d7d5bc
7eb1fb9
3029284
7eb1fb9
 
5c97131
7eb1fb9
 
3411c78
7eb1fb9
2827a07
7eb1fb9
bfc9a54
2827a07
3411c78
8cfccba
cba1cd6
83bfc19
f565471
8cfccba
7eb1fb9
2827a07
29b6bc4
8cfccba
94c332d
42dcabc
d1ea71a
52b5151
 
42dcabc
 
 
 
 
 
52b5151
e78e0df
42dcabc
 
 
 
52b5151
ac8a166
d028f9a
04c4599
d028f9a
04c4599
d028f9a
 
 
 
 
 
 
 
 
 
 
04c4599
 
38c0cd8
d028f9a
3bd875a
d1ea71a
2827a07
d1ea71a
653b1c0
fd46984
ae04509
8de58dc
653b1c0
8de58dc
0f46d61
653b1c0
 
aa7b3a1
d7c88bd
aa7b3a1
653b1c0
 
6f1e86c
653b1c0
 
 
f630f1d
653b1c0
 
2827a07
befce47
 
 
 
653b1c0
 
 
 
 
0f46d61
653b1c0
 
2827a07
d98fb67
7dd2b2c
94c332d
7dd2b2c
94c332d
86b031f
94c332d
f630f1d
5a4637d
d98fb67
653b1c0
bfc9a54
83bfc19
 
 
 
 
 
 
 
 
 
653b1c0
7dd2b2c
 
29b6bc4
653b1c0
7dd2b2c
 
0f46d61
81d4e26
 
653b1c0
0f46d61
426dc72
5bf6ac7
5e06b4e
83bfc19
d1ea71a
653b1c0
5e06b4e
d1ea71a
8de58dc
befce47
 
5e06b4e
befce47
5e06b4e
befce47
5e06b4e
 
c011795
befce47
d1ea71a
 
29e5b10
befce47
50394da
29b6bc4
426dc72
6b3434b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import json
import os

API_URL = "https://host.palple.polrambora.com/pmsq"

sessions = {}
PRIMARY_SYSTEM_INSTRUCTIONS = "You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations"
ASSISTANT_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/API.png"  
USER_PIC_PATH = "https://huggingface.co/spaces/PLRMB/P-MSQ-API-PREVIEW/resolve/main/usr.png"

def authorize(user, api_key, system_message):
    test_data = {
        "user": user,
        "key": api_key
    }
    test_headers = {
        'Accept': 'application/json'
    }

    print("Preparing to send the request...") 
    try:
        response = requests.post(
            "https://host.palple.polrambora.com/check_key_impv",
            json=test_data,
        )

        if response.status_code == 200:
            response_json = response.json()
            print(f"Response: {response_json}")
            
            if api_key not in sessions:
                sessions[api_key] = {
                    "history": [], 
                    "headers": {   
                        "authorization": api_key,
                        "Content-Type": 'application/json'
                    },
                    "system_message": system_message 
                }
                
            return True
        else:
            print(f"Failed request - Status code: {response.status_code}, Response: {response.text}")
            return False
    except requests.exceptions.Timeout:
        print("Request timed out")
        return False
    except requests.exceptions.ConnectionError:
        print("Network problem occurred (DNS failure, refused connection, etc.)")
        return False
    except Exception as e:
        print(f"Exception occurred: {str(e)}")
        return False


def respond(message, api_key, max_tokens, top_p, temperature):
    session = sessions.get(api_key, {})
    history = session.get("history", [])
    headers = session.get("headers", {})
    system_message = session.get("system_message", PRIMARY_SYSTEM_INSTRUCTIONS)
    messages = []
    for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
        if user_message:
            messages.append({
                "role": "user",
                "content": user_message,
                "profile": user_profile,
                "picture": user_pic
            })
        if assistant_message:
            messages.append({
                "role": "assistant",
                "content": assistant_message,
                "profile": assistant_profile,
                "picture": assistant_pic
            })

    data = {
        "preferences": {
            "max_char": max_tokens,
            "temperature": temperature,
            "top_p": top_p,
            "system_message": system_message
        },
        "conversation_history": messages,
        "input": message
    }
    
    response = requests.post(API_URL, headers=headers, data=json.dumps(data))
    
    if response.status_code == 200:
        response_json = response.json()
        assistant_reply = response_json["msq"]["message"][0]
        

        history.append((message, assistant_reply, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH))
        sessions[api_key]["history"] = history  
        
        return history, assistant_reply  
    else:
        return history, "Error: " + response.json().get("error", "Unknown error occurred.")



def render_message(history):
    messages_html = """
    <div id="chatbox-container" class="chatbox" style="height: 400px; overflow-y: auto; border: 1px solid #262626; padding: 10px; background-color: #171717;">
        <div id="messages">"""

    for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history:
        if user_message:
            messages_html += f"<div style='display: flex; align-items: center; margin-bottom: 10px;'>"
            if user_pic:
                messages_html += f"<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
            messages_html += f"<span style='color: white;'>{user_message}</span></div><br>"

        if assistant_message:
            messages_html += f"<div style='display: flex; align-items: center; margin-bottom: 10px;'>"
            if assistant_pic:
                messages_html += f"<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>"
            messages_html += f"<span style='color: white;'>{assistant_message}</span></div><br>"

    messages_html += "</div></div>" 

    scroll_script = """
    <script>
    const container = document.getElementById('chatbox-container');

    const observer = new MutationObserver(() => {
        container.scrollTop = container.scrollHeight;  // Force scroll to bottom after re-render
    });

    observer.observe(container, { childList: true, subtree: true });

    window.onload = function() {
        container.scrollTop = container.scrollHeight;
    };
    </script>
    """

    return messages_html + scroll_script

with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid #262626; padding: 10px; background-color: #171717;}") as demo:

    with gr.Column(visible=True) as auth_view:
        gr.Markdown("## P-MSQ Authorization")
        gr.Markdown("P-MSQ is in closed alpha test! The model, api and more are subject to change. Even though the space is public, P-MSQ is restricted for public use, available for testers.")
        api_user_input = gr.Textbox(placeholder="snowflake", label="UserID", type='email')
        api_key_input = gr.Textbox(placeholder="Enter your API key", label="Token", type='password')
        auth_button = gr.Button("Authorize")
        auth_status = gr.Textbox(label="Authorization Status", interactive=False)

    with gr.Column(visible=False) as chat_view:
        gr.Markdown("## P-MSQ Chat Interface")

        chatbot_output = gr.HTML(elem_id="chatbox")

        msg_input = gr.Textbox(
            show_label=False, 
            placeholder="Type your message and press Shift+Enter...", 
            lines=2,
            elem_id="input-text"
        )

        send_btn = gr.Button("Send")
        regen_btn = gr.Button("Clear")

        system_instructions_input = gr.Textbox(placeholder="Enter custom instructions (optional)", 
                                               label="Custom System Instructions", 
                                               lines=2)
        save_instructions_btn = gr.Button("Save Instructions")        
        gr.Markdown("### Settings")
        
        max_tokens = gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens")
        top_p = gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P")
        temperature = gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature")

        history_state = gr.State([])  
        last_message_state = gr.State("")  

        def user_interaction(message, history, api_key, max_tokens, top_p, temperature):
           history, assistant_reply = respond(message, api_key, max_tokens, top_p, temperature)

           full_history_html = render_message(history)

           return full_history_html, history, "", message



        def regenerate_response(history, last_message, max_tokens, top_p, temperature):
            return "", []

        def clear_history(api_key):
            if api_key in sessions:
                sessions[api_key]["history"] = []  
            return "", []

        def load_conversation(api_key):
            session = sessions.get(api_key, {})
            history = session.get("history", [])
            return render_message(history), history

        msg_input.submit(user_interaction, 
                 inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature], 
                 outputs=[chatbot_output, history_state, msg_input, last_message_state])

        send_btn.click(user_interaction, 
                inputs=[msg_input, history_state, api_key_input, max_tokens, top_p, temperature], 
                outputs=[chatbot_output, history_state, msg_input, last_message_state])

        regen_btn.click(clear_history, 
                        inputs=[api_key_input], 
                        outputs=[chatbot_output, history_state])

    def authorize_and_proceed(user, api_key):
        if authorize(user, api_key, PRIMARY_SYSTEM_INSTRUCTIONS):
            gr.Info("Loading, please wait.")
            messages_html, history = load_conversation(api_key)
            return gr.update(visible=False), gr.update(visible=True), messages_html, history
        else:
            gr.Warning("Incorrect userid/token")
            return gr.update(visible=True), gr.update(visible=False), auth_status.update(value="Invalid userid/token")

    def save_custom_instructions(api_key, custom_instructions):
        if api_key in sessions:
            gr.Info("Instructions updated, we recommend to start the new conversation to make it more efficient.")
            sessions[api_key]["system_message"] = custom_instructions  
            return "Instructions updated!", gr.update(value="")
        else:
            gr.Warning("Your session has been expired, please refresh the page and login again.")
            return "Session not found.", gr.update(value="")

            
    auth_button.click(authorize_and_proceed, inputs=[api_user_input, api_key_input], outputs=[auth_view, chat_view, chatbot_output, history_state])
    save_instructions_btn.click(save_custom_instructions, inputs=[api_key_input, system_instructions_input], outputs=auth_status)
    demo.load(lambda: None, [], None)


if __name__ == "__main__":
    demo.queue = False
    demo.launch(
        js="""
    function scrollToBottom() {
        const container = document.getElementById('chatbox-container');
        if (container) {
            container.scrollTop = container.scrollHeight;
        }
    }

    window.onload = scrollToBottom;

    const observer = new MutationObserver(scrollToBottom);
    observer.observe(document.getElementById('chatbox-container'), { childList: true, subtree: true });
        """
)