Kosmox / app.py
wop's picture
Update app.py
e44990b verified
raw
history blame
1.97 kB
import gradio as gr
from transformers import AutoModel, AutoTokenizer
import torch
# Load the model and tokenizer
model_name = "wop/kosmox-gguf"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModel.from_pretrained(model_name)
# Function to generate responses
def respond(message, history, system_message, max_tokens, temperature, top_p):
# Prepare the chat history
messages = [{"role": "system", "content": system_message}]
for user_msg, bot_msg in history:
if user_msg:
messages.append({"role": "user", "content": user_msg})
if bot_msg:
messages.append({"role": "assistant", "content": bot_msg})
messages.append({"role": "user", "content": message})
# Create the chat input for the model
chat_input = tokenizer.chat_template.format(
bos_token=tokenizer.bos_token,
messages=[{"from": "human", "value": m['content']} if m['role'] == 'user' else {"from": "gpt", "value": m['content']} for m in messages]
)
inputs = tokenizer(chat_input, return_tensors="pt")
# Generate response
with torch.no_grad():
outputs = model.generate(
input_ids=inputs['input_ids'],
max_length=max_tokens,
temperature=temperature,
top_p=top_p,
do_sample=True
)
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
yield response.strip()
# Define the Gradio interface
demo = gr.ChatInterface(
respond,
additional_inputs=[
gr.Textbox(value="You are a friendly Chatbot.", 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.95, step=0.05, label="Top-p (nucleus sampling)"),
],
)
# Launch the demo
if __name__ == "__main__":
demo.launch()