adriiita commited on
Commit
a4662d9
·
verified ·
1 Parent(s): 1d5efe6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -24
app.py CHANGED
@@ -1,8 +1,6 @@
1
  import os
2
  import gradio as gr
3
- from langchain_community.llms import Groq
4
- from langchain.prompts import PromptTemplate
5
- from langchain.chains import LLMChain
6
  import pandas as pd
7
  from datetime import datetime
8
  from sendgrid import SendGridAPIClient
@@ -10,6 +8,9 @@ from sendgrid.helpers.mail import Mail
10
  import json
11
  from pathlib import Path
12
  from typing import Dict, List, Optional
 
 
 
13
 
14
  # Define the storage directory in the Hugging Face Space
15
  STORAGE_DIR = os.environ.get('STORAGE_DIR', 'data')
@@ -48,7 +49,6 @@ class UserProfile:
48
  with open(self.storage_path, 'r') as f:
49
  data = json.load(f)
50
 
51
- # Update existing profile or add new one
52
  profiles = data.get("profiles", [])
53
  profile_exists = False
54
  for i, profile in enumerate(profiles):
@@ -110,33 +110,31 @@ class EmailTemplate:
110
 
111
  class EmailGenie:
112
  def __init__(self):
113
- self.llm = Groq(api_key=os.environ.get("GROQ_API_KEY"))
 
114
  self.sendgrid_client = initialize_email_client()
115
  self.sender_email = os.environ.get("SENDER_EMAIL")
116
  self.user_profile = UserProfile()
117
  self.email_template = EmailTemplate()
118
  self.current_profile = None
119
-
120
- def set_current_profile(self, name: str):
121
- self.current_profile = self.user_profile.get_profile(name)
122
- return self.current_profile is not None
123
-
124
- def structure_info(self, info: str) -> str:
125
- prompt = PromptTemplate(
 
 
126
  input_variables=["info"],
127
  template="Please structure and summarize the following information about a person or company: {info}"
128
  )
129
- chain = LLMChain(llm=self.llm, prompt=prompt)
130
- return chain.run(info=info)
131
-
132
- def generate_email(self, template_name: str, context: Dict) -> str:
133
- if not self.current_profile:
134
- return "Please set up your profile first."
135
-
136
- template = self.email_template.get_template(template_name)
137
- structured_info = self.structure_info(context.get('recipient_info', ''))
138
 
139
- email_prompt = PromptTemplate(
140
  input_variables=["template", "sender_name", "sender_industry", "sender_background", "recipient", "subject", "structured_info"],
141
  template="""
142
  Using the following template and information, create a personalized email:
@@ -148,9 +146,26 @@ class EmailGenie:
148
  Recipient Information: {structured_info}
149
  """
150
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
- chain = LLMChain(llm=self.llm, prompt=email_prompt)
153
- return chain.run(
 
 
154
  template=template,
155
  sender_name=self.current_profile['name'],
156
  sender_industry=self.current_profile['industry'],
@@ -159,6 +174,8 @@ class EmailGenie:
159
  subject=context['email_subject'],
160
  structured_info=structured_info
161
  )
 
 
162
 
163
  def preview_email(self, email_content: str) -> str:
164
  if not self.current_profile:
 
1
  import os
2
  import gradio as gr
3
+ from groq import Groq
 
 
4
  import pandas as pd
5
  from datetime import datetime
6
  from sendgrid import SendGridAPIClient
 
8
  import json
9
  from pathlib import Path
10
  from typing import Dict, List, Optional
11
+ from langchain.chains import LLMChain
12
+ from langchain.prompts import PromptTemplate
13
+ from langchain.llms import Groq as LangchainGroq
14
 
15
  # Define the storage directory in the Hugging Face Space
16
  STORAGE_DIR = os.environ.get('STORAGE_DIR', 'data')
 
49
  with open(self.storage_path, 'r') as f:
50
  data = json.load(f)
51
 
 
52
  profiles = data.get("profiles", [])
53
  profile_exists = False
54
  for i, profile in enumerate(profiles):
 
110
 
111
  class EmailGenie:
112
  def __init__(self):
113
+ groq_api_key = os.environ.get("GROQ_API_KEY")
114
+ self.client = Groq(api_key=groq_api_key)
115
  self.sendgrid_client = initialize_email_client()
116
  self.sender_email = os.environ.get("SENDER_EMAIL")
117
  self.user_profile = UserProfile()
118
  self.email_template = EmailTemplate()
119
  self.current_profile = None
120
+
121
+ # Initialize LangChain components
122
+ self.llm = LangchainGroq(
123
+ groq_api_key=groq_api_key,
124
+ model_name="llama-3.1-8b-instant"
125
+ )
126
+
127
+ # Create LLMChains with structured prompts
128
+ self.structure_info_prompt = PromptTemplate(
129
  input_variables=["info"],
130
  template="Please structure and summarize the following information about a person or company: {info}"
131
  )
132
+ self.structure_info_chain = LLMChain(
133
+ llm=self.llm,
134
+ prompt=self.structure_info_prompt
135
+ )
 
 
 
 
 
136
 
137
+ self.email_generation_prompt = PromptTemplate(
138
  input_variables=["template", "sender_name", "sender_industry", "sender_background", "recipient", "subject", "structured_info"],
139
  template="""
140
  Using the following template and information, create a personalized email:
 
146
  Recipient Information: {structured_info}
147
  """
148
  )
149
+ self.email_generation_chain = LLMChain(
150
+ llm=self.llm,
151
+ prompt=self.email_generation_prompt
152
+ )
153
+
154
+ def set_current_profile(self, name: str):
155
+ self.current_profile = self.user_profile.get_profile(name)
156
+ return self.current_profile is not None
157
+
158
+ def structure_info(self, info: str) -> str:
159
+ return self.structure_info_chain.run(info=info)
160
+
161
+ def generate_email(self, template_name: str, context: Dict) -> str:
162
+ if not self.current_profile:
163
+ return "Please set up your profile first."
164
 
165
+ template = self.email_template.get_template(template_name)
166
+ structured_info = self.structure_info(context.get('recipient_info', ''))
167
+
168
+ email_content = self.email_generation_chain.run(
169
  template=template,
170
  sender_name=self.current_profile['name'],
171
  sender_industry=self.current_profile['industry'],
 
174
  subject=context['email_subject'],
175
  structured_info=structured_info
176
  )
177
+
178
+ return email_content
179
 
180
  def preview_email(self, email_content: str) -> str:
181
  if not self.current_profile: