File size: 4,802 Bytes
bf8643c
c882f64
bf8643c
c882f64
fd6729b
c882f64
bf8643c
c882f64
 
 
 
bf8643c
 
c882f64
 
bf8643c
 
 
 
 
c882f64
 
 
 
 
bf8643c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c882f64
bf8643c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c882f64
 
 
 
bf8643c
 
 
 
 
 
 
 
 
 
 
 
c882f64
 
b68dd3b
bf8643c
 
 
 
 
 
 
c882f64
 
bf8643c
 
 
 
 
 
c882f64
b68dd3b
 
 
c882f64
5c5d824
bf8643c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fd6729b
b68dd3b
bf8643c
 
 
 
 
 
 
 
 
 
fd6729b
 
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
import json
import os
import random

import gradio as gr
from langchain.schema import AIMessage, HumanMessage
from langchain_huggingface import HuggingFaceEndpoint
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, SecretStr


class OAAPIKey(BaseModel):
    openai_api_key: SecretStr


class HFAPIKey(BaseModel):
    huggingface_api_key: SecretStr


def set_openai_api_key(api_key: SecretStr):
    os.environ["OPENAI_API_KEY"] = api_key.get_secret_value()
    llm = ChatOpenAI(temperature=1.0, model="gpt-3.5-turbo-0125")
    return llm


def set_huggingface_api_key(api_key: SecretStr):
    os.environ["HUGGINGFACEHUB_API_TOKEN"] = api_key.get_secret_value()
    your_endpoint_url = (
        "https://a0km823u69omaqm7.us-east-1.aws.endpoints.huggingface.cloud"
    )
    llm = HuggingFaceEndpoint(
        endpoint_url=f"{your_endpoint_url}",
        max_new_tokens=512,
        top_k=10,
        top_p=0.95,
        typical_p=0.95,
        temperature=0.01,
        repetition_penalty=1.03,
        stop_sequences=["<|human|>"],
    )
    return llm


def predict(
    message: str,
    chat_history_openai: list[tuple[str, str]],
    chat_history_huggingface: list[tuple[str, str]],
    openai_api_key: SecretStr,
    huggingface_api_key: SecretStr,
):
    openai_key_model = OAAPIKey(openai_api_key=openai_api_key)
    huggingface_key_model = HFAPIKey(huggingface_api_key=huggingface_api_key)

    openai_llm = set_openai_api_key(api_key=openai_key_model.openai_api_key)
    huggingface_llm = set_huggingface_api_key(
        api_key=huggingface_key_model.huggingface_api_key
    )

    # OpenAI
    history_langchain_format_openai = []
    for human, ai in chat_history_openai:
        history_langchain_format_openai.append(HumanMessage(content=human))
        history_langchain_format_openai.append(AIMessage(content=ai))
    history_langchain_format_openai.append(HumanMessage(content=message))

    openai_response = openai_llm.invoke(input=history_langchain_format_openai)

    # Huggingface Endpoint
    history_langchain_format_huggingface = []
    for human, ai in chat_history_openai:
        history_langchain_format_huggingface.append(f"\n<|human|> {human}\n<|ai|> {ai}")
    history_langchain_format_huggingface.append(f"\n<|human|> {message}\n<|ai|>")

    huggingface_response = huggingface_llm.invoke(
        input=history_langchain_format_huggingface
    )
    huggingface_response = huggingface_response.split("Human:")[0].strip()

    chat_history_openai.append((message, openai_response.content))
    chat_history_huggingface.append((message, huggingface_response))
    return "", chat_history_openai, chat_history_huggingface


with open("askbakingtop.json", "r") as file:
    ask_baking_msgs = json.load(file)


with gr.Blocks() as demo:
    with gr.Row():
        with gr.Column(scale=1):
            openai_api_key = gr.Textbox(
                label="Please enter your OpenAI API key",
                type="password",
                elem_id="lets-chat-openai-api-key",
            )
        with gr.Column(scale=1):
            huggingface_api_key = gr.Textbox(
                label="Please enter your HuggingFace API key",
                type="password",
                elem_id="lets-chat-huggingface-api-key",
            )

    with gr.Row():
        options = [ask["history"] for ask in random.sample(ask_baking_msgs, k=3)]
        msg = gr.Dropdown(
            options,
            label="Please enter your message",
            interactive=True,
            multiselect=False,
            allow_custom_value=True
        )

    with gr.Row():
        with gr.Column(scale=1):
            chatbot_openai = gr.Chatbot(label="OpenAI Chatbot 🏢")
        with gr.Column(scale=1):
            chatbot_huggingface = gr.Chatbot(
                label="Your own fine-tuned preference optimized Chatbot 💪"
            )

    with gr.Row():
        submit_button = gr.Button("Submit")

    with gr.Row():
        clear = gr.ClearButton([msg])

    def respond(
        message: str,
        chat_history_openai: list[tuple[str, str]],
        chat_history_huggingface: list[tuple[str, str]],
        openai_api_key: SecretStr,
        huggingface_api_key: SecretStr,
    ):
        return predict(
            message=message,
            chat_history_openai=chat_history_openai,
            chat_history_huggingface=chat_history_huggingface,
            openai_api_key=openai_api_key,
            huggingface_api_key=huggingface_api_key,
        )

    submit_button.click(
        fn=respond,
        inputs=[
            msg,
            chatbot_openai,
            chatbot_huggingface,
            openai_api_key,
            huggingface_api_key,
        ],
        outputs=[msg, chatbot_openai, chatbot_huggingface],
    )

demo.launch()