Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Initialize OpenRouter client
|
| 6 |
+
client = OpenAI(
|
| 7 |
+
base_url="https://openrouter.ai/api/v1",
|
| 8 |
+
api_key=os.environ.get("OPENROUTER_API_KEY"),
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
def generate_subject_lines(email_body, tone, num_suggestions):
|
| 12 |
+
"""Generate email subject lines based on content and tone."""
|
| 13 |
+
|
| 14 |
+
if not email_body.strip():
|
| 15 |
+
return "Please enter email content."
|
| 16 |
+
|
| 17 |
+
# Construct the prompt
|
| 18 |
+
prompt = f"""Generate {num_suggestions} compelling email subject lines for the following email content.
|
| 19 |
+
|
| 20 |
+
Tone: {tone}
|
| 21 |
+
|
| 22 |
+
Email content:
|
| 23 |
+
{email_body}
|
| 24 |
+
|
| 25 |
+
Requirements:
|
| 26 |
+
- Make them attention-grabbing but not clickbait
|
| 27 |
+
- Keep them under 60 characters when possible
|
| 28 |
+
- Match the {tone} tone
|
| 29 |
+
- Make each one distinct
|
| 30 |
+
|
| 31 |
+
Provide only the subject lines, numbered 1-{num_suggestions}."""
|
| 32 |
+
|
| 33 |
+
try:
|
| 34 |
+
# Call OpenRouter API with a free model
|
| 35 |
+
response = client.chat.completions.create(
|
| 36 |
+
model="google/gemma-2-9b-it:free", # Free model on OpenRouter
|
| 37 |
+
messages=[
|
| 38 |
+
{"role": "user", "content": prompt}
|
| 39 |
+
],
|
| 40 |
+
max_tokens=500,
|
| 41 |
+
temperature=0.8,
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
result = response.choices[0].message.content
|
| 45 |
+
return result
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error: {str(e)}\n\nMake sure your OPENROUTER_API_KEY is set correctly."
|
| 49 |
+
|
| 50 |
+
# Create Gradio interface
|
| 51 |
+
with gr.Blocks(title="Email Subject Line Generator", theme=gr.themes.Soft()) as demo:
|
| 52 |
+
gr.Markdown("""
|
| 53 |
+
# 📧 Smart Email Subject Line Generator
|
| 54 |
+
|
| 55 |
+
Generate compelling subject lines for your emails using AI. Perfect for:
|
| 56 |
+
- Business emails
|
| 57 |
+
- Marketing campaigns
|
| 58 |
+
- Cold outreach
|
| 59 |
+
- Newsletter titles
|
| 60 |
+
""")
|
| 61 |
+
|
| 62 |
+
with gr.Row():
|
| 63 |
+
with gr.Column():
|
| 64 |
+
email_input = gr.Textbox(
|
| 65 |
+
label="Email Content",
|
| 66 |
+
placeholder="Paste your email content here...",
|
| 67 |
+
lines=10
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
tone_select = gr.Radio(
|
| 71 |
+
choices=["Professional", "Friendly", "Urgent", "Casual", "Formal"],
|
| 72 |
+
label="Tone",
|
| 73 |
+
value="Professional"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
num_select = gr.Slider(
|
| 77 |
+
minimum=3,
|
| 78 |
+
maximum=10,
|
| 79 |
+
value=5,
|
| 80 |
+
step=1,
|
| 81 |
+
label="Number of Suggestions"
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
generate_btn = gr.Button("Generate Subject Lines", variant="primary")
|
| 85 |
+
|
| 86 |
+
with gr.Column():
|
| 87 |
+
output = gr.Textbox(
|
| 88 |
+
label="Generated Subject Lines",
|
| 89 |
+
lines=15,
|
| 90 |
+
show_copy_button=True
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Example emails
|
| 94 |
+
gr.Markdown("### 📝 Try These Examples:")
|
| 95 |
+
gr.Examples(
|
| 96 |
+
examples=[
|
| 97 |
+
[
|
| 98 |
+
"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",
|
| 99 |
+
"Professional",
|
| 100 |
+
5
|
| 101 |
+
],
|
| 102 |
+
[
|
| 103 |
+
"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!",
|
| 104 |
+
"Friendly",
|
| 105 |
+
4
|
| 106 |
+
],
|
| 107 |
+
],
|
| 108 |
+
inputs=[email_input, tone_select, num_select],
|
| 109 |
+
label=None
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Connect the button
|
| 113 |
+
generate_btn.click(
|
| 114 |
+
fn=generate_subject_lines,
|
| 115 |
+
inputs=[email_input, tone_select, num_select],
|
| 116 |
+
outputs=output
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Launch the app
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
demo.launch()
|