Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from langchain_openai import OpenAI
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain_community.llms import CTransformers
|
5 |
+
import os
|
6 |
+
import logging
|
7 |
+
|
8 |
+
os.environ["OPENAI_API_KEY"] = "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"
|
9 |
+
if "OPENAI_API_KEY" not in st.session_state:
|
10 |
+
st.session_state["OPENAI_API_KEY"] = ''
|
11 |
+
|
12 |
+
|
13 |
+
def getLLMResposne(form_input, email_sender, email_recipient, email_style):
|
14 |
+
try:
|
15 |
+
# llm = CTransformers(
|
16 |
+
# model = "models/llama-2-7b-chat.ggmlv3.q2_K.bin",
|
17 |
+
# model_type = 'llama',
|
18 |
+
# config = {'max_new_tokens':256,
|
19 |
+
# 'temperature':.01}
|
20 |
+
# )
|
21 |
+
|
22 |
+
llm = OpenAI(temperature=.9)
|
23 |
+
|
24 |
+
template = """
|
25 |
+
write an email with {style} style and includes topic: {email_topic}\n\nSender: {sender}\n\nRecipient: {recipient}
|
26 |
+
keep the email short and crisp in 150-200 words.
|
27 |
+
\n\nEmail Text:
|
28 |
+
|
29 |
+
"""
|
30 |
+
|
31 |
+
prompt = PromptTemplate(
|
32 |
+
input_variables=["style", "email_topic", "sender", "recipient"],
|
33 |
+
template=template,
|
34 |
+
)
|
35 |
+
|
36 |
+
response = llm(prompt.format(email_topic=form_input,
|
37 |
+
sender=email_sender,
|
38 |
+
recipient=email_recipient,
|
39 |
+
style=email_style))
|
40 |
+
|
41 |
+
return response
|
42 |
+
except Exception as e:
|
43 |
+
return f"An error occurred: {str(e)}"
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
def main():
|
48 |
+
st.set_page_config(
|
49 |
+
page_title = "Generate Emails",
|
50 |
+
page_icon = "📨",
|
51 |
+
layout = "centered",
|
52 |
+
initial_sidebar_state = "collapsed"
|
53 |
+
)
|
54 |
+
|
55 |
+
st.header("Generate Emails 📨")
|
56 |
+
|
57 |
+
open_api_key = st.sidebar.text_input("Enter OpenAI API Key")
|
58 |
+
|
59 |
+
os.environ["OPENAI_API_KEY"] = open_api_key
|
60 |
+
|
61 |
+
|
62 |
+
form_input = st.text_area("Enter the email topic", height = 275)
|
63 |
+
|
64 |
+
col1, col2, col3 = st.columns([10,10,5])
|
65 |
+
with col1:
|
66 |
+
email_sender = st.text_input("Sender name")
|
67 |
+
with col2:
|
68 |
+
email_recipient=st.text_input("Recipient name")
|
69 |
+
with col3:
|
70 |
+
email_style = st.selectbox(
|
71 |
+
'Writing style', ('Formal','Appreciating','Not Satisfied','Neutral'),
|
72 |
+
index=0
|
73 |
+
)
|
74 |
+
|
75 |
+
|
76 |
+
submit = st.button("Generate")
|
77 |
+
|
78 |
+
if submit:
|
79 |
+
with st.spinner():
|
80 |
+
st.write(getLLMResposne(form_input, email_sender, email_recipient, email_style))
|
81 |
+
|
82 |
+
if __name__=='__main__':
|
83 |
+
main()
|