File size: 2,282 Bytes
1f04715 afda60f 1f04715 afda60f f552f84 df99fca f552f84 1f04715 f552f84 1f04715 d3d903a | 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 | import gradio as gr
from huggingface_hub import InferenceClient
def chat_with_bot(user_input, history, hf_token):
client = InferenceClient(repo_id="facebook/blenderbot-400M-distill", token=hf_token)
response = client(inputs=user_input, parameters={"max_new_tokens": 100, "do_sample": True})
bot_reply = response['generated_text'].strip()
history.append((user_input, bot_reply))
return bot_reply, history
with gr.Blocks() as demo:
gr.Markdown("## Chat with BlenderBot (supports complex topics)")
hf_token_input = gr.Textbox(label="Hugging Face API Token", type="password")
chat_box = gr.Chatbot()
user_input = gr.Textbox(label="Your message")
send_button = gr.Button("Send")
chat_history = []
def respond(user_message, chat_history, token):
reply, new_history = chat_with_bot(user_message, chat_history, token)
chat_history.append((user_message, reply))
return chat_history, new_history
send_button.click(
respond,
inputs=[user_input, chat_box, hf_token_input],
outputs=[chat_box, "state"]
)
demo.launch(share=True)
import gradio as gr
from huggingface_hub import InferenceApi
def chat_with_bot(
user_input,
history,
hf_token
):
client = InferenceApi(repo_id="facebook/blenderbot-400M-distill", token=hf_token)
response = client(inputs=user_input, parameters={"max_new_tokens": 100, "do_sample": True})
bot_reply = response['generated_text'].strip()
# Update chat history
history.append((user_input, bot_reply))
return bot_reply, history
with gr.Blocks() as demo:
gr.Markdown("## Chat with BlenderBot (supports complex topics)")
hf_token_input = gr.Textbox(label="Hugging Face API Token", type="password")
chat_box = gr.Chatbot()
user_input = gr.Textbox(label="Your message")
send_button = gr.Button("Send")
chat_history = []
def respond(user_message, chat_history, token):
reply, new_history = chat_with_bot(user_message, chat_history, token)
chat_history.append((user_message, reply))
return chat_history, new_history
send_button.click(
respond,
inputs=[user_input, chat_box, hf_token_input],
outputs=[chat_box, "state"]
)
demo.launch(share=True) |