Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from transformers import AutoTokenizer, AutoModelForCausalLM | |
| from huggingface_hub import login | |
| import os | |
| from dotenv import load_dotenv | |
| import logging | |
| import sys # Ensure sys is imported | |
| # Load environment variables | |
| load_dotenv() | |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', stream=sys.stdout) | |
| # Login to Hugging Face using the token | |
| hf_token = os.getenv('HUGGING_FACE_TOKEN') | |
| if hf_token: | |
| #login(token=hf_token) | |
| os.environ["HUGGINGFACEHUB_API_TOKEN"] = hf_token | |
| else: | |
| raise ValueError("HUGGING_FACE_TOKEN environment variable not set.") | |
| # Load the Llama-3.1-8B model and tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B") | |
| model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B") | |
| # Function to generate a formatted email | |
| def generate_email(recipient_name, recipient_email, industry, recipient_role, details): | |
| prompt = ( | |
| f"Write a cold outreach email for a {recipient_role} " | |
| f"working in the {industry} industry. Use the following details: {details}. " | |
| "The email should be professional and engaging, without any additional instructions or templates." | |
| ) | |
| inputs = tokenizer(prompt, return_tensors="pt") | |
| outputs = model.generate(**inputs, max_length=300, num_return_sequences=1, temperature=0.7) | |
| email_body = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # Extract only the relevant email content | |
| email_lines = email_body.split('\n') | |
| relevant_lines = [] | |
| for line in email_lines: | |
| if line.strip().lower().startswith(('dear', 'hello', 'hi')): | |
| relevant_lines = [line] | |
| elif relevant_lines: | |
| if line.strip().lower().startswith(('sincerely', 'best regards', 'regards', 'thank you')): | |
| break | |
| relevant_lines.append(line) | |
| cleaned_email_body = '\n'.join(relevant_lines).strip() | |
| # Format the email | |
| formatted_email = f"""\ | |
| To: {recipient_name} <{recipient_email}> | |
| Subject: Collaboration Opportunity | |
| {cleaned_email_body} | |
| Best regards, | |
| Jane Smith | |
| Android Developer | |
| Albertsons | |
| [Your Contact Information] | |
| """ | |
| return formatted_email | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=generate_email, | |
| inputs=[ | |
| gr.Textbox(lines=1, label="Recipient Name"), | |
| gr.Textbox(lines=1, label="Recipient Email"), | |
| gr.Textbox(lines=1, label="Industry (e.g., Technology, Healthcare)"), | |
| gr.Textbox(lines=1, label="Recipient Role (e.g., Manager, Director)"), | |
| gr.Textbox(lines=5, label="Personal/Company Details (e.g., name, product)"), | |
| ], | |
| outputs="text", | |
| title="EmailGenie: AI-Powered Email Generator", | |
| description="Automate the creation of personalized emails to increase engagement and conversion rates. Enter details to generate tailored emails." | |
| ) | |
| # Launch the app | |
| if __name__ == '__main__': | |
| iface.launch() | |