Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import json | |
import os | |
API_URL = "https://host.palple.polrambora.com/pmsq" | |
API_TOKEN = os.getenv("POLLY") | |
headers = { | |
"Authorization": f"{API_TOKEN}", | |
"Content-Type": "application/json", | |
} | |
def respond( | |
message, | |
history: list[tuple[str, str, str, str, str, str]], # Added profile picture fields | |
system_message, | |
max_tokens, | |
top_p, | |
temperature, | |
): | |
messages = [] | |
for val in history: | |
user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic = val | |
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": "API.png" | |
}) | |
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() | |
print(response_json) | |
respond = response_json["msq"]["message"][0] | |
yield respond | |
else: | |
response_json = response.json() | |
yield "Error: " + response_json.get("error", "Unknown error occurred.") | |
""" | |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface | |
""" | |
def custom_render(message, history): | |
formatted_history = "" | |
for user_message, assistant_message, user_profile, assistant_profile, user_pic, assistant_pic in history: | |
if user_message: | |
formatted_history += f"<div style='display: flex; align-items: center;'>" | |
if user_pic: | |
formatted_history += f"<img src='{user_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>" | |
formatted_history += f"<b>{user_profile}:</b> {user_message}</div><br>" | |
if assistant_message: | |
formatted_history += f"<div style='display: flex; align-items: center;'>" | |
if assistant_pic: | |
formatted_history += f"<img src='{assistant_pic}' style='width: 40px; height: 40px; border-radius: 50%; margin-right: 10px;'>" | |
formatted_history += f"<b>{assistant_profile}:</b> {assistant_message}</div><br>" | |
return formatted_history | |
demo = gr.ChatInterface( | |
respond, | |
additional_inputs=[ | |
gr.Textbox(value="You are P-MSQ (Messaging Service Query), a friendly AI Chatbot that can help in any situations. Answer in user's language as concisely as possible.", label="System message"), | |
gr.Slider(minimum=1, maximum=2048, value=1024, step=1, label="Max new tokens"), | |
gr.Slider(minimum=0, maximum=2, value=0.8, step=0.1, label="Top P"), | |
gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature"), | |
gr.File(label="Upload User Profile Picture"), | |
gr.File(label="Upload Assistant Profile Picture") | |
], | |
chatbot_ui=custom_render | |
) | |
if __name__ == "__main__": | |
demo.launch() | |