Spaces:
Sleeping
Sleeping
File size: 1,493 Bytes
f2e5f54 4ab2f7b f2e5f54 cbd2edc f2e5f54 4ab2f7b f2e5f54 4ab2f7b f2e5f54 4ab2f7b f2e5f54 cbd2edc f2e5f54 4ab2f7b f2e5f54 4ab2f7b f2e5f54 4ab2f7b f2e5f54 | 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 | 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() |