File size: 1,425 Bytes
03e2f07 |
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 |
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# Load tokenizer and model from Hugging Face Hub
tokenizer = AutoTokenizer.from_pretrained("bilalRahib/TinyLLama-NSFW-Chatbot")
model = AutoModelForCausalLM.from_pretrained("bilalRahib/TinyLLama-NSFW-Chatbot")
model.eval()
# Text generation function
def generate_response(prompt, max_new_tokens=100, temperature=0.8, top_p=0.95):
inputs = tokenizer(prompt, return_tensors="pt")
with torch.no_grad():
output = model.generate(
inputs["input_ids"],
attention_mask=inputs.get("attention_mask", None),
max_new_tokens=max_new_tokens,
temperature=temperature,
top_p=top_p,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
return tokenizer.decode(output[0], skip_special_tokens=True)
# Gradio interface
iface = gr.Interface(
fn=generate_response,
inputs=[
gr.Textbox(label="Enter your prompt", placeholder="Type a message..."),
gr.Slider(20, 300, value=100, label="Max New Tokens"),
gr.Slider(0.1, 1.5, value=0.8, label="Temperature"),
gr.Slider(0.5, 1.0, value=0.95, label="Top-p"),
],
outputs="text",
title="TinyLLama NSFW Chatbot",
description="A chatbot using TinyLLama NSFW fine-tuned model.",
)
# Launch app
if __name__ == "__main__":
iface.launch()
|