import gradio as gr import os from openai import OpenAI api_key = os.getenv("OPENAI_API_KEY") client = OpenAI(api_key=api_key) # template to instruct the assistant SYSTEM_PROMPT = { "role": "system", "content": ( "You are an expert email writing assistant. Given a description of the email's purpose and the desired tone, " "write a clear and professional email. Avoid including headers like 'Subject:' unless specifically requested." ) } def generate_email(description, tone): messages = [ SYSTEM_PROMPT, { "role": "user", "content": f"Write an email in a {tone.lower()} tone. Here is the context: {description}" } ] response = client.chat.completions.create( model="gpt-3.5-turbo", messages=messages, temperature=0.7, max_tokens=500 ) return response.choices[0].message.content.strip() # Gradio UI with gr.Blocks() as app: gr.Markdown("## ✉️ AI Email Generator") with gr.Row(): description = gr.Textbox(label="What's the email about?", placeholder="e.g., Asking for a raise", lines=3) tone = gr.Dropdown(["Formal", "Friendly", "Apologetic", "Persuasive", "Thankful", "Urgent"], value="Formal", label="Tone") generate_btn = gr.Button("Generate Email") email_output = gr.Textbox(label="Generated Email", lines=12) generate_btn.click(fn=generate_email, inputs=[description, tone], outputs=email_output) app.launch()