| import gradio as gr |
| from huggingface_hub import InferenceClient |
|
|
| |
| client = InferenceClient(model="tiiuae/falcon-7b-instruct") |
|
|
| |
| 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}" |
|
|
| |
| 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() |