chatbot / app.py
Fragenfragen's picture
Update app.py
3c76e6a verified
import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
def respond(message, chat_history, system_message, max_tokens, temperature, top_p):
messages = [{"role": "system", "content": system_message}]
for user_msg, bot_msg in chat_history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
response = ""
for message_obj in client.chat_completion(
messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = message_obj.choices[0].delta.content or ""
response += token
yield chat_history + [(message, response)]
def initialize():
# Initialer Verlauf mit Begrüßung
return [("", "Hallo! Ich bin ein hilfreicher Assistent. Ich beantworte alle Fragen auf Deutsch.")]
with gr.Blocks() as demo:
chatbot = gr.Chatbot(value=initialize(), height=400)
with gr.Row():
msg = gr.Textbox(label="Nachricht", placeholder="Stelle mir eine Frage...")
send_btn = gr.Button("Senden")
system_message = gr.Textbox(
value="Du bist ein hilfreicher Assistent. Antworte immer auf Deutsch.",
label="System message"
)
max_tokens = gr.Slider(1, 2048, value=512, step=1, label="Max new tokens")
temperature = gr.Slider(0.1, 4.0, value=0.7, step=0.1, label="Temperature")
top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
send_btn.click(
respond,
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p],
outputs=chatbot
)
msg.submit(
respond,
inputs=[msg, chatbot, system_message, max_tokens, temperature, top_p],
outputs=chatbot
)
demo.launch()