Spaces:
Running
Running
| import gradio as gr | |
| import random | |
| from transformers import pipeline | |
| # Load the model once when the app starts | |
| generator = pipeline('text-generation', model='distilgpt2', max_length=25) | |
| # Predefined words to check | |
| SPECIAL_WORDS = ['weather', 'sun', 'middle', 'summer', 'heat', 'spring', 'winter'] | |
| def generate_design(word, color='#000000', animate=False): | |
| """Generate design for the special word.""" | |
| 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" | |
| ] | |
| font_sizes = ["18px", "19px", "20px"] | |
| letter_spacings = ["-1px", "0px", "1px"] | |
| 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 = ["-10deg", "0deg", "10deg"] | |
| letters = list(word) | |
| styled_letters = [] | |
| animation_name = f"animate_{random.randint(0, 10000)}" if animate else "" | |
| keyframes = f""" | |
| @keyframes {animation_name} {{ | |
| 0% {{ transform: scale(1) rotate(0deg); }} | |
| 50% {{ transform: scale(1.2) rotate(10deg); }} | |
| 100% {{ transform: scale(1) rotate(0deg); }} | |
| }} | |
| """ if animate else "" | |
| for i, letter in enumerate(letters): | |
| style = { | |
| 'font-family': random.choice(fonts), | |
| 'line-height': '1.6', | |
| '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)})', | |
| 'color': color, | |
| 'display': 'inline-block', | |
| 'margin': '0 1px', | |
| 'vertical-align': 'middle', | |
| 'animation': f'{animation_name} 0.5s ease-in-out' if animate else '', | |
| 'animation-delay': f'{i * 0.1}s' if animate else '' | |
| } | |
| style_str = '; '.join([f'{k}: {v}' for k, v in style.items()]) | |
| styled_letter = f'<span class="styled-letter" style="{style_str}">{letter}</span>' | |
| styled_letters.append(styled_letter) | |
| return f''' | |
| <style> | |
| {keyframes} | |
| .styled-letter {{ | |
| transition: all 0.3s ease; | |
| }} | |
| </style> | |
| <span style="display: inline-flex; align-items: baseline; vertical-align: middle; margin: 0 2px;"> | |
| {" ".join(styled_letters)} | |
| </span> | |
| ''' | |
| def process_text(input_text): | |
| """Process text and generate the initial output with special word styled in black.""" | |
| generated = generator(input_text, num_return_sequences=1) | |
| generated_text = generated[0]['generated_text'] | |
| generated_text = generated_text[:200] | |
| words = generated_text.split() | |
| processed_words = [] | |
| for word in words: | |
| clean_word = ''.join(filter(str.isalnum, word)).lower() | |
| if clean_word in SPECIAL_WORDS: | |
| processed_words.append(generate_design(word)) | |
| else: | |
| processed_words.append(word) | |
| output_html = ' '.join(processed_words) | |
| final_output = f""" | |
| <html> | |
| <head> | |
| <link href="https://fonts.googleapis.com/css2?family=VT323&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Josefin+Sans:wght@100&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@300&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Anton&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Caveat&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Patrick+Hand&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Nothing+You+Could+Do&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Reenie+Beanie&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css2?family=Orbitron&display=swap" rel="stylesheet"> | |
| <link href="https://fonts.googleapis.com/css?family=Raleway:500" rel="stylesheet"> | |
| <style> | |
| body {{ | |
| background-color: #fff; | |
| color: #000; | |
| font-size: 18px; | |
| line-height: 1.6; | |
| font-family: "Josefin Sans", sans-serif; | |
| padding: 20px; | |
| }} | |
| </style> | |
| </head> | |
| <body> | |
| <div style='max-width: 800px; margin: auto;'> | |
| {output_html} | |
| </div> | |
| </body> | |
| </html> | |
| """ | |
| return final_output | |
| def trigger_movement(input_html): | |
| """Function to trigger the movement animation for all special words.""" | |
| words = input_html.split() | |
| updated_words = [] | |
| for word in words: | |
| clean_word = ''.join(filter(str.isalnum, word)).lower() | |
| if clean_word in SPECIAL_WORDS: | |
| updated_words.append(generate_design(clean_word, color=f'#{random.randint(0, 0xFFFFFF):06x}', animate=True)) | |
| else: | |
| updated_words.append(word) | |
| updated_html = ' '.join(updated_words) | |
| return updated_html | |
| # Create Gradio interface using Blocks | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# Circular Text Styler\nEnter a prompt to generate text with special word styling.") | |
| with gr.Row(): | |
| input_text = gr.Textbox(label="Input Prompt") | |
| submit_button = gr.Button("Generate") | |
| output_html = gr.HTML() | |
| animate_button = gr.Button("Trigger Movement") | |
| submit_button.click(process_text, inputs=input_text, outputs=output_html) | |
| animate_button.click(trigger_movement, inputs=output_html, outputs=output_html) | |
| # Launch the app | |
| demo.launch() |