Spaces:
Running
Running
File size: 5,837 Bytes
657585b |
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 |
from typing import Callable, Literal
import gradio as gr
from uuid import uuid4
chinese_examples = [
["你好!"],
["你是谁?"],
["请介绍一下上海"],
["请介绍一下英特尔公司"],
["晚上睡不着怎么办?"],
["给我讲一个年轻人奋斗创业最终取得成功的故事。"],
["给这个故事起一个标题。"],
]
english_examples = [
["Hello there! How are you doing?"],
["What is OpenVINO?"],
["Who are you?"],
["Can you explain to me briefly what is Python programming language?"],
["Explain the plot of Cinderella in a sentence."],
["What are some common mistakes to avoid when writing code?"],
["Write a 100-word blog post on “Benefits of Artificial Intelligence and OpenVINO“"],
]
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
Params:
message: current message
history: conversation history
Returns:
None
"""
# Append the user's message to the conversation history
return "", history + [[message, ""]]
def make_demo(
run_fn: Callable,
stop_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")
stop = gr.Button("Stop")
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,
)
# Stop button functionality
stop.click(
fn=stop_fn,
inputs=None,
outputs=None,
cancels=[submit_event], # Cancels the submission event
queue=False,
)
# Clear chat button functionality
clear.click(lambda: None, None, chatbot, queue=False)
return demo
|