File size: 7,053 Bytes
618f8fd
b2c0464
618f8fd
bc99a91
 
b2c0464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
618f8fd
 
 
b2c0464
 
 
 
618f8fd
b2c0464
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc99a91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2c0464
 
 
 
 
 
 
 
618f8fd
b2c0464
 
 
 
 
 
 
 
 
 
618f8fd
 
 
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
import gradio as gr
import requests

from theme import logo, theme

DEFAULT_DEPLOYMENT_URL = "https://api.app.deeploy.ml/workspaces/708b5808-27af-461a-8ee5-80add68384c7/deployments/a0a5d36d-ede6-4c53-8705-e4a8727bb0b7/predict"

DEFAULT_PROMPTS = [
    ["What are requirements for a high-risk AI system?"],
    ["Can you help me understand AI content moderation guidelines and limitations?"],
]

MAX_TOKENS = 800
TEMPERATURE = 0.7
TOP_P = 0.95

ERROR_401 = "Error: Invalid Deployment token"
ERROR_403 = "Error: No valid permissions for this Deployment token"
ERROR_404 = "Error: Deployment not found. Check the API URL."

indexed_prediction_log_ids = {}


def respond(
    message: str,
    history: list,
    api_url: str,
    deployment_token: str,
):
    formatted_history = []

    if history and isinstance(history[0], list):
        for user_msg, assistant_msg in history:
            if user_msg:
                formatted_history.append(message_from_user(user_msg))
            if assistant_msg:
                formatted_history.append(message_from_assistant(assistant_msg))
    else:
        formatted_history = history

    messages = [message_from_system("Your are a friendly Chatbot.")]
    messages.extend(formatted_history)

    if message:
        messages.append(message_from_user(message))

    headers = get_headers(deployment_token)
    payload = get_prediction_payload(messages)
    predict_url = get_predict_url(api_url)

    response = requests.post(predict_url, json=payload, headers=headers)

    new_history = formatted_history.copy()

    if message:
        new_history.append(message_from_user(message))

    if response.status_code != 201:
        append_error_to_history(new_history, response)
        return new_history

    try:
        response_data = response.json()

        if isinstance(response_data, dict) and "choices" in response_data:
            if (
                len(response_data["choices"]) > 0
                and "message" in response_data["choices"][0]
            ):
                content = response_data["choices"][0]["message"].get("content", "")
                prediction_log_id = response_data["predictionLogIds"][0]
                indexed_prediction_log_ids[len(new_history)] = prediction_log_id
                new_history.append(message_from_assistant(content))
                return new_history
        else:
            new_history.append(
                message_from_assistant(
                    f"Error: Unexpected response format: {response_data}"
                )
            )
            return new_history

    except Exception as error:
        new_history.append(
            message_from_assistant(f"Error parsing API response: {str(error)}")
        )
        return new_history


def evaluate(
    like_data: gr.LikeData,
    api_url: str,
    deployment_token: str,
) -> str | None:
    prediction_log_id = indexed_prediction_log_ids.get(like_data.index)

    headers = get_headers(deployment_token)
    evaluate_url = get_evaluation_url(api_url, prediction_log_id)
    evaluation_payload = get_evaluation_payload(like_data.liked)

    response = requests.post(evaluate_url, json=evaluation_payload, headers=headers)

    if response.status_code != 201:
        error_msg = "Error: Failed to evaluate the prediction, does your token have the right permissions?"
        return error_msg


def get_prediction_payload(messages: list) -> dict:
    return {
        "messages": messages,
        "max_tokens": MAX_TOKENS,
        "temperature": TEMPERATURE,
        "top_p": TOP_P,
    }


def get_evaluation_payload(liked: bool) -> dict:
    if liked:
        return {"agree": True, "comment": "Clicked thumbs up in the chat"}
    else:
        return {
            "agree": False,
            "comment": "Clicked thumbs down in the chat",
            "desiredOutput": {"predictions": ["A new example output"]},
        }


def get_headers(bearer_token: str) -> dict:
    return {
        "Authorization": f"Bearer {bearer_token}",
        "Content-Type": "application/json",
    }


def append_error_to_history(history: list, response: requests.Response) -> None:
    if response.status_code == 401:
        history.append(message_from_assistant(ERROR_401))
    elif response.status_code == 403:
        history.append(message_from_assistant(ERROR_403))
    elif response.status_code == 404:
        history.append(message_from_assistant(ERROR_404))
    else:
        history.append(
            message_from_assistant(
                f"Error: API returned status code {response.status_code}"
            )
        )


def message_from_assistant(message: str) -> dict:
    return {"role": "assistant", "content": message}


def message_from_user(message: str) -> dict:
    return {"role": "user", "content": message}


def message_from_system(message: str) -> dict:
    return {"role": "system", "content": message}


def get_base_url(url: str) -> str:
    if url.endswith("/predict"):
        return url.split("/predict")[0]
    else:
        if url.endswith("/"):
            return url[:-1]
        else:
            return url


def get_predict_url(url: str) -> str:
    return get_base_url(url) + "/predict"


def get_evaluation_url(url: str, prediction_log_id: str) -> str:
    return (
        get_base_url(url)
        + "/predictionLogs/"
        + prediction_log_id
        + "/evaluatePrediction"
    )


with gr.Blocks(theme=theme, mode="light") as demo:
    with gr.Row():
        with gr.Column(scale=1):
            with gr.Row():
                gr.HTML(f"""
                    <div style="display: flex; align-items: center; column-gap: 8px;">
                        {logo}
                        <h1 style="margin: 0;">Deeploy OpenAI</h1>
                    </div>
                """)
            api_url = gr.Textbox(
                value=DEFAULT_DEPLOYMENT_URL, label="Deeploy API URL", type="text"
            )
            deployment_token = gr.Textbox(label="Deployment token", type="password")

        with gr.Column(scale=2):
            chatbot = gr.Chatbot(
                height=600,
                type="messages",
                render_markdown=True,
                show_copy_button=True,
            )
            msg = gr.Textbox(
                label="Message",
                placeholder="Type your message here...",
                show_label=False,
                submit_btn="Send",
            )
            gr.Examples(
                examples=DEFAULT_PROMPTS,
                inputs=[msg],
            )

    msg.submit(
        respond,
        inputs=[msg, chatbot, api_url, deployment_token],
        outputs=chatbot,
    ).then(lambda: "", None, msg, queue=False)

    error_output = gr.Textbox(visible=False)

    chatbot.like(
        evaluate,
        inputs=[api_url, deployment_token],
        outputs=[error_output],
        like_user_message=False,
    ).success(
        lambda msg: gr.Info(msg) if msg else None,
        [error_output],
        None,
    )

if __name__ == "__main__":
    demo.launch()