import streamlit as st import torch from transformers import GPT2LMHeadModel, GPT2Tokenizer from huggingface_hub import hf_hub_download import re # Configure the page st.set_page_config( page_title="Ghost Writer: Winds of Winter", page_icon="📚", layout="wide" ) # Custom CSS for styling st.markdown(""" """, unsafe_allow_html=True) # App header st.markdown('

Ghost Writer: Winds of Winter

', unsafe_allow_html=True) st.markdown('

Continue the saga in the style of George R.R. Martin

', unsafe_allow_html=True) @st.cache_resource def load_model(): """Load the model and tokenizer from Hugging Face""" try: model_name = "lucy143/gpt-george-1" tokenizer = GPT2Tokenizer.from_pretrained(model_name) model = GPT2LMHeadModel.from_pretrained(model_name) # Add padding token if it doesn't exist if tokenizer.pad_token is None: tokenizer.pad_token = tokenizer.eos_token return model, tokenizer except Exception as e: st.error(f"Error loading model: {e}") return None, None def generate_text(prompt, model, tokenizer, max_length=500, temperature=0.8, top_p=0.9): """Generate text continuation""" try: # Encode the prompt inputs = tokenizer.encode(prompt, return_tensors='pt', truncation=True, max_length=1024) # Generate text with torch.no_grad(): outputs = model.generate( inputs, max_length=max_length, temperature=temperature, top_p=top_p, do_sample=True, pad_token_id=tokenizer.eos_token_id, repetition_penalty=1.1, early_stopping=True ) # Decode and return generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True) # Clean up the text (remove repeated prompt if present) if generated_text.startswith(prompt): generated_text = generated_text[len(prompt):].strip() return generated_text except Exception as e: return f"Error generating text: {e}" # Load model with st.spinner("Loading the Maester's knowledge..."): model, tokenizer = load_model() if model is None: st.error("Failed to load the model. Please check if the model repository exists and is accessible.") else: st.success("Model loaded successfully!") # Sidebar for controls st.sidebar.title("Generation Settings") max_length = st.sidebar.slider("Max Length", 100, 1000, 500, 50) temperature = st.sidebar.slider("Temperature", 0.1, 1.5, 0.8, 0.1) top_p = st.sidebar.slider("Top-p", 0.1, 1.0, 0.9, 0.1) # Example prompts st.sidebar.markdown("### Example Prompts") example_prompts = [ "The night was dark and full of terrors,", "Winter is coming, the Stark words echoed", "In the throne room, the queen sat", "Beyond the Wall, the wind whispered", "Tyrion poured the wine and said," ] for prompt in example_prompts: if st.sidebar.button(prompt[:50] + "..." if len(prompt) > 50 else prompt): st.session_state.prompt = prompt # Main content area col1, col2 = st.columns([1, 1]) with col1: st.subheader("Start Your Chapter") prompt = st.text_area( "Enter the beginning of your text:", value=st.session_state.get('prompt', 'The night was dark and full of terrors,'), height=150, placeholder="Begin your tale in the style of George R.R. Martin..." ) if st.button("Generate Continuation", type="primary"): if prompt.strip(): with st.spinner("The maester is writing..."): generated = generate_text( prompt, model, tokenizer, max_length=max_length, temperature=temperature, top_p=top_p ) st.session_state.generated_text = generated st.session_state.original_prompt = prompt else: st.warning("Please enter some text to continue.") with col2: st.subheader("Generated Text") if 'generated_text' in st.session_state: st.markdown('
', unsafe_allow_html=True) st.write(st.session_state.original_prompt + " " + st.session_state.generated_text) st.markdown('
', unsafe_allow_html=True) # Download button full_text = st.session_state.original_prompt + " " + st.session_state.generated_text st.download_button( label="Download Text", data=full_text, file_name="winds_of_winter_continuation.txt", mime="text/plain" ) else: st.info("Your generated text will appear here...") # Footer st.markdown("---") st.markdown("*This is a fan creation using AI. George R.R. Martin's 'A Song of Ice and Fire' is owned by George R.R. Martin.*")