File size: 2,202 Bytes
7d02efe ccffefd 7d02efe ccffefd | 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 | import gradio as gr
from huggingface_hub import InferenceClient
# Load the model from Hugging Face Hub
client = InferenceClient(model="tiiuae/falcon-7b-instruct")
# Chat completion function
def respond(message, history, system_message, max_tokens, temperature, top_p):
messages = [{"role": "system", "content": system_message}]
messages += history
messages.append({"role": "user", "content": message})
response = ""
try:
for chunk in client.chat_completion(
messages=messages,
max_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
if hasattr(chunk.choices[0].delta, "content"):
token = chunk.choices[0].delta.content
response += token
yield response
except Exception as e:
yield f"[Error] {e}"
# Gradio interface layout
with gr.Blocks() as demo:
gr.Markdown("### 🧠 Falcon-7B-Instruct Chat UI — Powered by Hugging Face")
with gr.Row():
system_message = gr.Textbox(value="You are a helpful assistant.", label="System Prompt", lines=2)
with gr.Row():
message = gr.Textbox(placeholder="Ask something…", label="Your Message", lines=2)
with gr.Row():
max_tokens = gr.Slider(minimum=64, maximum=1024, value=256, step=64, label="Max Tokens")
temperature = gr.Slider(minimum=0.1, maximum=1.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 (nucleus sampling)")
chatbot = gr.Chatbot()
state = gr.State([])
submit = gr.Button("Send")
def handle_submit(user_message, history, system_message, max_tokens, temperature, top_p):
history = history + [[user_message, ""]]
for updated_response in respond(user_message, history[:-1], system_message, max_tokens, temperature, top_p):
history[-1][1] = updated_response
yield history, history
submit.click(
handle_submit,
inputs=[message, state, system_message, max_tokens, temperature, top_p],
outputs=[chatbot, state],
)
demo.launch() |