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ialize the client
|
| 6 |
+
client = InferenceClient("HuggingFaceH4/starchat2-15b-v0.1")
|
| 7 |
+
|
| 8 |
+
def respond(
|
| 9 |
+
message,
|
| 10 |
+
history: list[tuple[str, str]],
|
| 11 |
+
system_message,
|
| 12 |
+
max_tokens,
|
| 13 |
+
temperature,
|
| 14 |
+
top_p,
|
| 15 |
+
model_name
|
| 16 |
+
):
|
| 17 |
+
"""
|
| 18 |
+
Generate chat responses using the specified model.
|
| 19 |
+
"""
|
| 20 |
+
# Update client if model changes
|
| 21 |
+
global client
|
| 22 |
+
client = InferenceClient(model_name)
|
| 23 |
+
|
| 24 |
+
messages = [{"role": "system", "content": system_message}]
|
| 25 |
+
|
| 26 |
+
# Build conversation history
|
| 27 |
+
for user_msg, assistant_msg in history:
|
| 28 |
+
if user_msg:
|
| 29 |
+
messages.append({"role": "user", "content": user_msg})
|
| 30 |
+
if assistant_msg:
|
| 31 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 32 |
+
|
| 33 |
+
messages.append({"role": "user", "content": message})
|
| 34 |
+
response = ""
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
for message in client.chat_completion(
|
| 38 |
+
messages,
|
| 39 |
+
max_tokens=max_tokens,
|
| 40 |
+
stream=True,
|
| 41 |
+
temperature=temperature,
|
| 42 |
+
top_p=top_p,
|
| 43 |
+
):
|
| 44 |
+
token = message.choices[0].delta.content
|
| 45 |
+
response += token
|
| 46 |
+
yield response
|
| 47 |
+
except Exception as e:
|
| 48 |
+
yield f"Error: {str(e)}"
|
| 49 |
+
|
| 50 |
+
def create_chat_interface():
|
| 51 |
+
"""
|
| 52 |
+
Create and configure the Gradio interface
|
| 53 |
+
"""
|
| 54 |
+
# Default system message
|
| 55 |
+
default_system = """You are a helpful AI assistant. You provide accurate, informative, and engaging responses while being direct and concise."""
|
| 56 |
+
|
| 57 |
+
# Available models
|
| 58 |
+
models = [
|
| 59 |
+
"HuggingFaceH4/starchat2-15b-v0.1",
|
| 60 |
+
"meta-llama/Llama-2-70b-chat-hf",
|
| 61 |
+
"mistralai/Mixtral-8x7B-Instruct-v0.1"
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
# Create the interface
|
| 65 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 66 |
+
gr.Markdown("# 🤖 Advanced AI Chatbot")
|
| 67 |
+
|
| 68 |
+
with gr.Row():
|
| 69 |
+
with gr.Column(scale=3):
|
| 70 |
+
chatbot = gr.Chatbot(
|
| 71 |
+
height=600,
|
| 72 |
+
show_label=False,
|
| 73 |
+
container=True,
|
| 74 |
+
scale=2
|
| 75 |
+
)
|
| 76 |
+
msg = gr.Textbox(
|
| 77 |
+
show_label=False,
|
| 78 |
+
placeholder="Type your message here...",
|
| 79 |
+
container=False
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
with gr.Column(scale=1):
|
| 83 |
+
with gr.Accordion("Settings", open=False):
|
| 84 |
+
system_msg = gr.Textbox(
|
| 85 |
+
label="System Message",
|
| 86 |
+
value=default_system,
|
| 87 |
+
lines=3
|
| 88 |
+
)
|
| 89 |
+
model = gr.Dropdown(
|
| 90 |
+
choices=models,
|
| 91 |
+
value=models[0],
|
| 92 |
+
label="Model"
|
| 93 |
+
)
|
| 94 |
+
max_tokens = gr.Slider(
|
| 95 |
+
minimum=50,
|
| 96 |
+
maximum=4096,
|
| 97 |
+
value=1024,
|
| 98 |
+
step=1,
|
| 99 |
+
label="Max Tokens"
|
| 100 |
+
)
|
| 101 |
+
temperature = gr.Slider(
|
| 102 |
+
minimum=0.1,
|
| 103 |
+
maximum=2.0,
|
| 104 |
+
value=0.7,
|
| 105 |
+
step=0.1,
|
| 106 |
+
label="Temperature"
|
| 107 |
+
)
|
| 108 |
+
top_p = gr.Slider(
|
| 109 |
+
minimum=0.1,
|
| 110 |
+
maximum=1.0,
|
| 111 |
+
value=0.9,
|
| 112 |
+
step=0.1,
|
| 113 |
+
label="Top P"
|
| 114 |
+
)
|
| 115 |
+
|
| 116 |
+
with gr.Row():
|
| 117 |
+
clear = gr.Button("Clear Chat")
|
| 118 |
+
stop = gr.Button("Stop")
|
| 119 |
+
|
| 120 |
+
# Handle sending messages
|
| 121 |
+
msg.submit(
|
| 122 |
+
respond,
|
| 123 |
+
[msg, chatbot, system_msg, max_tokens, temperature, top_p, model],
|
| 124 |
+
[chatbot],
|
| 125 |
+
api_name="chat"
|
| 126 |
+
)
|
| 127 |
+
|
| 128 |
+
# Clear chat history
|
| 129 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
| 130 |
+
|
| 131 |
+
# Example prompts
|
| 132 |
+
gr.Examples(
|
| 133 |
+
examples=[
|
| 134 |
+
["Tell me a short story about a robot learning to paint."],
|
| 135 |
+
["Explain quantum computing in simple terms."],
|
| 136 |
+
["Write a haiku about artificial intelligence."]
|
| 137 |
+
],
|
| 138 |
+
inputs=msg
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
return demo
|
| 142 |
+
|
| 143 |
+
# Create and launch the interface
|
| 144 |
+
if __name__ == "__main__":
|
| 145 |
+
demo = create_chat_interface()
|
| 146 |
+
demo.queue()
|
| 147 |
+
demo.launch(share=True)
|