-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopywriting_app.py
163 lines (134 loc) · 5.43 KB
/
copywriting_app.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import time
import os
import json
import google.generativeai as genai
import streamlit as st
from tenacity import retry, stop_after_attempt, wait_random_exponential
def main():
set_page_config()
custom_css()
hide_elements()
title_and_description()
input_section()
def set_page_config():
st.set_page_config(
page_title="Alwrity Copywriting",
layout="wide",
)
def custom_css():
st.markdown("""
<style>
.block-container {
padding-top: 0rem;
padding-bottom: 0rem;
padding-left: 1rem;
padding-right: 1rem;
}
</style>
""", unsafe_allow_html=True)
st.markdown("""
<style>
[class="st-emotion-cache-7ym5gk ef3psqc12"] {
display: inline-block;
padding: 5px 20px;
background-color: #4681f4;
color: #FBFFFF;
width: 300px;
height: 35px;
text-align: center;
text-decoration: none;
font-size: 16px;
border-radius: 8px;
}
</style>
""", unsafe_allow_html=True)
def hide_elements():
hide_decoration_bar_style = '<style>header {visibility: hidden;}</style>'
st.markdown(hide_decoration_bar_style, unsafe_allow_html=True)
hide_streamlit_footer = '<style>#MainMenu {visibility: hidden;} footer {visibility: hidden;}</style>'
st.markdown(hide_streamlit_footer, unsafe_allow_html=True)
def title_and_description():
st.title("🧕 Alwrity - AI Generator for CopyWriting APP Formula")
def input_section():
with st.expander("**PRO-TIP** - Easy Steps to Create Compelling Copy", expanded=True):
col1, space, col2 = st.columns([5, 0.1, 5])
with col1:
brand_name = st.text_input('**Enter Brand/Company Name**', help="Enter the name of your brand or company.")
with col2:
description = st.text_input('**Describe What Your Brand Does ?** (In 2-3 words)', help="Describe your product or service briefly.")
agree = st.text_input('Establish Common Problem that Your company solves',
help="Connect with the audience by acknowledging a shared problem.",
placeholder="We all face..., Like you, I've..., Safety, Unprofessionalism..")
if st.button('**Get APP Copy**'):
if agree.strip() and brand_name and description:
with st.spinner("Generating APP Copy..."):
app_copy = generate_app_copy(brand_name, description, agree)
if app_copy:
st.subheader('**👩🔬👩🔬 Your APP Copy**')
st.markdown(app_copy)
else:
st.error("💥 **Failed to generate APP copy. Please try again!**")
else:
st.error("All fields are required!")
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def generate_app_copy(brand_name, description, agree):
prompt = f"""As an expert copywriter, I need your help in creating a marketing campaign for {brand_name},
which is a {description}. Your task is to use the APP (Agree-Promise-Preview) formula to craft compelling copy.
Establish Common Problem that Your company solves and acknowledging a shared problem of {agree}.
Make a Compelling Promise or Offer a Solution.
Provide a compelling Preview of servie.
Do not provide explanations, provide the final marketing copy.
"""
try:
response = generate_text_with_exception_handling(prompt)
return response
except Exception as err:
st.error(f"Exit: Failed to get response from LLM: {err}")
exit(1)
@retry(wait=wait_random_exponential(min=1, max=60), stop=stop_after_attempt(6))
def generate_text_with_exception_handling(prompt):
"""
Generates text using the Gemini model with exception handling.
Args:
api_key (str): Your Google Generative AI API key.
prompt (str): The prompt for text generation.
Returns:
str: The generated text.
"""
try:
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 0,
"max_output_tokens": 8192,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
]
model = genai.GenerativeModel(model_name="gemini-1.5-pro-latest",
generation_config=generation_config,
safety_settings=safety_settings)
convo = model.start_chat(history=[])
convo.send_message(prompt)
return convo.last.text
except Exception as e:
st.exception(f"An unexpected error occurred: {e}")
return None
if __name__ == "__main__":
main()