abhishekjoel's picture
Update app.py
732941a verified
import gradio as gr
import os
from groq import Groq
# Load API key from environment variable
API_KEY = os.environ.get("GROQ_API_KEY")
# Check if API key is set
if API_KEY is None:
raise ValueError("GROQ_API_KEY environment variable is not set")
# Initialize Groq client
client = Groq(api_key=API_KEY)
def generate_email(business_or_individual, company_name, company_industry, recipient_name, recipient_role, tone, tokens, additional_info):
# Construct a prompt based on user inputs
prompt = f"Write a {tone} email to {recipient_name} at {company_name} in the {company_industry} industry."
prompt += f" The recipient is a {recipient_role}."
if additional_info:
prompt += f" Additional information: {additional_info}"
prompt += f" The email should have approximately {tokens} tokens."
# Use Groq API to generate email
try:
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model="llama-3.1-8b-instant",
)
generated_text = chat_completion.choices[0].message.content
except Exception as e:
generated_text = f"Error: {e}"
return generated_text
iface = gr.Interface(
fn=generate_email,
inputs=[
gr.Radio(["Business", "Individual"], label="Business or Individual"),
gr.Textbox(label="Company Name", placeholder="Enter company name here...", lines=2),
gr.Textbox(label="Company Industry", placeholder="Enter industry here...", lines=2),
gr.Textbox(label="Recipient's Name", placeholder="Enter recipient's name here...", lines=2),
gr.Textbox(label="Recipient Role", placeholder="Enter role here...", lines=2),
gr.Radio(["Professional", "Casual", "Cold"], label="Tone"),
gr.Slider(minimum=200, maximum=1000, step=100, value=500, label="Tokens"),
gr.Textbox(label="Prompt Additional Information (optional)", placeholder="Enter any additional information here...", lines=4),
],
outputs="text",
title="Email Generator powered by Llama 3.1",
description="Generate personalized emails using AI",
examples=[
["Business", "Company A", "Technology", "John Doe", "CEO", "Professional", 500, "This is a test email."],
["Individual", "Company B", "Finance", "Jane Smith", "Manager", "Casual", 300, ""],
],
css="""
.gradio-button {
background-color: #FFA07A;
color: #000;
}
.gradio-checkbox {
accent-color: #FFA07A;
}
.gradio-label {
color: orange;
}
""",
)
iface.launch()