abhlash commited on
Commit
c5331aa
·
1 Parent(s): 568cb3c

updated the app

Browse files
Files changed (2) hide show
  1. app.py +65 -39
  2. requirements.txt +5 -4
app.py CHANGED
@@ -1,55 +1,81 @@
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
- from huggingface_hub import login, HfApi
4
  import os
5
  from dotenv import load_dotenv
6
- from flask import flash, redirect, url_for, session
 
7
 
8
  # Load environment variables
9
  load_dotenv()
 
10
 
11
- # Login to Hugging Face (you'll need to set HUGGINGFACE_TOKEN in your Secrets)
12
- def login():
13
- # Get the Hugging Face token from environment variable
14
- hf_token = os.getenv('HUGGING_FACE_TOKEN')
15
-
16
- if not hf_token:
17
- flash('Hugging Face token not found. Please set the HUGGING_FACE_TOKEN environment variable.', 'error')
18
- return redirect(url_for('index'))
19
-
20
- # Use the token for authentication
21
- try:
22
- api = HfApi()
23
- user = api.whoami(token=hf_token)
24
- if user:
25
- session['logged_in'] = True
26
- flash('Successfully authenticated with Hugging Face', 'success')
27
- else:
28
- session['logged_in'] = False
29
- flash('Failed to authenticate with Hugging Face', 'error')
30
- except Exception as e:
31
- session['logged_in'] = False
32
- flash(f'Error during Hugging Face authentication: {str(e)}', 'error')
33
- return redirect(url_for('index'))
34
-
35
- # Load model and tokenizer
36
- tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-70B")
37
- model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-70B")
38
-
39
- def generate_email(context):
40
- prompt = f"Generate a professional email based on the following context: {context}"
41
  inputs = tokenizer(prompt, return_tensors="pt")
42
- outputs = model.generate(**inputs, max_length=300)
43
- return tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Create Gradio interface
46
  iface = gr.Interface(
47
  fn=generate_email,
48
- inputs=gr.Textbox(lines=5, label="Context"),
49
- outputs=gr.Textbox(label="Generated Email"),
50
- title="Email Generator",
51
- description="Enter the context, and the app will generate an email using Llama 3.1 70B."
 
 
 
 
 
 
52
  )
53
 
54
  # Launch the app
55
- iface.launch()
 
 
1
  import gradio as gr
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ from huggingface_hub import login
4
  import os
5
  from dotenv import load_dotenv
6
+ import logging
7
+ import sys # Ensure sys is imported
8
 
9
  # Load environment variables
10
  load_dotenv()
11
+ logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', stream=sys.stdout)
12
 
13
+ # Login to Hugging Face using the token
14
+ hf_token = os.getenv('HUGGING_FACE_TOKEN')
15
+ if hf_token:
16
+ #login(token=hf_token)
17
+ os.environ["HUGGINGFACEHUB_API_TOKEN"] = hf_token
18
+ else:
19
+ raise ValueError("HUGGING_FACE_TOKEN environment variable not set.")
20
+
21
+ # Load the Llama-3.1-8B model and tokenizer
22
+ tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-3.1-8B")
23
+ model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-3.1-8B")
24
+
25
+ # Function to generate a formatted email
26
+ def generate_email(recipient_name, recipient_email, industry, recipient_role, details):
27
+ prompt = (
28
+ f"Write a cold outreach email for a {recipient_role} "
29
+ f"working in the {industry} industry. Use the following details: {details}. "
30
+ "The email should be professional and engaging, without any additional instructions or templates."
31
+ )
 
 
 
 
 
 
 
 
 
 
 
32
  inputs = tokenizer(prompt, return_tensors="pt")
33
+ outputs = model.generate(**inputs, max_length=300, num_return_sequences=1, temperature=0.7)
34
+ email_body = tokenizer.decode(outputs[0], skip_special_tokens=True)
35
+
36
+ # Extract only the relevant email content
37
+ email_lines = email_body.split('\n')
38
+ relevant_lines = []
39
+ for line in email_lines:
40
+ if line.strip().lower().startswith(('dear', 'hello', 'hi')):
41
+ relevant_lines = [line]
42
+ elif relevant_lines:
43
+ if line.strip().lower().startswith(('sincerely', 'best regards', 'regards', 'thank you')):
44
+ break
45
+ relevant_lines.append(line)
46
+
47
+ cleaned_email_body = '\n'.join(relevant_lines).strip()
48
+
49
+ # Format the email
50
+ formatted_email = f"""\
51
+ To: {recipient_name} <{recipient_email}>
52
+ Subject: Collaboration Opportunity
53
+
54
+ {cleaned_email_body}
55
+
56
+ Best regards,
57
+ Jane Smith
58
+ Android Developer
59
+ Albertsons
60
+ [Your Contact Information]
61
+ """
62
+ return formatted_email
63
 
64
  # Create Gradio interface
65
  iface = gr.Interface(
66
  fn=generate_email,
67
+ inputs=[
68
+ gr.Textbox(lines=1, label="Recipient Name"),
69
+ gr.Textbox(lines=1, label="Recipient Email"),
70
+ gr.Textbox(lines=1, label="Industry (e.g., Technology, Healthcare)"),
71
+ gr.Textbox(lines=1, label="Recipient Role (e.g., Manager, Director)"),
72
+ gr.Textbox(lines=5, label="Personal/Company Details (e.g., name, product)"),
73
+ ],
74
+ outputs="text",
75
+ title="EmailGenie: AI-Powered Email Generator",
76
+ description="Automate the creation of personalized emails to increase engagement and conversion rates. Enter details to generate tailored emails."
77
  )
78
 
79
  # Launch the app
80
+ if __name__ == '__main__':
81
+ iface.launch()
requirements.txt CHANGED
@@ -1,4 +1,5 @@
1
- gradio
2
- transformers
3
- torch
4
- python-dotenv
 
 
1
+ gradio==3.50.2
2
+ transformers==4.36.2
3
+ torch==2.1.2
4
+ huggingface_hub==0.20.2
5
+ python-dotenv==1.0.0