File size: 1,049 Bytes
9ecac9b
5e956af
 
 
234a6c9
 
9ecac9b
5e956af
9ecac9b
 
 
 
 
 
 
 
5e956af
9ecac9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline

# The cache directory is now set in the Dockerfile
# os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"

st.title("💬 DistilGPT-2 Playground")

@st.cache_resource
def load_model_and_tokenizer(model_id):
    tokenizer = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(
        model_id,
        device_map="cpu",
        torch_dtype=torch.float32,
        low_cpu_mem_usage=True
    )
    return tokenizer, model

model_id = "distilgpt2"

with st.spinner(f"Loading model {model_id}... This may take a while."):
    tokenizer, model = load_model_and_tokenizer(model_id)

generator = pipeline("text-generation", model=model, tokenizer=tokenizer)

prompt = st.text_area("Enter your prompt here:")
if prompt:
    with st.spinner("Generating text..."):
        output = generator(prompt, max_length=100, temperature=0.7)
        st.write("### Output:")
        st.success(output[0]['generated_text'])