File size: 6,180 Bytes
657585b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b741a
 
 
 
 
 
 
 
 
 
657585b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b741a
657585b
 
 
 
 
93b741a
657585b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93b741a
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
from typing import Callable, Literal
import gradio as gr
from uuid import uuid4


chinese_examples = [
    ["你好!"],
    ["你是谁?"],
    ["请介绍一下上海"],
    ["请介绍一下英特尔公司"],
    ["晚上睡不着怎么办?"],
    ["给我讲一个年轻人奋斗创业最终取得成功的故事。"],
    ["给这个故事起一个标题。"],
]

english_examples = [
    "What are the key findings of the latest climate summit, and how do they impact global policies?",
    "Who are some of the most recent Nobel Prize winners, and what breakthroughs did they contribute to their fields?",
    "What are the recent developments in the electric vehicle industry, and how are governments incentivizing their adoption?",
    "Which tech companies recently announced breakthroughs in AI, and how could these innovations shape future industries?",
    "What are the most recent discoveries in the search for habitable exoplanets, and how close are we to confirming one?",
    "How are inflation and recent economic policies affecting global financial markets?",
    "What are the latest advancements in cancer research, and how close are we to new treatments?",
    "What steps are major companies taking to address recent cybersecurity threats and breaches?",
    "How are recent regulatory changes in cryptocurrency markets affecting investors and innovation?",
    "What are the latest scientific findings related to deep-sea exploration, and what have researchers uncovered about ocean ecosystems?"
]

japanese_examples = [
    ["こんにちは!調子はどうですか?"],
    ["OpenVINOとは何ですか?"],
    ["あなたは誰ですか?"],
    ["Pythonプログラミング言語とは何か簡単に説明してもらえますか?"],
    ["シンデレラのあらすじを一文で説明してください。"],
    ["コードを書くときに避けるべきよくある間違いは何ですか?"],
    ["人工知能と「OpenVINOの利点」について100語程度のブログ記事を書いてください。"],
]


def get_uuid():
    """
    Universal unique identifier for thread
    """
    return str(uuid4())

def handle_user_message(message, history):
    """
    Callback function for updating user messages in interface on submit button click
    """
    # Append the user's message to the conversation history
    return "", history + [[message, ""]]

def make_demo(
    run_fn: Callable,
    title: str = "OpenVINO Chatbot",
    language: Literal["English", "Chinese", "Japanese"] = "English"
):
    # Define examples based on the selected language
    examples = (
        chinese_examples if language == "Chinese" 
        else japanese_examples if language == "Japanese" 
        else english_examples
    )

    with gr.Blocks(
        theme=gr.themes.Soft(),
        css=".disclaimer {font-variant-caps: all-small-caps;}"
    ) as demo:
        conversation_id = gr.State(get_uuid)  # Ensure get_uuid is defined elsewhere
        gr.Markdown(f"<h1><center>{title}</center></h1>")
        chatbot = gr.Chatbot(height=500)

        # User message input
        with gr.Row():
            with gr.Column():
                msg = gr.Textbox(
                    label="Chat Message Box",
                    placeholder="Chat Message Box",
                    show_label=False,
                    container=False,
                )
            with gr.Column():
                submit = gr.Button("Submit")
                clear = gr.Button("Clear")

        # Advanced options for the chat
        with gr.Row():
            with gr.Accordion("Advanced Options:", open=False):
                temperature = gr.Slider(
                    label="Temperature",
                    value=0.1,
                    minimum=0.0,
                    maximum=1.0,
                    step=0.1,
                    interactive=True,
                    info="Higher values produce more diverse outputs",
                )
                top_p = gr.Slider(
                    label="Top-p (nucleus sampling)",
                    value=1.0,
                    minimum=0.0,
                    maximum=1.0,
                    step=0.01,
                    interactive=True,
                    info=("Sample from the smallest possible set of tokens whose cumulative probability exceeds top_p. "
                          "Set to 1 to disable and sample from all tokens."),
                )
                top_k = gr.Slider(
                    label="Top-k",
                    value=50,
                    minimum=0,
                    maximum=200,
                    step=1,
                    interactive=True,
                    info="Sample from a shortlist of top-k tokens — 0 to disable and sample from all tokens.",
                )
                repetition_penalty = gr.Slider(
                    label="Repetition Penalty",
                    value=1.1,
                    minimum=1.0,
                    maximum=2.0,
                    step=0.1,
                    interactive=True,
                    info="Penalize repetition — 1.0 to disable.",
                )

        # Example messages
        gr.Examples(examples, inputs=msg, label="Click on any example and press the 'Submit' button")

        # Submit message event
        submit_event = msg.submit(
            fn=handle_user_message,
            inputs=[msg, chatbot],
            outputs=[msg, chatbot],
            queue=False,
        ).then(
            fn=run_fn,
            inputs=[chatbot, temperature, top_p, top_k, repetition_penalty, conversation_id],
            outputs=chatbot,
            queue=True,
        )

        # Submit button click event
        submit.click(
            fn=handle_user_message,
            inputs=[msg, chatbot],
            outputs=[msg, chatbot],
            queue=False,
        ).then(
            fn=run_fn,
            inputs=[chatbot, temperature, top_p, top_k, repetition_penalty, conversation_id],
            outputs=chatbot,
            queue=True,
        )

        # Clear chat button functionality
        clear.click(lambda: None, None, chatbot, queue=False)

    return demo