minitron / app.py
gheinrich's picture
Update app.py
6863e73 verified
raw
history blame
4.01 kB
import spaces
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
title = """# Minitron-8B-Base Story Generator"""
description = """
# Minitron
Minitron is a family of small language models (SLMs) obtained by pruning [NVIDIA's](https://huggingface.co/nvidia) Nemotron-4 15B model. We prune model embedding size, attention heads, and MLP intermediate dimension, following which, we perform continued training with distillation to arrive at the final models.
# Short Story Generator
Welcome to the Short Story Generator! This application helps you create unique short stories based on your inputs.
**Instructions:**
1. **Main Character:** Describe the main character of your story. For example, "a brave knight" or "a curious cat".
2. **Setting:** Describe the setting where your story takes place. For example, "in an enchanted forest" or "in a bustling city".
3. **Plot Twist:** Add an interesting plot twist to make the story exciting. For example, "discovers a hidden treasure" or "finds a secret portal to another world".
After filling in these details, click the "Submit" button, and a short story will be generated for you.
"""
inputs = [
gr.Textbox(label="Main Character", placeholder="e.g. a brave knight"),
gr.Textbox(label="Setting", placeholder="e.g. in an enchanted forest"),
gr.Textbox(label="Plot Twist", placeholder="e.g. discovers a hidden treasure"),
gr.Slider(minimum=1, maximum=2048, value=64, 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)"),
]
outputs = gr.Textbox(label="Generated Story")
# Load the tokenizer and model
model_path = "nvidia/Minitron-8B-Base"
tokenizer = AutoTokenizer.from_pretrained(model_path)
device='cuda'
dtype=torch.bfloat16
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=dtype, device_map=device)
# Define the prompt format
def create_prompt(instruction):
PROMPT = '''Below is an instruction that describes a task.\n\nWrite a response that appropriately completes the request.\n\n### Instruction:\n{instruction}\n\n### Response:'''
return PROMPT.format(instruction=instruction)
@spaces.GPU
def respond(message, history, system_message, max_tokens, temperature, top_p):
prompt = create_prompt(message)
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
output_ids = model.generate(input_ids, max_length=50, num_return_sequences=1)
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
return output_text
@spaces.GPU
def generate_story(character, setting, plot_twist, max_tokens, temperature, top_p):
"""Define the function to generate the story."""
prompt = f"Write a short story with the following details:\nMain character: {character}\nSetting: {setting}\nPlot twist: {plot_twist}\n\nStory:"
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
output_ids = model.generate(input_ids, max_length=max_tokens, num_return_sequences=1, temperature=temperature, top_p=top_p)
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
return output_text
#demo = gr.ChatInterface(
# title=gr.Markdown(title),
# description=gr.Markdown(description),
# fn=generate_story,
# additional_inputs=[
# 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)")
# ],
#)
# Create the Gradio interface
demo = gr.Interface(
fn=generate_story,
inputs=inputs,
outputs=outputs,
title="Short Story Generator",
description=description
)
if __name__ == "__main__":
demo.launch()