|
import gradio as gr |
|
from huggingface_hub import InferenceClient |
|
|
|
|
|
client = InferenceClient("molbal/CRA-v1-7B") |
|
|
|
def respond(message, history: list[tuple[str, str]], system_message, max_tokens, temperature, top_p): |
|
|
|
messages = [{"role": "system", "content": system_message}] |
|
for user_msg, assistant_msg in history: |
|
if user_msg: |
|
messages.append({"role": "user", "content": user_msg}) |
|
if assistant_msg: |
|
messages.append({"role": "assistant", "content": assistant_msg}) |
|
messages.append({"role": "user", "content": message}) |
|
|
|
response = "" |
|
|
|
for chunk in client.chat_completion( |
|
messages, |
|
max_tokens=max_tokens, |
|
stream=True, |
|
temperature=temperature, |
|
top_p=top_p, |
|
num_ctx=16384, |
|
repeat_penalty=1.05, |
|
): |
|
token = chunk.choices[0].delta.content |
|
response += token |
|
yield response |
|
|
|
|
|
cpu_alert = gr.Markdown("**Note:** This model runs on CPU, so inference may be slow.") |
|
|
|
|
|
with gr.Blocks() as demo: |
|
cpu_alert.render() |
|
chat_interface = gr.ChatInterface( |
|
respond, |
|
additional_inputs=[ |
|
gr.Textbox( |
|
value="### System: You are a writer’s assistant. ### Task: Understand how the story flows, what motivations the characters have and how they will interact with each other and the world as a step by step thought process before continuing the story. ### Context: {context}", |
|
label="System message" |
|
), |
|
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), |
|
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), |
|
gr.Slider(minimum=0.1, maximum=1.0, value=0.8, step=0.05, label="Top-p (nucleus sampling)") |
|
] |
|
) |
|
chat_interface.render() |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|