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()