Spaces:
Sleeping
Sleeping
import os | |
os.system('pip install minijinja') | |
import gradio as gr | |
from huggingface_hub import InferenceClient | |
import torch | |
import spaces | |
# Initialize the client with your model | |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") # Replace with your model's name or endpoint | |
default_system = 'You are a helpful assistant' | |
def respond(message, history, system_message, max_tokens, temperature, top_p): | |
messages = [{"role": "system", "content": system_message}] | |
for user, assistant in history: | |
messages.append({"role": "user", "content": user}) | |
messages.append({"role": "assistant", "content": assistant}) | |
messages.append({"role": "user", "content": message}) | |
response = "" | |
for message in client.chat_completion( | |
messages, | |
max_tokens=max_tokens, | |
stream=True, | |
temperature=temperature, | |
top_p=top_p, | |
): | |
token = message.choices[0].delta.content | |
response += token | |
yield response | |
def clear_session(): | |
return "", [] | |
def modify_system_session(system): | |
if not system: | |
system = default_system | |
return system, system, [] | |
with gr.Blocks() as demo: | |
gr.Markdown("<h1 style='text-align: center;'>LLM.C 1.5B Chat Demo π€</h1>") | |
with gr.Row(): | |
with gr.Column(scale=3): | |
system_input = gr.Textbox(value=default_system, lines=1, label='System Prompt') | |
with gr.Column(scale=3): | |
modify_system = gr.Button("π οΈ Set system prompt and clear history") | |
system_state = gr.Textbox(value=default_system, visible=False) | |
chatbot = gr.Chatbot(label='LLM.C Chat') | |
message = gr.Textbox(lines=1, label='Your message') | |
with gr.Row(): | |
clear_history = gr.Button("π§Ή Clear history") | |
submit = gr.Button("π Send") | |
with gr.Accordion("Advanced Settings", open=False): | |
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max New Tokens") | |
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature") | |
top_p = gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (Nucleus Sampling)") | |
# Set up event handlers | |
message.submit(respond, inputs=[message, chatbot, system_state, max_tokens, temperature, top_p], outputs=[chatbot]) | |
submit.click(respond, inputs=[message, chatbot, system_state, max_tokens, temperature, top_p], outputs=[chatbot]) | |
clear_history.click(fn=clear_session, inputs=[], outputs=[message, chatbot]) | |
modify_system.click(fn=modify_system_session, inputs=[system_input], outputs=[system_state, system_input, chatbot]) | |
gr.Markdown( | |
""" | |
## About LLM.C | |
Some stuff about llm.c + link to the github repo | |
""" | |
) | |
if __name__ == "__main__": | |
demo.launch() |