RomZay's picture
Update app.py
3029284 verified
raw
history blame
1.9 kB
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]],
system_message,
max_tokens,
top_p,
temperature,
):
messages = []
for val in history:
if val[0]:
messages.append({"role": "user", "content": val[0]})
if val[1]:
messages.append({"role": "assistant", "content": val[1]})
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))
response_json = response.json()
if response.status_code == 200:
print(response_json)
respond = response_json["msq"]["message"][0]
yield respond
else:
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
"""
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.8, label="Top P"),
gr.Slider(minimum=0.1, maximum=1, value=0.7, step=0.1, label="Temperature"),
],
)
if __name__ == "__main__":
demo.launch()