Spaces:
Sleeping
Sleeping
pr19
#19
by Mia2024 - opened
app.py
CHANGED
|
@@ -1,14 +1,6 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
|
| 4 |
-
# def submit_callback():
|
| 5 |
-
# st.session_state.clicked = False
|
| 6 |
-
# handle the user_input here
|
| 7 |
-
# st.write("Product: " + st.session_state.product)
|
| 8 |
-
# st.write("Gender: " + st.session_state.gender)
|
| 9 |
-
# st.write("Profession: " + st.session_state.profession)
|
| 10 |
-
# st.write("Hobby: "+ st.session_state.hobby)
|
| 11 |
-
|
| 12 |
|
| 13 |
col1, col2=st.columns(2)
|
| 14 |
with col1:
|
|
@@ -18,18 +10,24 @@ with col1:
|
|
| 18 |
# product
|
| 19 |
product=st.text_input("product", key="product")
|
| 20 |
# gender
|
| 21 |
-
gender=st.radio("gender", ["male", "female", "
|
| 22 |
# profession
|
| 23 |
profession=st.text_input("profession", key="profession")
|
| 24 |
# hobby
|
| 25 |
hobby=st.text_input("hobby", key="hobby")
|
| 26 |
# Every form must have a submit button.
|
| 27 |
-
# submitted = st.form_submit_button(label='Submit', on_click=submit_callback)
|
| 28 |
submitted = st.form_submit_button(label='Submit')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
with col2:
|
| 30 |
if submitted:
|
| 31 |
with st.spinner('Wait for it...'):
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
with st.container(height=500):
|
| 34 |
placeholder = st.empty()
|
| 35 |
# Replace the chart with several elements:
|
|
@@ -38,6 +36,7 @@ with col2:
|
|
| 38 |
st.write("Gender: " + st.session_state.gender)
|
| 39 |
st.write("Profession: " + st.session_state.profession)
|
| 40 |
st.write("Hobby: "+ st.session_state.hobby)
|
|
|
|
| 41 |
|
| 42 |
if 'clicked' not in st.session_state:
|
| 43 |
st.session_state.clicked = False
|
|
@@ -53,317 +52,7 @@ if st.session_state.clicked:
|
|
| 53 |
del st.session_state.gender
|
| 54 |
del st.session_state.profession
|
| 55 |
del st.session_state.hobby
|
|
|
|
| 56 |
del st.session_state.clicked
|
| 57 |
if submitted:
|
| 58 |
placeholder.empty()
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# Clear all those elements:
|
| 63 |
-
# placeholder.empty()
|
| 64 |
-
|
| 65 |
-
# if clear:
|
| 66 |
-
# del st.session_state.product
|
| 67 |
-
# del st.session_state.gender
|
| 68 |
-
# del st.session_state.profession
|
| 69 |
-
# del st.session_state.hobby
|
| 70 |
-
# placeholder.empty()
|
| 71 |
-
|
| 72 |
-
# container1 = st.container(border=True)
|
| 73 |
-
|
| 74 |
-
# col1, col2 = st.columns(2)
|
| 75 |
-
# with col1:
|
| 76 |
-
# container1 = col1.container(height=3000, )
|
| 77 |
-
|
| 78 |
-
# with col2:
|
| 79 |
-
# container2 = col2.container(height=300)
|
| 80 |
-
|
| 81 |
-
# placeholder = st.empty()
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
# placeholder = st.empty()
|
| 85 |
-
# # Create two columns
|
| 86 |
-
# col1, col2 = st.columns(2)
|
| 87 |
-
|
| 88 |
-
# # Place a form in each column
|
| 89 |
-
# with col1:
|
| 90 |
-
# def submit_callback():
|
| 91 |
-
# st.write("Product: " + st.session_state.product)
|
| 92 |
-
# st.write("Gender: " + st.session_state.gender)
|
| 93 |
-
# st.write("Profession: " + st.session_state.profession)
|
| 94 |
-
# st.write("Hobby: "+ st.session_state.hobby)
|
| 95 |
-
|
| 96 |
-
# def delete_callback():
|
| 97 |
-
# del st.session_state.product
|
| 98 |
-
# del st.session_state.gender
|
| 99 |
-
# del st.session_state.profession
|
| 100 |
-
# del st.session_state.hobby
|
| 101 |
-
# placeholder.empty()
|
| 102 |
-
|
| 103 |
-
# import streamlit as st
|
| 104 |
-
# import time
|
| 105 |
-
|
| 106 |
-
# class SessionState:
|
| 107 |
-
# def __init__(self):
|
| 108 |
-
# self.product = ""
|
| 109 |
-
# self.gender = ""
|
| 110 |
-
# self.profession = ""
|
| 111 |
-
# self.hobby = ""
|
| 112 |
-
|
| 113 |
-
# state = SessionState()
|
| 114 |
-
|
| 115 |
-
# def submit_callback():
|
| 116 |
-
# with output_placeholder.container():
|
| 117 |
-
# st.write("Product: " + state.product)
|
| 118 |
-
# st.write("Gender: " + state.gender)
|
| 119 |
-
# st.write("Profession: " + state.profession)
|
| 120 |
-
# st.write("Hobby: "+ state.hobby)
|
| 121 |
-
|
| 122 |
-
# def delete_callback():
|
| 123 |
-
# state.product = ""
|
| 124 |
-
# state.gender = ""
|
| 125 |
-
# state.profession = ""
|
| 126 |
-
# state.hobby = ""
|
| 127 |
-
# output_placeholder.empty()
|
| 128 |
-
|
| 129 |
-
# form_placeholder = st.empty()
|
| 130 |
-
# output_placeholder = st.empty()
|
| 131 |
-
|
| 132 |
-
# submitted = False
|
| 133 |
-
# clear = False
|
| 134 |
-
|
| 135 |
-
# with form_placeholder.form("my_form", clear_on_submit=True):
|
| 136 |
-
# st.write("Input")
|
| 137 |
-
# # product
|
| 138 |
-
# state.product = st.text_input("product", value=state.product, key="product")
|
| 139 |
-
# # gender
|
| 140 |
-
# state.gender = st.radio("gender", ["male", "female"], value=state.gender, key="gender")
|
| 141 |
-
# # profession
|
| 142 |
-
# state.profession = st.text_input("profession", value=state.profession, key="profession")
|
| 143 |
-
# # hobby
|
| 144 |
-
# state.hobby = st.text_input("hobby", value=state.hobby, key="hobby")
|
| 145 |
-
# # Every form must have a submit button.
|
| 146 |
-
# btn1, btn2=st.columns(2)
|
| 147 |
-
# with btn1:
|
| 148 |
-
# submitted = st.form_submit_button(label='Submit')
|
| 149 |
-
# with btn2:
|
| 150 |
-
# clear = st.form_submit_button(label='Clear')
|
| 151 |
-
|
| 152 |
-
# if clear:
|
| 153 |
-
# delete_callback()
|
| 154 |
-
# if submitted:
|
| 155 |
-
# submit_callback()
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
# def submit_callback():
|
| 159 |
-
# with output_placeholder.container():
|
| 160 |
-
# st.write("Product: " + st.session_state.product)
|
| 161 |
-
# st.write("Gender: " + st.session_state.gender)
|
| 162 |
-
# st.write("Profession: " + st.session_state.profession)
|
| 163 |
-
# st.write("Hobby: "+ st.session_state.hobby)
|
| 164 |
-
|
| 165 |
-
# def delete_callback():
|
| 166 |
-
# del st.session_state.product
|
| 167 |
-
# del st.session_state.gender
|
| 168 |
-
# del st.session_state.profession
|
| 169 |
-
# del st.session_state.hobby
|
| 170 |
-
# output_placeholder.empty()
|
| 171 |
-
|
| 172 |
-
# form_placeholder = st.empty()
|
| 173 |
-
# output_placeholder = st.empty()
|
| 174 |
-
|
| 175 |
-
# with form_placeholder.form("my_input", clear_on_submit=True):
|
| 176 |
-
# st.write("Input")
|
| 177 |
-
# # product
|
| 178 |
-
# product=st.text_input("product", key="product")
|
| 179 |
-
# # gender
|
| 180 |
-
# gender=st.radio("gender", ["male", "female"], key="gender")
|
| 181 |
-
# # profession
|
| 182 |
-
# profession=st.text_input("profession", key="profession")
|
| 183 |
-
# # hobby
|
| 184 |
-
# hobby=st.text_input("hobby", key="hobby")
|
| 185 |
-
# # Every form must have a submit button.
|
| 186 |
-
# btn1, btn2=st.columns(2)
|
| 187 |
-
# with btn1:
|
| 188 |
-
# submitted = st.form_submit_button(label='Submit', on_click=submit_callback)
|
| 189 |
-
# # submitted = st.form_submit_button(label='Submit')
|
| 190 |
-
# with btn2:
|
| 191 |
-
# clear = st.form_submit_button(label='Clear', on_click=delete_callback)
|
| 192 |
-
# if submitted:
|
| 193 |
-
# # handle the user_input here
|
| 194 |
-
# st.write("Product: " + st.session_state.product)
|
| 195 |
-
# st.write("Gender: " + st.session_state.gender)
|
| 196 |
-
# st.write("Profession: " + st.session_state.profession)
|
| 197 |
-
# st.write("Hobby: "+ st.session_state.hobby)
|
| 198 |
-
|
| 199 |
-
# if clear:
|
| 200 |
-
# del st.session_state.product
|
| 201 |
-
# del st.session_state.gender
|
| 202 |
-
# del st.session_state.profession
|
| 203 |
-
# del st.session_state.hobby
|
| 204 |
-
# placeholder.empty()
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
# # with col2:
|
| 210 |
-
# _LOREM_IPSUM = (
|
| 211 |
-
# "product" + str(st.session_state.product) + "\n"
|
| 212 |
-
# + "gender" + str(st.session_state.gender) + "\n"
|
| 213 |
-
# + "profession" + str(st.session_state.profession) + "\n"
|
| 214 |
-
# + "hobby" + str(st.session_state.hobby) +"\n"
|
| 215 |
-
# )
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
# def stream_data():
|
| 219 |
-
# for word in _LOREM_IPSUM.split(" "):
|
| 220 |
-
# yield word + " "
|
| 221 |
-
# time.sleep(0.02)
|
| 222 |
-
|
| 223 |
-
# if st.button("Stream data"):
|
| 224 |
-
# st.write_stream(stream_data)
|
| 225 |
-
# placeholder = st.empty()
|
| 226 |
-
|
| 227 |
-
# with placeholder.container():
|
| 228 |
-
# st.write("Hello")
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
# import streamlit as st
|
| 232 |
-
|
| 233 |
-
# class SessionState:
|
| 234 |
-
# def __init__(self):
|
| 235 |
-
# self.input_text = ""
|
| 236 |
-
|
| 237 |
-
# state = SessionState()
|
| 238 |
-
|
| 239 |
-
# with st.form("my_form"):
|
| 240 |
-
# state.input_text = st.text_input("Enter something:", value=state.input_text)
|
| 241 |
-
# clear_button = st.form_submit_button("Clear")
|
| 242 |
-
# submit_button = st.form_submit_button("Submit")
|
| 243 |
-
|
| 244 |
-
# if clear_button:
|
| 245 |
-
# state.input_text = ""
|
| 246 |
-
# if submit_button:
|
| 247 |
-
# # handle the user_input here
|
| 248 |
-
# st.write(f"You submitted: {state.input_text}")
|
| 249 |
-
|
| 250 |
-
# import streamlit as st
|
| 251 |
-
# import time
|
| 252 |
-
|
| 253 |
-
# class SessionState:
|
| 254 |
-
# def __init__(self):
|
| 255 |
-
# self.product = ""
|
| 256 |
-
# self.gender = ""
|
| 257 |
-
# self.profession = ""
|
| 258 |
-
# self.hobby = ""
|
| 259 |
-
|
| 260 |
-
# state = SessionState()
|
| 261 |
-
|
| 262 |
-
# def submit_callback():
|
| 263 |
-
# output_placeholder.write("Product: " + state.product)
|
| 264 |
-
# output_placeholder.write("Gender: " + state.gender)
|
| 265 |
-
# output_placeholder.write("Profession: " + state.profession)
|
| 266 |
-
# output_placeholder.write("Hobby: "+ state.hobby)
|
| 267 |
-
|
| 268 |
-
# def delete_callback():
|
| 269 |
-
# state.product = ""
|
| 270 |
-
# state.gender = ""
|
| 271 |
-
# state.profession = ""
|
| 272 |
-
# state.hobby = ""
|
| 273 |
-
# output_placeholder.empty()
|
| 274 |
-
|
| 275 |
-
# form_placeholder = st.empty()
|
| 276 |
-
# output_placeholder = st.empty()
|
| 277 |
-
|
| 278 |
-
# with form_placeholder.form("my_form"):
|
| 279 |
-
# st.write("Input")
|
| 280 |
-
# # product
|
| 281 |
-
# state.product = st.text_input("product", value=state.product, key="product")
|
| 282 |
-
# # gender
|
| 283 |
-
# state.gender = st.radio("gender", ["male", "female"], value=state.gender, key="gender")
|
| 284 |
-
# # profession
|
| 285 |
-
# state.profession = st.text_input("profession", value=state.profession, key="profession")
|
| 286 |
-
# # hobby
|
| 287 |
-
# state.hobby = st.text_input("hobby", value=state.hobby, key="hobby")
|
| 288 |
-
# # Every form must have a submit button.
|
| 289 |
-
# btn1, btn2=st.columns(2)
|
| 290 |
-
# with btn1:
|
| 291 |
-
# submitted = st.form_submit_button(label='Submit')
|
| 292 |
-
# with btn2:
|
| 293 |
-
# clear = st.form_submit_button(label='Clear')
|
| 294 |
-
|
| 295 |
-
# if clear:
|
| 296 |
-
# delete_callback()
|
| 297 |
-
# if submitted:
|
| 298 |
-
# # handle the user_input here
|
| 299 |
-
# submit_callback()
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
# _LOREM_IPSUM = """
|
| 304 |
-
# Lorem ipsum dolor sit amet, **consectetur adipiscing** elit, sed do eiusmod tempor
|
| 305 |
-
# incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
|
| 306 |
-
# nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
|
| 307 |
-
# """
|
| 308 |
-
|
| 309 |
-
# def stream_data():
|
| 310 |
-
# for word in _LOREM_IPSUM.split(" "):
|
| 311 |
-
# yield word + " "
|
| 312 |
-
# time.sleep(0.02)
|
| 313 |
-
|
| 314 |
-
# for word in _LOREM_IPSUM.split(" "):
|
| 315 |
-
# yield word + " "
|
| 316 |
-
# time.sleep(0.02)
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
# if st.button("Stream data"):
|
| 320 |
-
# st.write_stream(stream_data)
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
# import streamlit as st
|
| 324 |
-
# import SessionState
|
| 325 |
-
# from streamlit.server.server import Server
|
| 326 |
-
# import streamlit.report_thread as ReportThread
|
| 327 |
-
|
| 328 |
-
# # Initialize session state
|
| 329 |
-
# session_state = SessionState.get(product="", gender="", profession="", hobby="")
|
| 330 |
-
|
| 331 |
-
# # Create a form for user inputs
|
| 332 |
-
# with st.form("my_input"):
|
| 333 |
-
# st.write("Input")
|
| 334 |
-
# # product
|
| 335 |
-
# product = st.text_input("product", value=session_state.product)
|
| 336 |
-
# # gender
|
| 337 |
-
# gender = st.radio("gender", ["male", "female"], index=["male", "female"].index(session_state.gender) if session_state.gender else 0)
|
| 338 |
-
# # profession
|
| 339 |
-
# profession = st.text_input("profession", value=session_state.profession)
|
| 340 |
-
# # hobby
|
| 341 |
-
# hobby = st.text_input("hobby", value=session_state.hobby)
|
| 342 |
-
|
| 343 |
-
# # Every form must have a submit button.
|
| 344 |
-
# submitted = st.form_submit_button("Submit")
|
| 345 |
-
# clear = st.form_submit_button("Clear")
|
| 346 |
-
|
| 347 |
-
# # Display the user inputs
|
| 348 |
-
# with st.form("my_output"):
|
| 349 |
-
# if submitted and not clear:
|
| 350 |
-
# # Save inputs to session state
|
| 351 |
-
# session_state.product = product
|
| 352 |
-
# session_state.gender = gender
|
| 353 |
-
# session_state.profession = profession
|
| 354 |
-
# session_state.hobby = hobby
|
| 355 |
-
|
| 356 |
-
# st.write("product", product)
|
| 357 |
-
# st.write("gender", gender)
|
| 358 |
-
# st.write("profession", profession)
|
| 359 |
-
# st.write("hobby", hobby)
|
| 360 |
-
|
| 361 |
-
# # Clear the user inputs
|
| 362 |
-
# if clear:
|
| 363 |
-
# # Clear session state
|
| 364 |
-
# session_state.product = ""
|
| 365 |
-
# session_state.gender = ""
|
| 366 |
-
# session_state.profession = ""
|
| 367 |
-
# session_state.hobby = ""
|
| 368 |
-
|
| 369 |
-
# st.experimental_rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from utils import generate_email
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
col1, col2=st.columns(2)
|
| 6 |
with col1:
|
|
|
|
| 10 |
# product
|
| 11 |
product=st.text_input("product", key="product")
|
| 12 |
# gender
|
| 13 |
+
gender=st.radio("gender", ["male", "female", "non-binary"], key="gender")
|
| 14 |
# profession
|
| 15 |
profession=st.text_input("profession", key="profession")
|
| 16 |
# hobby
|
| 17 |
hobby=st.text_input("hobby", key="hobby")
|
| 18 |
# Every form must have a submit button.
|
|
|
|
| 19 |
submitted = st.form_submit_button(label='Submit')
|
| 20 |
+
# if submitted:
|
| 21 |
+
# response = generate_email(st.session_state.product, st.session_state.gender, st.session_state.profession, st.session_state.hobby)
|
| 22 |
+
# st.session_state.email=response
|
| 23 |
+
|
| 24 |
with col2:
|
| 25 |
if submitted:
|
| 26 |
with st.spinner('Wait for it...'):
|
| 27 |
+
response = generate_email(st.session_state.product, st.session_state.gender, st.session_state.profession, st.session_state.hobby)
|
| 28 |
+
st.session_state.email=response
|
| 29 |
+
print("st.session_state.email")
|
| 30 |
+
print(st.session_state.email)
|
| 31 |
with st.container(height=500):
|
| 32 |
placeholder = st.empty()
|
| 33 |
# Replace the chart with several elements:
|
|
|
|
| 36 |
st.write("Gender: " + st.session_state.gender)
|
| 37 |
st.write("Profession: " + st.session_state.profession)
|
| 38 |
st.write("Hobby: "+ st.session_state.hobby)
|
| 39 |
+
st.write("Email: "+ st.session_state.email)
|
| 40 |
|
| 41 |
if 'clicked' not in st.session_state:
|
| 42 |
st.session_state.clicked = False
|
|
|
|
| 52 |
del st.session_state.gender
|
| 53 |
del st.session_state.profession
|
| 54 |
del st.session_state.hobby
|
| 55 |
+
del st.session_state.email
|
| 56 |
del st.session_state.clicked
|
| 57 |
if submitted:
|
| 58 |
placeholder.empty()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
utils.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 2 |
+
import re
|
| 3 |
+
|
| 4 |
+
# Load saved model and tokenizer
|
| 5 |
+
model_checkpoint = "24NLPGroupO/EmailGeneration"
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained(model_checkpoint, truncation=True)
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_checkpoint)
|
| 8 |
+
|
| 9 |
+
# Set up the generation pipeline
|
| 10 |
+
generator = pipeline('text-generation', model=model, tokenizer=tokenizer)
|
| 11 |
+
|
| 12 |
+
def clean_generated_text(text):
|
| 13 |
+
# Basic cleaning
|
| 14 |
+
text = re.sub(r'^(Re:|Fwd:)', '', text) # Remove reply and forward marks
|
| 15 |
+
text = re.sub(r'Best regards,.*$', '', text, flags=re.DOTALL) # Remove everything after signature
|
| 16 |
+
text = re.sub(r'PHONE.*$', '', text, flags=re.DOTALL) # Remove everything after phone numbers
|
| 17 |
+
text = re.sub(r'Email:.*$', '', text, flags=re.DOTALL) # Remove everything after email addresses
|
| 18 |
+
text = re.sub(r'Cc:.*$', '', text, flags=re.DOTALL) # Remove CC list
|
| 19 |
+
text = re.sub(r'\* Attachments:.*', '', text, flags=re.S) # Remove 'Attachments:' and everything following it
|
| 20 |
+
text = re.sub(r'©️ .*$', '', text, flags=re.DOTALL) # Remove copyright and ownership statements
|
| 21 |
+
text = re.sub(r'URL If this message is not displaying properly, click here.*$', '', text, flags=re.DOTALL) # Remove error display message and links
|
| 22 |
+
text = re.sub(r'\d{5,}', 'NUMBER', text) # Replace long sequences of numbers, likely phone numbers or ZIP codes
|
| 23 |
+
return text.strip()
|
| 24 |
+
|
| 25 |
+
def generate_email(product, gender, profession, hobby):
|
| 26 |
+
input_text = f"{product} {gender} {profession} {hobby}"
|
| 27 |
+
result = generator(
|
| 28 |
+
input_text, # Initial text to prompt the model. Sets the context or topic for text generation.
|
| 29 |
+
max_length=256, # Maximum length of the generated text in tokens, limiting the output size.
|
| 30 |
+
do_sample=True, # Enables stochastic sampling; the model can generate diverse outputs at each step.
|
| 31 |
+
top_k=20, # Limits the vocabulary considered at each step to the top-k most likely next words.
|
| 32 |
+
top_p=0.6, # Uses nucleus sampling: Narrows down to the smallest set of words totaling 60% of the likelihood.
|
| 33 |
+
temperature=0.4, # Scales logits before sampling to reduce randomness and produce more deterministic output.
|
| 34 |
+
repetition_penalty=1.5, # Penalizes words that were already mentioned, reducing repetition in the text.
|
| 35 |
+
# truncation=True, # Truncates the output to the maximum length if it exceeds it.
|
| 36 |
+
num_return_sequences=3 # Generates three different sequences to choose from, enhancing output variety.
|
| 37 |
+
)
|
| 38 |
+
# Select the best output from the generated sequences
|
| 39 |
+
best_text = sorted([clean_generated_text(r['generated_text']) for r in result], key=len)[-1]
|
| 40 |
+
return best_text
|