-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglobalize-text-streamlit.py
109 lines (82 loc) · 4.43 KB
/
globalize-text-streamlit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# https://github.com/gkamradt/globalize-text-streamlit
import streamlit as st
from langchain.chat_models import ChatOpenAI
from langchain import LLMChain
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate
)
template = """
Below is an email that may be poorly worded.
Your goal is to:
- Properly format the email
- Convert the input text to a specified tone
- Convert the input text to a specified dialect
Here are some examples different Tones:
- Formal: We went to Barcelona for the weekend. We have a lot of things to tell you.
- Informal: Went to Barcelona for the weekend. Lots to tell you.
Here are some examples of words in different dialects:
- American: French Fries, cotton candy, apartment, garbage, cookie, green thumb, parking lot, pants, windshield
- British: chips, candyfloss, flag, rubbish, biscuit, green fingers, car park, trousers, windscreen
Example Sentences from each dialect:
- American: I headed straight for the produce section to grab some fresh vegetables, like bell peppers and zucchini. After that, I made my way to the meat department to pick up some chicken breasts.
- British: Well, I popped down to the local shop just the other day to pick up a few bits and bobs. As I was perusing the aisles, I noticed that they were fresh out of biscuits, which was a bit of a disappointment, as I do love a good cuppa with a biscuit or two.
Please start the email with a warm introduction. Add the introduction if you need to.
Below is the email, tone, and dialect:
TONE: {tone}
DIALECT: {dialect}
EMAIL: {email}
YOUR {dialect} RESPONSE:
"""
prompt = ChatPromptTemplate.from_messages([
HumanMessagePromptTemplate.from_template(template)
])
def load_LLM(openai_api_key):
"""Logic for loading the chain you want to use should go here."""
# Make sure your openai_api_key is set as an environment variable
llm = ChatOpenAI(temperature=.7, openai_api_key=openai_api_key, verbose=True)
return llm
st.set_page_config(page_title="Globalize Email", page_icon=":robot:")
st.header("Globalize Text")
col1, col2 = st.columns(2)
with col1:
st.markdown("Often professionals would like to improve their emails, but don't have the skills to do so. \n\n This tool \
will help you improve your email skills by converting your emails into a more professional format. This tool \
is powered by [LangChain](https://langchain.com/) and [OpenAI](https://openai.com) and made by \
[@GregKamradt](https://twitter.com/GregKamradt). \n\n View Source Code on [Github](https://github.com/gkamradt/globalize-text-streamlit/blob/main/main.py)")
with col2:
st.image(image='TweetScreenshot.png', width=500, caption='https://twitter.com/DannyRichman/status/1598254671591723008')
st.markdown("## Enter Your Email To Convert")
def get_api_key():
input_text = st.text_input(label="OpenAI API Key ", type="password", placeholder="Ex: sk-2twm...", key="openai_api_key_input")
return input_text
openai_api_key = get_api_key()
col1, col2 = st.columns(2)
with col1:
option_tone = st.selectbox(
'Which tone would you like your email to have?',
('Formal', 'Informal'))
with col2:
option_dialect = st.selectbox(
'Which English Dialect would you like?',
('American', 'British'))
def get_text():
input_text = st.text_area(label="Email Input", label_visibility='collapsed', placeholder="Your Email...", key="email_input")
return input_text
email_input = get_text()
if len(email_input.split(" ")) > 700:
st.write("Please enter a shorter email. The maximum length is 700 words.")
st.stop()
def update_text_with_example():
print ("in updated")
st.session_state.email_input = "Sally I am starts work at yours monday from dave"
st.button("*See An Example*", type='secondary', help="Click to see an example of the email you will be converting.", on_click=update_text_with_example)
st.markdown("### Your Converted Email:")
if email_input:
if not openai_api_key:
st.warning('Please insert OpenAI API Key. Instructions [here](https://platform.openai.com/account/api-keys)', icon="⚠️")
st.stop()
llm = load_LLM(openai_api_key=openai_api_key)
llm_chain = LLMChain(prompt=prompt, llm=llm, verbose=True)
formatted_email = llm_chain.predict(tone=option_tone, dialect=option_dialect, email=email_input)
st.write(formatted_email)