File size: 3,904 Bytes
41f0774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2253f9
6bcf519
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d2253f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41f0774
 
 
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
import os
import gradio as gr
from groq import Groq

client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

def structure_info(info):
    prompt = f"Please just structure and summarize the following information about a person or company and just simply give the result directly: {info}"
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model="llama-3.1-8b-instant",
    )
    return chat_completion.choices[0].message.content

def generate_email(sender_name, email_subject, recipient, structured_info):
    prompt = f"""I'm {sender_name}. Write a personalized email to {recipient} about {email_subject} which will be personalized to {recipient} by understanding {structured_info}. The email should introduce the sender, briefly describe the subject, and highlight its key benefits or relevance to the recipient's profile or company."""
    
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": prompt,
            }
        ],
        model="llama-3.1-8b-instant",
    )
    return chat_completion.choices[0].message.content

def email_interface(name, subject, recipient, recipient_info):
    structured_info = structure_info(recipient_info)
    return generate_email(name, subject, recipient, structured_info)

with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
    gr.Markdown(
        """
        # 📧 EmailGenie
        Generate personalized emails based on recipient information using AI.
        """
    )
    
    with gr.Row():
        with gr.Column(scale=1):
            name = gr.Textbox(label="Your Name", placeholder="John Doe")
            subject = gr.Textbox(label="Email Subject", placeholder="Collaboration on AI Project")
        with gr.Column(scale=1):
            recipient = gr.Textbox(label="Recipient Name/Company", placeholder="TechCorp")
            recipient_info = gr.Textbox(
                label="Recipient Information",
                placeholder="e.g., Name, Motive, What they do",
                lines=3
            )
    
    generate_button = gr.Button("Generate Email", variant="primary")
    
    output = gr.Textbox(label="Generated Email", lines=10)
    
    generate_button.click(
        email_interface,
        inputs=[name, subject, recipient, recipient_info],
        outputs=output
    )
    
    gr.Examples(
        [
            ["John Doe", "Collaboration on AI Project", "TechCorp", "TechCorp is a leading AI research company focused on developing cutting-edge machine learning algorithms."],
            ["Jane Smith", "AI Workshop Invitation", "Dr. Alex Johnson", "Dr. Alex Johnson is a renowned AI researcher specializing in natural language processing at Stanford University."]
        ],
        inputs=[name, subject, recipient, recipient_info],
        outputs=output,
        fn=email_interface,
        cache_examples=True,
    )
    
    # Add a slider for email formality
    formality = gr.Slider(minimum=1, maximum=5, value=3, step=1, label="Email Formality (1: Casual, 5: Formal)")
    
    # Add a checkbox for including a call-to-action
    include_cta = gr.Checkbox(label="Include Call-to-Action")
    
    # Add a dropdown for email tone
    tone = gr.Dropdown(["Friendly", "Professional", "Enthusiastic", "Neutral"], label="Email Tone", value="Professional")
    
    # Add an accordion with tips
    with gr.Accordion("Tips for Effective Emails"):
        gr.Markdown("""
        1. Keep it concise and to the point
        2. Use a clear and descriptive subject line
        3. Personalize the email based on the recipient's background
        4. Proofread before sending
        5. Include a clear call-to-action if necessary
        """)

if __name__ == "__main__":
    demo.launch()