Download app.py from derek0890/AdGenAI: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/spaces/derek0890/AdGenAI/resolve/main/app.py
- Command line
-
hf download hf://spaces/derek0890/AdGenAI/app.py
-
curl -L -o app.py https://huggingface.co/spaces/derek0890/AdGenAI/resolve/main/app.py
1.96 kB
| import gradio as gr | |
| from transformers import pipeline | |
| # Load GPT-Neo model, optimized for CPU usage | |
| generator = pipeline( | |
| "text-generation", | |
| model="EleutherAI/gpt-neo-1.3B" # Runs fine on CPU Basic tier | |
| ) | |
| # Recommended length per content type | |
| max_lengths = { | |
| "social media post": 280, | |
| "email newsletter": 800, | |
| "product description": 600, | |
| "ad copy": 400 | |
| } | |
| def generate_marketing_text(prompt, content_type, _, temperature=0.7): | |
| """ | |
| Generate marketing text using GPT-Neo based on topic and content type. | |
| """ | |
| # Smart prompt engineering for better output | |
| enhanced_prompt = f"Write a {content_type} for the following product: {prompt}\nMake it persuasive, professional, and engaging." | |
| # Choose max_length based on content type | |
| max_length = max_lengths.get(content_type, 400) | |
| # Generate text using the model | |
| result = generator( | |
| enhanced_prompt, | |
| max_length=max_length, | |
| temperature=temperature, | |
| do_sample=True, | |
| pad_token_id=50256 | |
| ) | |
| return result[0]['generated_text'] | |
| # Gradio UI definition | |
| demo = gr.Interface( | |
| fn=generate_marketing_text, | |
| inputs=[ | |
| gr.Textbox(lines=3, placeholder="Enter your product or topic here...", label="Topic"), | |
| gr.Radio( | |
| ["social media post", "email newsletter", "product description", "ad copy"], | |
| label="Content Type", | |
| value="social media post" | |
| ), | |
| gr.Slider(minimum=50, maximum=800, value=280, step=10, label="(Auto-set) Max Length", interactive=False), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.1, label="Creativity (Temperature)") | |
| ], | |
| outputs=gr.Textbox(lines=10, label="Generated Marketing Content"), | |
| title="AdGenAI - Marketing Content Generator", | |
| description="Free-tier friendly version using GPT-Neo 1.3B. Enter a topic and select the type of content you want to generate." | |
| ) | |
| # Launch the app | |
| demo.launch() | |