Happy / app.py
TpsNandhini's picture
Update app.py
c91f78f verified
raw
history blame contribute delete
938 Bytes
import streamlit as st
from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch
def load_model():
model_name_or_path = "./fine_tuned_model" # Ensure this is the correct path
tokenizer = GPT2Tokenizer.from_pretrained(model_name_or_path)
model = GPT2LMHeadModel.from_pretrained(model_name_or_path)
model.to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
return model, tokenizer
model, tokenizer = load_model()
st.title("GPT-2 Text Generation")
input_text = st.text_input("Enter some text to start generating")
if st.button("Generate"):
input_ids = tokenizer.encode(input_text, return_tensors="pt").to(torch.device("cuda" if torch.cuda.is_available() else "cpu"))
output = model.generate(input_ids, max_length=100, num_return_sequences=1, do_sample=True, top_k=50, top_p=0.95)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
st.write(generated_text)