Spaces:
Sleeping
Sleeping
| 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'] | |
| def generate_circular_text_design(word): | |
| """Generate a simple animated design for a word using CSS animations.""" | |
| # Controlled randomization parameters | |
| fonts = [ | |
| "'Dosis', sans-serif", | |
| "'Josefin Sans', sans-serif", | |
| "'Orbitron', sans-serif", | |
| "'Roboto Condensed', sans-serif" | |
| ] | |
| font_size = random.choice([18, 20, 22, 24]) | |
| skew_angle = random.choice([-15, -10, 0, 10, 15]) | |
| base_color = f'#{random.randint(0, 0xFFFFFF):06x}' | |
| # Unique animation name | |
| animation_name = f"animation_{random.randint(0, 10000)}" | |
| # CSS Keyframes | |
| keyframes = f""" | |
| @keyframes {animation_name} {{ | |
| 0% {{ transform: skew({skew_angle}deg); color: {base_color}; }} | |
| 50% {{ transform: skew({-skew_angle}deg); color: {base_color}; }} | |
| 100% {{ transform: skew({skew_angle}deg); color: {base_color}; }} | |
| }} | |
| """ | |
| # Style properties | |
| style = { | |
| 'font-family': random.choice(fonts), | |
| 'font-size': f'{font_size}px', | |
| 'color': base_color, | |
| 'display': 'inline-block', | |
| 'animation': f'{animation_name} 2s infinite', | |
| 'margin': '0 2px' | |
| } | |
| # Convert style to inline CSS | |
| style_str = '; '.join([f'{k}: {v}' for k, v in style.items()]) | |
| # Construct HTML with embedded style and animation | |
| styled_word = f''' | |
| <style> | |
| {keyframes} | |
| </style> | |
| <span style="{style_str}">{word}</span> | |
| ''' | |
| return styled_word | |
| def process_text(input_text): | |
| """Process text and apply special styling to matching words.""" | |
| # Limit input length to prevent long processing times | |
| max_input_length = 20 # Adjust as needed | |
| input_text = ' '.join(input_text.split()[:max_input_length]) | |
| # 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 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: | |
| words[i] = generate_circular_text_design(word) | |
| else: | |
| words[i] = word | |
| # Combine words back into HTML-formatted text | |
| output_html = ' '.join(words) | |
| # Build the complete HTML | |
| final_output = f""" | |
| <html> | |
| <head> | |
| <!-- Include only necessary fonts --> | |
| <link href='https://fonts.googleapis.com/css2?family=Dosis&family=Josefin+Sans&family=Orbitron&family=Roboto+Condensed&display=swap' rel='stylesheet'> | |
| </head> | |
| <body style='background-color: #000; color: #fff; padding: 20px; font-size: 18px; line-height: 1.6; font-family: "Josefin Sans", sans-serif;'> | |
| <div style='max-width: 800px; margin: auto;'>{output_html}</div> | |
| </body> | |
| </html> | |
| """ | |
| return final_output | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=process_text, | |
| inputs="text", | |
| outputs=gr.HTML(label="Generated Text"), | |
| title="Circular Text Styler", | |
| description="Enter a prompt to generate text with special word styling." | |
| ) | |
| # Launch the interface | |
| iface.launch() |