Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,31 @@ import gradio as gr
|
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
| 6 |
-
title = """# Minitron-8B-Base"""
|
| 7 |
description = """
|
|
|
|
|
|
|
| 8 |
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.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"""
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
# Load the tokenizer and model
|
| 12 |
model_path = "nvidia/Minitron-8B-Base"
|
| 13 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
@@ -31,19 +51,39 @@ def respond(message, history, system_message, max_tokens, temperature, top_p):
|
|
| 31 |
|
| 32 |
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 33 |
|
| 34 |
-
return output_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
demo = gr.ChatInterface(
|
| 37 |
-
title=gr.Markdown(title),
|
| 38 |
-
|
| 39 |
-
fn=
|
| 40 |
-
additional_inputs=[
|
| 41 |
-
gr.
|
| 42 |
-
gr.Slider(minimum=1, maximum=
|
| 43 |
-
gr.Slider(minimum=0.1, maximum=
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
demo.launch()
|
|
|
|
| 3 |
import torch
|
| 4 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
|
| 6 |
+
title = """# Minitron-8B-Base Story Generator"""
|
| 7 |
description = """
|
| 8 |
+
# Minitron
|
| 9 |
+
|
| 10 |
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.
|
| 11 |
+
|
| 12 |
+
# Short Story Generator
|
| 13 |
+
Welcome to the Short Story Generator! This application helps you create unique short stories based on your inputs.
|
| 14 |
+
|
| 15 |
+
**Instructions:**
|
| 16 |
+
1. **Main Character:** Describe the main character of your story. For example, "a brave knight" or "a curious cat".
|
| 17 |
+
2. **Setting:** Describe the setting where your story takes place. For example, "in an enchanted forest" or "in a bustling city".
|
| 18 |
+
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".
|
| 19 |
+
|
| 20 |
+
After filling in these details, click the "Submit" button, and a short story will be generated for you.
|
| 21 |
"""
|
| 22 |
|
| 23 |
+
inputs = [
|
| 24 |
+
gr.inputs.Textbox(label="Main Character", placeholder="e.g. a brave knight"),
|
| 25 |
+
gr.inputs.Textbox(label="Setting", placeholder="e.g. in an enchanted forest"),
|
| 26 |
+
gr.inputs.Textbox(label="Plot Twist", placeholder="e.g. discovers a hidden treasure")
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
outputs = gr.outputs.Textbox(label="Generated Story")
|
| 30 |
+
|
| 31 |
# Load the tokenizer and model
|
| 32 |
model_path = "nvidia/Minitron-8B-Base"
|
| 33 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
|
|
|
| 51 |
|
| 52 |
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 53 |
|
| 54 |
+
return output_text
|
| 55 |
+
|
| 56 |
+
@spaces.GPU
|
| 57 |
+
def generate_story(character, setting, plot_twist):
|
| 58 |
+
"""Define the function to generate the story."""
|
| 59 |
+
prompt = f"Write a short story with the following details:\nMain character: {character}\nSetting: {setting}\nPlot twist: {plot_twist}\n\nStory:"
|
| 60 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
| 61 |
+
|
| 62 |
+
output_ids = model.generate(input_ids, max_length=50, num_return_sequences=1)
|
| 63 |
+
|
| 64 |
+
output_text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 65 |
+
|
| 66 |
+
return output_text
|
| 67 |
|
| 68 |
+
#demo = gr.ChatInterface(
|
| 69 |
+
# title=gr.Markdown(title),
|
| 70 |
+
# description=gr.Markdown(description),
|
| 71 |
+
# fn=generate_story,
|
| 72 |
+
# additional_inputs=[
|
| 73 |
+
# gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 74 |
+
# gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 75 |
+
# gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)")
|
| 76 |
+
# ],
|
| 77 |
+
#)
|
| 78 |
+
|
| 79 |
+
# Create the Gradio interface
|
| 80 |
+
demo = gr.Interface(
|
| 81 |
+
fn=generate_story,
|
| 82 |
+
inputs=inputs,
|
| 83 |
+
outputs=outputs,
|
| 84 |
+
title="Short Story Generator",
|
| 85 |
+
description=description
|
| 86 |
+
)
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|
| 89 |
demo.launch()
|