Spaces:
Sleeping
Sleeping
File size: 4,933 Bytes
71516c7 268d349 71516c7 268d349 71516c7 268d349 71516c7 268d349 71516c7 268d349 71516c7 268d349 71516c7 268d349 71516c7 268d349 71516c7 268d349 71516c7 3ecdd58 71516c7 268d349 |
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 |
import gradio as gr
from huggingface_hub import InferenceClient
import time
# Initialize the client
client = InferenceClient("HuggingFaceH4/starchat2-15b-v0.1")
def respond(
message,
chat_history,
system_message,
max_tokens,
temperature,
top_p,
model_name
):
"""
Generate chat responses using the specified model.
"""
# Update client if model changes
global client
client = InferenceClient(model_name)
messages = [{"role": "system", "content": system_message}]
# Build conversation history
for human_msg, assistant_msg in chat_history:
if human_msg:
messages.append({"role": "user", "content": human_msg})
if assistant_msg:
messages.append({"role": "assistant", "content": assistant_msg})
messages.append({"role": "user", "content": message})
response = ""
try:
for message in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message.choices[0].delta.content
response += token
chat_history = chat_history + [(message, response)]
yield chat_history
except Exception as e:
chat_history = chat_history + [(message, f"Error: {str(e)}")]
yield chat_history
def create_chat_interface():
"""
Create and configure the Gradio interface
"""
# Default system message
default_system = """You are a helpful AI assistant. You provide accurate, informative, and engaging responses while being direct and concise."""
# Available models
models = [
"HuggingFaceH4/starchat2-15b-v0.1",
"meta-llama/Llama-2-70b-chat-hf",
"mistralai/Mixtral-8x7B-Instruct-v0.1"
]
# Create the interface
with gr.Blocks(theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🤖 Advanced AI Chatbot")
with gr.Row():
with gr.Column(scale=3):
chatbot = gr.Chatbot(
height=600,
show_label=False,
container=True,
scale=2,
type="messages" # Set type to messages format
)
msg = gr.Textbox(
show_label=False,
placeholder="Type your message here...",
container=False
)
with gr.Column(scale=1):
with gr.Accordion("Settings", open=False):
system_msg = gr.Textbox(
label="System Message",
value=default_system,
lines=3
)
model = gr.Dropdown(
choices=models,
value=models[0],
label="Model"
)
max_tokens = gr.Slider(
minimum=50,
maximum=4096,
value=1024,
step=1,
label="Max Tokens"
)
temperature = gr.Slider(
minimum=0.1,
maximum=2.0,
value=0.7,
step=0.1,
label="Temperature"
)
top_p = gr.Slider(
minimum=0.1,
maximum=1.0,
value=0.9,
step=0.1,
label="Top P"
)
with gr.Row():
clear = gr.Button("Clear Chat")
stop = gr.Button("Stop")
# Initialize chat history
state = gr.State([])
# Handle sending messages
msg.submit(
respond,
[msg, state, system_msg, max_tokens, temperature, top_p, model],
[state],
api_name="chat"
).then(
lambda x: "",
[msg],
[msg]
)
# Clear chat history
clear.click(lambda: [], None, state, queue=False)
# Example prompts
gr.Examples(
examples=[
["Tell me a short story about a robot learning to paint."],
["Explain quantum computing in simple terms."],
["Write a haiku about artificial intelligence."]
],
inputs=msg
)
print(demo)
return demo
# Create and launch the interface
if __name__ == "__main__":
demo = create_chat_interface()
demo.queue()
# Disable SSR and sharing for Spaces
demo.launch(
share=False, # Disable sharing on Spaces
)
|