Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
import os
|
2 |
import gradio as gr
|
3 |
-
from
|
4 |
-
|
5 |
-
from
|
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.
|
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 |
-
|
124 |
-
|
125 |
-
|
126 |
-
model="llama-3.1-8b-instant",
|
127 |
)
|
128 |
-
|
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 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
|
|
|
|
|
|
146 |
|
147 |
-
|
148 |
-
|
149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
)
|
151 |
-
|
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."
|