Spaces:
Build error
Build error
| import gradio as gr | |
| import requests | |
| import json | |
| import os | |
| API_URL = "https://host.palple.polrambora.com/pmsq" | |
| API_TOKEN = os.getenv("POLLY") | |
| body = { | |
| "kind": "PER_DATA", | |
| "identificator": "", | |
| } | |
| 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(api_key): | |
| test_data = {"kind": "PER_DATA","identificator": f"{api_key}"} | |
| test_headers = { | |
| "Authorization": f"{API_TOKEN}", | |
| "Content-Type": "application/json", | |
| } | |
| print("hello") | |
| response = requests.post("https://polrambora.com/api/v2/checkSDPA", headers=test_headers, data=json.dumps(test_data)) | |
| if response.status_code == 200: | |
| body["identificator"] = f"{api_key}" | |
| return True | |
| else: | |
| return False | |
| def respond(message, history, system_message, max_tokens, top_p, temperature): | |
| 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)) | |
| return history, assistant_reply | |
| else: | |
| return history, "Error: " + response.json().get("error", "Unknown error occurred.") | |
| def load_conversation_from_file(json_file): | |
| with open(json_file, 'r') as file: | |
| data = json.load(file) | |
| history = [] | |
| for msg in data['conversation']: | |
| user_message = msg.get('user_message', "") | |
| assistant_message = msg.get('assistant_message', "") | |
| history.append((user_message, assistant_message, "You", "P-ALPLE", USER_PIC_PATH, ASSISTANT_PIC_PATH)) | |
| return history | |
| # Message rendering | |
| def render_message(history): | |
| messages_html = "" | |
| 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"{user_message}</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"{assistant_message}</div><br>" | |
| return messages_html | |
| with gr.Blocks(css=".chatbox {height: 400px; overflow-y: auto; border: 1px solid #ccc; padding: 10px; background-color: #f9f9f9;}") 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_key_input = gr.Textbox(placeholder="Enter your API key or enter your Discord UserID", label="API Key/User ID") | |
| auth_button = gr.Button("Authorize") | |
| 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_message = gr.Textbox(value="You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations.", label="System message") | |
| 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("") | |
| load_btn = gr.Button("Load Conversation") | |
| json_file_input = gr.Textbox(placeholder="Path to .json file", label="JSON File Path") | |
| def load_conversation(file_path): | |
| history = load_conversation_from_file(file_path) | |
| return render_message(history), history | |
| load_btn.click(load_conversation, inputs=json_file_input, outputs=[chatbot_output, history_state]) | |
| def user_interaction(message, history, system_message, max_tokens, top_p, temperature): | |
| history, assistant_reply = respond(message, history, system_message, max_tokens, top_p, temperature) | |
| return render_message(history), history, "", message | |
| def regenerate_response(history, last_message, system_message, max_tokens, top_p, temperature): | |
| return "", [] | |
| msg_input.submit(user_interaction, | |
| inputs=[msg_input, history_state, system_message, 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, system_message, max_tokens, top_p, temperature], | |
| outputs=[chatbot_output, history_state, msg_input, last_message_state]) | |
| regen_btn.click(regenerate_response, | |
| inputs=[history_state, last_message_state, system_message, max_tokens, top_p, temperature], | |
| outputs=[chatbot_output, history_state]) | |
| def authorize_and_proceed(api_key): | |
| if authorize(api_key): | |
| return gr.update(visible=False), gr.update(visible=True) | |
| else: | |
| return gr.update(visible=True), gr.update(visible=False) | |
| auth_button.click(authorize_and_proceed, inputs=api_key_input, outputs=[auth_view, chat_view]) | |
| if __name__ == "__main__": | |
| demo.launch() | |