import gradio as gr import random from transformers import pipeline # Load the model once when the app starts generator = pipeline('text-generation', model='distilgpt2') # Lightweight model # Predefined words to check SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat'] # Function to generate the initial design (black color) def generate_initial_design(word): """Generate initial design for the special word in black color.""" # Controlled randomization parameters fonts = [ "'VT323', monospace", "'Josefin Sans', sans-serif", "'Rajdhani', sans-serif", "'Anton', sans-serif", "'Caveat', cursive", "'Patrick Hand', cursive", "'Nothing You Could Do', cursive", "'Reenie Beanie', cursive", "'Orbitron', sans-serif", "'Raleway', sans-serif", "'Open Sans Condensed', sans-serif", "'Poiret One', cursive", "'Indie Flower', cursive", "'Pacifico', cursive", "'Teko', sans-serif", "'Abril Fatface', cursive", "'Gloria Hallelujah', cursive", "'Righteous', cursive", "'Annie Use Your Telescope', cursive" ] font_sizes = ["17px", "19px", "21px", "23px", "25px", "27px"] font_tops = ["11px", "11px", "13px", "13px", "15px", "15px"] letter_spacings = ["-6px", "-4px", "-3px", "-2px", "-1px", "0px", "1px", "2px"] text_shadows = [ "0px 0px 1px", "0px 0px 2px", "1px 0px 0px", "0px 0px 0px", "0px 1px 0px", "0px 2px 0px", "0px 1px 1px", "1px 1px 0px", "1px 0px 1px" ] skew_angles = ["-25deg", "-20deg", "-15deg", "-10deg", "0deg", "10deg", "15deg", "20deg", "25deg"] # Create HTML for each letter with random styling (black color) letters = list(word) styled_letters = [] for i, letter in enumerate(letters): style = { 'font-family': random.choice(fonts), 'line-height': '110%', 'font-size': random.choice(font_sizes), 'letter-spacing': random.choice(letter_spacings), 'text-shadow': random.choice(text_shadows), 'transform': f'skew({random.choice(skew_angles)})', 'margin-top': random.choice(["-0.06cm", "-0.03cm", "0.00cm", "0.03cm", "0.06cm"]), 'position': 'relative', 'top': random.choice(font_tops), 'color': '#000000', # Black color 'display': 'inline-block', 'margin': '0 1px' } # Convert style to inline CSS style_str = '; '.join([f'{k}: {v}' for k, v in style.items()]) # Wrap the letter in a span styled_letter = f'{letter}' styled_letters.append(styled_letter) # Combine letters into a container with inline display return f'{" ".join(styled_letters)}' # Function to generate the movement design (random color) def generate_movement_design(word): """Generate movement design for the special word in random color.""" # Controlled randomization parameters (could be different ranges if desired) # Reuse the same parameters or define new ranges for the movement fonts = [ "'VT323', monospace", "'Josefin Sans', sans-serif", "'Rajdhani', sans-serif", "'Anton', sans-serif", # You can include different fonts if you want ] font_sizes = ["20px", "22px", "24px", "26px", "28px", "30px"] font_tops = ["10px", "12px", "14px", "16px"] letter_spacings = ["-3px", "-2px", "-1px", "0px", "1px", "2px", "3px"] text_shadows = [ "0px 0px 5px #ff0000", # Red glow "0px 0px 5px #00ff00", # Green glow "0px 0px 5px #0000ff", # Blue glow # Add more if desired ] skew_angles = ["-15deg", "-10deg", "0deg", "10deg", "15deg"] # Generate a unique animation name for CSS animation_name = f"animate_{random.randint(0, 10000)}" # Create CSS keyframes for the animation keyframes = f""" @keyframes {animation_name} {{ 0% {{ transform: translateY(0px); }} 50% {{ transform: translateY(-10px); }} 100% {{ transform: translateY(0px); }} }} """ # Generate random color random_color = f'#{random.randint(0, 0xFFFFFF):06x}' # Create HTML for each letter with random styling and animation (random color) letters = list(word) styled_letters = [] for i, letter in enumerate(letters): style = { 'font-family': random.choice(fonts), 'line-height': '110%', 'font-size': random.choice(font_sizes), 'letter-spacing': random.choice(letter_spacings), 'text-shadow': random.choice(text_shadows), 'transform': f'skew({random.choice(skew_angles)})', 'margin-top': random.choice(["-0.03cm", "0.00cm", "0.03cm"]), 'position': 'relative', 'top': random.choice(font_tops), 'color': random_color, # Random color 'display': 'inline-block', 'margin': '0 1px', 'animation': f'{animation_name} 1s ease-in-out forwards' } # Convert style to inline CSS style_str = '; '.join([f'{k}: {v}' for k, v in style.items()]) # Wrap the letter in a span styled_letter = f'{letter}' styled_letters.append(styled_letter) # Combine letters into a container with inline display and include keyframes return f''' {" ".join(styled_letters)} ''' # Global variable to store the initial design initial_word_design = "" def process_text(input_text): """Process text and generate the initial output with special word styled in black.""" global initial_word_design # Generate text with limited length generated = generator(input_text, max_length=50, num_return_sequences=1) generated_text = generated[0]['generated_text'] # Limit output length to prevent overflow generated_text = generated_text[:300] # Limit to first 300 characters # Split text into words words = generated_text.split() # Check for special words and apply initial styling for i, word in enumerate(words): # Clean the word for matching clean_word = ''.join(filter(str.isalnum, word)).lower() if clean_word in SPECIAL_WORDS: # Store the initial design initial_word_design = generate_initial_design(word) words[i] = initial_word_design else: words[i] = word # Combine words back into HTML-formatted text output_html = ' '.join(words) # Build the complete HTML final_output = f"""