HugChatWrap / app.py
K00B404's picture
Update app.py
3ecdd58 verified
raw
history blame
4.93 kB
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
)