File size: 1,903 Bytes
29b6bc4
7eb1fb9
 
 
29b6bc4
7eb1fb9
 
29b6bc4
7eb1fb9
 
 
 
29b6bc4
 
 
 
 
 
3029284
29b6bc4
 
7eb1fb9
 
29b6bc4
 
 
 
 
 
 
7eb1fb9
 
7d7d5bc
7eb1fb9
3029284
7eb1fb9
 
5c97131
7eb1fb9
 
 
 
5b32d08
7eb1fb9
 
ae26802
0a52bc2
06b7100
7eb1fb9
 
29b6bc4
 
 
 
 
 
 
a88d6d9
 
3029284
a88d6d9
29b6bc4
 
 
 
7eb1fb9
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
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()