Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| from openai import OpenAI | |
| # Initialize OpenRouter client | |
| client = OpenAI( | |
| base_url="https://openrouter.ai/api/v1", | |
| api_key=os.environ.get("OPENROUTER_API_KEY"), | |
| ) | |
| def generate_subject_lines(email_body, tone, num_suggestions): | |
| """Generate email subject lines based on content and tone.""" | |
| if not email_body.strip(): | |
| return "Please enter email content." | |
| # Construct the prompt | |
| prompt = f"""Generate {num_suggestions} compelling email subject lines for the following email content. | |
| Tone: {tone} | |
| Email content: | |
| {email_body} | |
| Requirements: | |
| - Make them attention-grabbing but not clickbait | |
| - Keep them under 60 characters when possible | |
| - Match the {tone} tone | |
| - Make each one distinct | |
| Provide only the subject lines, numbered 1-{num_suggestions}.""" | |
| try: | |
| # Call OpenRouter API with a free model | |
| response = client.chat.completions.create( | |
| model="google/gemma-2-9b-it:free", # Free model on OpenRouter | |
| messages=[ | |
| {"role": "user", "content": prompt} | |
| ], | |
| max_tokens=500, | |
| temperature=0.8, | |
| ) | |
| result = response.choices[0].message.content | |
| return result | |
| except Exception as e: | |
| return f"Error: {str(e)}\n\nMake sure your OPENROUTER_API_KEY is set correctly." | |
| # Create Gradio interface | |
| with gr.Blocks(title="Email Subject Line Generator", theme=gr.themes.Soft()) as demo: | |
| gr.Markdown(""" | |
| # π§ Smart Email Subject Line Generator | |
| Generate compelling subject lines for your emails using AI. Perfect for: | |
| - Business emails | |
| - Marketing campaigns | |
| - Cold outreach | |
| - Newsletter titles | |
| """) | |
| with gr.Row(): | |
| with gr.Column(): | |
| email_input = gr.Textbox( | |
| label="Email Content", | |
| placeholder="Paste your email content here...", | |
| lines=10 | |
| ) | |
| tone_select = gr.Radio( | |
| choices=["Professional", "Friendly", "Urgent", "Casual", "Formal"], | |
| label="Tone", | |
| value="Professional" | |
| ) | |
| num_select = gr.Slider( | |
| minimum=3, | |
| maximum=10, | |
| value=5, | |
| step=1, | |
| label="Number of Suggestions" | |
| ) | |
| generate_btn = gr.Button("Generate Subject Lines", variant="primary") | |
| with gr.Column(): | |
| output = gr.Textbox( | |
| label="Generated Subject Lines", | |
| lines=15, | |
| show_copy_button=True | |
| ) | |
| # Example emails | |
| gr.Markdown("### π Try These Examples:") | |
| gr.Examples( | |
| examples=[ | |
| [ | |
| "Hi there,\n\nI wanted to reach out about our new productivity tool that's helped over 5,000 teams save 10+ hours per week. We're offering a free 30-day trial for early adopters. Would you be interested in a quick demo?\n\nBest regards", | |
| "Professional", | |
| 5 | |
| ], | |
| [ | |
| "Hey team,\n\nQuick reminder that our monthly all-hands meeting is tomorrow at 2 PM. We'll be discussing Q4 goals and the new office policy. Please come prepared with questions!\n\nSee you there!", | |
| "Friendly", | |
| 4 | |
| ], | |
| ], | |
| inputs=[email_input, tone_select, num_select], | |
| label=None | |
| ) | |
| # Connect the button | |
| generate_btn.click( | |
| fn=generate_subject_lines, | |
| inputs=[email_input, tone_select, num_select], | |
| outputs=output | |
| ) | |
| # Launch the app | |
| if __name__ == "__main__": | |
| demo.launch() |