File size: 3,738 Bytes
acc32ed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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()