adriiita commited on
Commit
a331e73
·
verified ·
1 Parent(s): f2179a7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -1,8 +1,8 @@
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
7
  from sendgrid.helpers.mail import Mail
8
  import json
@@ -37,7 +37,7 @@ class UserProfile:
37
  self._ensure_storage_exists()
38
 
39
  def _ensure_storage_exists(self):
40
- if not os.path.exists(self.storage_path):
41
  with open(self.storage_path, 'w') as f:
42
  json.dump({"profiles": []}, f)
43
 
@@ -108,7 +108,7 @@ class EmailTemplate:
108
 
109
  class EmailGenie:
110
  def __init__(self):
111
- self.client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
112
  self.sendgrid_client = initialize_email_client()
113
  self.sender_email = os.environ.get("SENDER_EMAIL")
114
  self.user_profile = UserProfile()
@@ -120,13 +120,13 @@ class EmailGenie:
120
  return self.current_profile is not None
121
 
122
  def structure_info(self, info: str) -> str:
123
- prompt = f"Please structure and summarize the following information about a person or company: {info}"
124
- chat_completion = self.client.chat.completions.create(
125
- messages=[{"role": "user", "content": prompt}],
126
- model="llama-3.1-8b-instant",
127
  )
128
- return chat_completion.choices[0].message.content
129
-
 
130
  def generate_email(self, template_name: str, context: Dict) -> str:
131
  if not self.current_profile:
132
  return "Please set up your profile first."
@@ -134,22 +134,30 @@ class EmailGenie:
134
  template = self.email_template.get_template(template_name)
135
  structured_info = self.structure_info(context.get('recipient_info', ''))
136
 
137
- prompt = f"""
138
- Using the following template and information, create a personalized email:
139
- Template: {template}
140
- Sender: {self.current_profile['name']} ({self.current_profile['industry']})
141
- Sender Background: {self.current_profile['background']}
142
- Recipient: {context['recipient']}
143
- Subject: {context['email_subject']}
144
- Recipient Information: {structured_info}
145
- """
 
 
 
146
 
147
- chat_completion = self.client.chat.completions.create(
148
- messages=[{"role": "user", "content": prompt}],
149
- model="llama-3.1-8b-instant",
 
 
 
 
 
 
150
  )
151
- return chat_completion.choices[0].message.content
152
-
153
  def preview_email(self, email_content: str) -> str:
154
  if not self.current_profile:
155
  return "Please set up your profile first."
 
1
  import os
2
  import gradio as gr
3
+ from langchain.llms import Groq
4
+ from langchain.prompts import PromptTemplate
5
+ from langchain.chains import LLMChain
6
  from sendgrid import SendGridAPIClient
7
  from sendgrid.helpers.mail import Mail
8
  import json
 
37
  self._ensure_storage_exists()
38
 
39
  def _ensure_storage_exists(self):
40
+ if not os.path.exists(self.storage_path):
41
  with open(self.storage_path, 'w') as f:
42
  json.dump({"profiles": []}, f)
43
 
 
108
 
109
  class EmailGenie:
110
  def __init__(self):
111
+ self.llm = Groq(api_key=os.environ.get("GROQ_API_KEY"))
112
  self.sendgrid_client = initialize_email_client()
113
  self.sender_email = os.environ.get("SENDER_EMAIL")
114
  self.user_profile = UserProfile()
 
120
  return self.current_profile is not None
121
 
122
  def structure_info(self, info: str) -> str:
123
+ structure_prompt = PromptTemplate(
124
+ input_variables=["info"],
125
+ template="Please structure and summarize the following information about a person or company: {info}"
 
126
  )
127
+ structure_chain = LLMChain(llm=self.llm, prompt=structure_prompt)
128
+ return structure_chain.run(info)
129
+
130
  def generate_email(self, template_name: str, context: Dict) -> str:
131
  if not self.current_profile:
132
  return "Please set up your profile first."
 
134
  template = self.email_template.get_template(template_name)
135
  structured_info = self.structure_info(context.get('recipient_info', ''))
136
 
137
+ email_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:
141
+ Template: {template}
142
+ Sender: {sender_name} ({sender_industry})
143
+ Sender Background: {sender_background}
144
+ Recipient: {recipient}
145
+ Subject: {subject}
146
+ Recipient Information: {structured_info}
147
+ """
148
+ )
149
 
150
+ email_chain = LLMChain(llm=self.llm, prompt=email_prompt)
151
+ return email_chain.run(
152
+ template=template,
153
+ sender_name=self.current_profile['name'],
154
+ sender_industry=self.current_profile['industry'],
155
+ sender_background=self.current_profile['background'],
156
+ recipient=context['recipient'],
157
+ subject=context['email_subject'],
158
+ structured_info=structured_info
159
  )
160
+
 
161
  def preview_email(self, email_content: str) -> str:
162
  if not self.current_profile:
163
  return "Please set up your profile first."