File size: 1,526 Bytes
feb9574
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import gradio as gr

# Load the tokenizer and model
model_path = 'nvidia/Minitron-4B-Base'
tokenizer = AutoTokenizer.from_pretrained(model_path)
model = AutoModelForCausalLM.from_pretrained(model_path, device_map='cpu', torch_dtype=torch.float32)

def generate_text(prompt, max_length=100):
    # Encode the input text
    inputs = tokenizer.encode(prompt, return_tensors='pt')
    
    # Generate the output
    outputs = model.generate(
        inputs,
        max_length=max_length,
        num_return_sequences=1,
        no_repeat_ngram_size=2,
        do_sample=True,
        temperature=0.7,
        pad_token_id=tokenizer.eos_token_id
    )
    
    # Decode and return the output
    generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    return generated_text

# Create Gradio interface
demo = gr.Interface(
    fn=generate_text,
    inputs=[
        gr.Textbox(label="Enter your prompt", placeholder="Type your prompt here..."),
        gr.Slider(minimum=20, maximum=200, value=100, step=10, label="Max Length")
    ],
    outputs=gr.Textbox(label="Generated Text"),
    title="Text Generation with Minitron-4B",
    description="Enter a prompt and get AI-generated text completion.",
    examples=[
        ["Complete the paragraph: our solar system is"],
        ["Write a short story about"],
        ["Explain the concept of"]
    ]
)

# Launch the application
if __name__ == "__main__":
    demo.launch(share=False)