Update utils.py
Browse files
utils.py
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import re
|
2 |
import os
|
3 |
import streamlit as st
|
4 |
-
import requests
|
5 |
from transformers import pipeline
|
6 |
from typing import Dict, Optional
|
7 |
from together import Together
|
8 |
from gtts import gTTS
|
9 |
-
from
|
10 |
|
11 |
|
12 |
# Image-to-text
|
@@ -61,56 +60,40 @@ def txt2speech(text: str) -> None:
|
|
61 |
|
62 |
# Get user preferences for the story
|
63 |
def get_user_preferences() -> Dict[str, str]:
|
64 |
-
preferences = {
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
return preferences
|
77 |
|
78 |
|
79 |
def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
|
80 |
"""
|
81 |
-
Send the story text and audio file to the specified email address using
|
82 |
Returns True if successful, False otherwise
|
83 |
"""
|
84 |
try:
|
85 |
-
#
|
86 |
-
|
87 |
-
sender_password = os.getenv('SENDER_PASSWORD')
|
88 |
|
89 |
-
|
90 |
-
raise ValueError("Email credentials not configured")
|
91 |
-
|
92 |
-
# Create email subject and content
|
93 |
subject = "Your Generated Story"
|
94 |
-
content = f"
|
95 |
-
|
96 |
-
{story_text}
|
97 |
-
|
98 |
-
Enjoy!"""
|
99 |
-
|
100 |
-
# Initialize mail sender
|
101 |
-
mailer = Mailsender(
|
102 |
-
sender_email,
|
103 |
-
sender_password,
|
104 |
-
recipient_email,
|
105 |
-
subject,
|
106 |
-
content
|
107 |
-
)
|
108 |
-
|
109 |
-
# Add audio file attachment
|
110 |
-
mailer.add_attachment(audio_file_path)
|
111 |
|
112 |
# Send email
|
113 |
-
|
|
|
|
|
|
|
|
|
|
|
114 |
return True
|
115 |
|
116 |
except Exception as e:
|
@@ -122,24 +105,4 @@ def validate_email(email: str) -> bool:
|
|
122 |
Basic email validation
|
123 |
"""
|
124 |
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
125 |
-
return re.match(pattern, email) is not None
|
126 |
-
|
127 |
-
def validate_gmail_credentials(email: str, password: str) -> Optional[str]:
|
128 |
-
"""
|
129 |
-
Validate Gmail credentials by attempting to create a test mailer
|
130 |
-
Returns None if successful, error message if failed
|
131 |
-
"""
|
132 |
-
try:
|
133 |
-
test_mailer = Mailsender(
|
134 |
-
email,
|
135 |
-
password,
|
136 |
-
email, # Send to self for testing
|
137 |
-
"Test Connection",
|
138 |
-
"Testing credentials"
|
139 |
-
)
|
140 |
-
# Just initialize the connection without sending
|
141 |
-
test_mailer.initialize_connection()
|
142 |
-
test_mailer.close_connection()
|
143 |
-
return None
|
144 |
-
except Exception as e:
|
145 |
-
return str(e)
|
|
|
1 |
import re
|
2 |
import os
|
3 |
import streamlit as st
|
|
|
4 |
from transformers import pipeline
|
5 |
from typing import Dict, Optional
|
6 |
from together import Together
|
7 |
from gtts import gTTS
|
8 |
+
from python_mail_sender import MailSender # Updated import for python-mail-sender
|
9 |
|
10 |
|
11 |
# Image-to-text
|
|
|
60 |
|
61 |
# Get user preferences for the story
|
62 |
def get_user_preferences() -> Dict[str, str]:
|
63 |
+
preferences = {
|
64 |
+
'continent': st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"]),
|
65 |
+
'genre': st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"]),
|
66 |
+
'setting': st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"]),
|
67 |
+
'plot': st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"]),
|
68 |
+
'tone': st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"]),
|
69 |
+
'theme': st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"]),
|
70 |
+
'conflict': st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"]),
|
71 |
+
'twist': st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"]),
|
72 |
+
'ending': st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
|
73 |
+
}
|
|
|
74 |
return preferences
|
75 |
|
76 |
|
77 |
def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
|
78 |
"""
|
79 |
+
Send the story text and audio file to the specified email address using python-mail-sender
|
80 |
Returns True if successful, False otherwise
|
81 |
"""
|
82 |
try:
|
83 |
+
# Initialize the MailSender with env variables
|
84 |
+
mail_sender = MailSender()
|
|
|
85 |
|
86 |
+
# Email configuration
|
|
|
|
|
|
|
87 |
subject = "Your Generated Story"
|
88 |
+
content = f"Here's your generated story:\n\n{story_text}\n\nEnjoy!"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
# Send email
|
91 |
+
mail_sender.send_mail(
|
92 |
+
receiver_address=recipient_email,
|
93 |
+
subject=subject,
|
94 |
+
email_content=content,
|
95 |
+
attached_files=[audio_file_path]
|
96 |
+
)
|
97 |
return True
|
98 |
|
99 |
except Exception as e:
|
|
|
105 |
Basic email validation
|
106 |
"""
|
107 |
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
108 |
+
return re.match(pattern, email) is not None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|