|
import gradio as gr |
|
import requests |
|
from serpapi import GoogleSearch |
|
import os |
|
from datetime import datetime |
|
|
|
|
|
serpapi_key = os.getenv("SERPAPI_API_KEY") |
|
groq_api_key = os.getenv("GROQ_CLOUD_API_KEY") |
|
|
|
|
|
def get_linkedin_profile_via_serpapi(name, city): |
|
query = f"{name} LinkedIn {city}" |
|
params = { |
|
"q": query, |
|
"hl": "en", |
|
"gl": "us", |
|
"api_key": serpapi_key, |
|
} |
|
search = GoogleSearch(params) |
|
results = search.get_dict() |
|
|
|
|
|
for result in results.get("organic_results", []): |
|
if "linkedin.com" in result.get("link", ""): |
|
return result["link"] |
|
|
|
return "LinkedIn profile not found" |
|
|
|
|
|
def generate_and_correct_email(bio, company_name, role): |
|
url = "https://api.groq.com/openai/v1/chat/completions" |
|
headers = { |
|
"Authorization": f"Bearer {groq_api_key}", |
|
"Content-Type": "application/json", |
|
} |
|
|
|
|
|
prompt = f""" |
|
Write a professional email applying for a {role} position at {company_name}. Use this bio: {bio}. |
|
The email should include an introduction, relevant experience, skills, and a closing. |
|
Ensure that the email is grammatically correct and formal. |
|
""" |
|
|
|
|
|
data = { |
|
"messages": [ |
|
{ |
|
"role": "user", |
|
"content": prompt |
|
} |
|
], |
|
"model": "llama3-8b-8192" |
|
} |
|
|
|
response = requests.post(url, headers=headers, json=data) |
|
|
|
if response.status_code == 200: |
|
|
|
return response.json()["choices"][0]["message"]["content"].strip() |
|
else: |
|
|
|
print(f"Error: {response.status_code}, {response.text}") |
|
return "Error generating email. Please check your API key or try again later." |
|
|
|
|
|
def validate_dob(dob): |
|
try: |
|
|
|
datetime.strptime(dob, "%d-%m-%Y") |
|
return True |
|
except ValueError: |
|
|
|
return False |
|
|
|
|
|
def create_email(name, dob, city, company_name, role, email, phone): |
|
|
|
if not validate_dob(dob): |
|
return "Invalid Date of Birth format. Please use DD-MM-YYYY." |
|
|
|
|
|
linkedin_profile = get_linkedin_profile_via_serpapi(name, city) |
|
|
|
|
|
bio = f"{name} is a professional in {city}." |
|
|
|
|
|
generated_email = generate_and_correct_email(bio, company_name, role) |
|
|
|
|
|
signature = f"\n\nBest regards,\n{name}\nEmail: {email}\nPhone: {phone}\nLinkedIn: {linkedin_profile}" |
|
|
|
|
|
return generated_email + signature |
|
|
|
|
|
def gradio_ui(): |
|
|
|
name_input = gr.Textbox(label="Name", placeholder="Enter your name") |
|
dob_input = gr.Textbox(label="Date of Birth", placeholder="Enter your date of birth (DD-MM-YYYY)") |
|
city_input = gr.Textbox(label="City", placeholder="Enter your city of residence") |
|
|
|
company_name_input = gr.Textbox(label="Company Name", placeholder="Enter the name of the company you are applying to") |
|
role_input = gr.Textbox(label="Role", placeholder="Enter the role you are applying for") |
|
|
|
email_input = gr.Textbox(label="Email Address", placeholder="Enter your email address") |
|
phone_input = gr.Textbox(label="Phone Number", placeholder="Enter your phone number") |
|
|
|
|
|
email_output = gr.Textbox(label="Generated Email", placeholder="Your generated email will appear here", lines=10) |
|
|
|
|
|
demo = gr.Interface( |
|
fn=create_email, |
|
inputs=[name_input, dob_input, city_input, company_name_input, role_input, email_input, phone_input], |
|
outputs=[email_output], |
|
title="Email Writing AI Agent", |
|
description="Generate a professional email for a job application by providing your basic info.", |
|
allow_flagging="never" |
|
) |
|
|
|
|
|
demo.launch() |
|
|
|
|
|
if __name__ == "__main__": |
|
gradio_ui() |
|
|