Spaces:
Running
Running
File size: 2,010 Bytes
b70d32d eb450e3 b70d32d eb450e3 91e7ac0 eb450e3 91e7ac0 eb450e3 531f276 eb450e3 1ba65b2 eb450e3 91e7ac0 eb450e3 91e7ac0 eb450e3 91e7ac0 |
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 |
import os
import gradio as gr
from huggingface_hub import InferenceClient
client = InferenceClient("lambdaindie/lambdai", token=os.environ["HF_TOKEN"])
def respond(message, history, system_message, max_tokens, temperature, top_p):
messages = [{"role": "system", "content": system_message}] if system_message else []
for user, assistant in history:
if user:
messages.append({"role": "user", "content": user})
if assistant:
messages.append({"role": "assistant", "content": assistant})
messages.append({"role": "user", "content": message})
response = ""
for chunk in client.text_generation(
messages,
max_new_tokens=max_tokens,
stream=True,
temperature=temperature,
top_p=top_p,
):
token = chunk.choices[0].delta.content
response += token
yield response
with gr.Blocks() as demo:
gr.Markdown("# 🧠 lambdai — Chat Demo")
chatbot = gr.Chatbot()
with gr.Row():
system_msg = gr.Textbox(label="System message", placeholder="e.g. You are a helpful assistant.")
with gr.Row():
max_tokens = gr.Slider(1, 2048, value=512, step=1, label="Max 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")
msg = gr.Textbox(placeholder="Ask something...", label="Your message")
state = gr.State([])
def user_submit(user_message, history):
return "", history + [[user_message, None]]
def generate_response(message, history, sys_msg, max_tokens, temperature, top_p):
gen = respond(message, history, sys_msg, max_tokens, temperature, top_p)
return gen, history
msg.submit(user_submit, [msg, state], [msg, state], queue=False).then(
generate_response,
[msg, state, system_msg, max_tokens, temperature, top_p],
[chatbot, state]
)
if __name__ == "__main__":
demo.launch() |