abhlash commited on
Commit
0979a5b
·
1 Parent(s): 924bd16

updated app

Browse files
Files changed (1) hide show
  1. app.py +33 -31
app.py CHANGED
@@ -5,6 +5,7 @@ from dotenv import load_dotenv
5
  import logging
6
  import sys # Ensure sys is imported
7
  from huggingface_hub import login, HfApi
 
8
 
9
  # Load environment variables
10
  load_dotenv()
@@ -51,41 +52,42 @@ except Exception as e:
51
  # Function to generate a formatted email
52
  def generate_email(recipient_name, recipient_email, industry, recipient_role, details):
53
  prompt = (
54
- f"Write a cold outreach email for a {recipient_role} "
55
- f"working in the {industry} industry. Use the following details: {details}. "
56
- "The email should be professional and engaging, without any additional instructions or templates."
57
  )
58
- inputs = tokenizer(prompt, return_tensors="pt")
59
- outputs = model.generate(**inputs, max_length=300, num_return_sequences=1, temperature=0.7)
60
- email_body = tokenizer.decode(outputs[0], skip_special_tokens=True)
61
-
62
- # Extract only the relevant email content
63
- email_lines = email_body.split('\n')
64
- relevant_lines = []
65
- for line in email_lines:
66
- if line.strip().lower().startswith(('dear', 'hello', 'hi')):
67
- relevant_lines = [line]
68
- elif relevant_lines:
69
- if line.strip().lower().startswith(('sincerely', 'best regards', 'regards', 'thank you')):
70
- break
71
- relevant_lines.append(line)
72
-
73
- cleaned_email_body = '\n'.join(relevant_lines).strip()
74
-
75
- # Format the email
76
- formatted_email = f"""\
77
- To: {recipient_name} <{recipient_email}>
78
- Subject: Collaboration Opportunity
79
 
80
- {cleaned_email_body}
 
 
 
81
 
82
- Best regards,
83
- Jane Smith
84
- Android Developer
85
- Albertsons
86
- [Your Contact Information]
87
  """
88
- return formatted_email
89
 
90
  # Create Gradio interface
91
  iface = gr.Interface(
 
5
  import logging
6
  import sys # Ensure sys is imported
7
  from huggingface_hub import login, HfApi
8
+ import torch
9
 
10
  # Load environment variables
11
  load_dotenv()
 
52
  # Function to generate a formatted email
53
  def generate_email(recipient_name, recipient_email, industry, recipient_role, details):
54
  prompt = (
55
+ f"Write a short professional email to {recipient_name}, "
56
+ f"a {recipient_role} in the {industry} industry. "
57
+ f"Mention: {details}. Keep it under 100 words."
58
  )
59
+ try:
60
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
61
+
62
+ # Check if we're using CPU or GPU
63
+ device = "cuda" if torch.cuda.is_available() else "cpu"
64
+ model.to(device)
65
+ inputs = {k: v.to(device) for k, v in inputs.items()}
66
+
67
+ with torch.no_grad():
68
+ outputs = model.generate(
69
+ **inputs,
70
+ max_length=200, # Reduced max length
71
+ num_return_sequences=1,
72
+ temperature=0.7,
73
+ top_k=50,
74
+ top_p=0.95,
75
+ do_sample=True
76
+ )
77
+
78
+ email_body = tokenizer.decode(outputs[0], skip_special_tokens=True)
79
+ logging.info(f"Generated raw email body: {email_body}")
80
 
81
+ # Minimal cleaning
82
+ email_body = email_body.strip()
83
+ if not email_body:
84
+ raise ValueError("Generated email body is empty")
85
 
86
+ # Format the email
87
+ formatted_email = f"""\
88
+ To: {recipient_name}
 
 
89
  """
90
+ return formatted_email
91
 
92
  # Create Gradio interface
93
  iface = gr.Interface(