Update utils.py
Browse files
utils.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import os
|
2 |
import streamlit as st
|
3 |
import requests
|
@@ -5,6 +6,8 @@ from transformers import pipeline
|
|
5 |
from typing import Dict
|
6 |
from together import Together
|
7 |
from gtts import gTTS
|
|
|
|
|
8 |
|
9 |
# Image-to-text
|
10 |
def img2txt(url: str) -> str:
|
@@ -71,3 +74,56 @@ def get_user_preferences() -> Dict[str, str]:
|
|
71 |
preferences['ending'] = st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
|
72 |
|
73 |
return preferences
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import re
|
2 |
import os
|
3 |
import streamlit as st
|
4 |
import requests
|
|
|
6 |
from typing import Dict
|
7 |
from together import Together
|
8 |
from gtts import gTTS
|
9 |
+
from gmail_smtplib_micro import GmailSMTPLib, Email
|
10 |
+
|
11 |
|
12 |
# Image-to-text
|
13 |
def img2txt(url: str) -> str:
|
|
|
74 |
preferences['ending'] = st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
|
75 |
|
76 |
return preferences
|
77 |
+
|
78 |
+
|
79 |
+
def create_gmail_config():
|
80 |
+
"""
|
81 |
+
Creates a Gmail configuration file if it doesn't exist
|
82 |
+
"""
|
83 |
+
config_content = f"""[GMail]
|
84 |
+
username = {os.getenv('SENDER_EMAIL', '')}
|
85 |
+
password = {os.getenv('SENDER_PASSWORD', '')}
|
86 |
+
"""
|
87 |
+
with open('pysnail.conf', 'w') as f:
|
88 |
+
f.write(config_content)
|
89 |
+
|
90 |
+
def send_story_email(recipient_email: str, story_text: str, audio_file_path: str) -> bool:
|
91 |
+
"""
|
92 |
+
Send the story text and audio file to the specified email address
|
93 |
+
Returns True if successful, False otherwise
|
94 |
+
"""
|
95 |
+
try:
|
96 |
+
# Create email content
|
97 |
+
subject = "Your Generated Story"
|
98 |
+
body = f"""Here's your generated story:
|
99 |
+
|
100 |
+
{story_text}
|
101 |
+
|
102 |
+
Enjoy!"""
|
103 |
+
|
104 |
+
# Create email object
|
105 |
+
email = Email(subject, body)
|
106 |
+
|
107 |
+
# Add audio file attachment
|
108 |
+
email.add_attachment(audio_file_path)
|
109 |
+
|
110 |
+
# Create Gmail configuration if it doesn't exist
|
111 |
+
if not os.path.exists('pysnail.conf'):
|
112 |
+
create_gmail_config()
|
113 |
+
|
114 |
+
# Initialize Gmail client and send email
|
115 |
+
gmail = GmailSMTPLib('pysnail.conf')
|
116 |
+
gmail.send_object(email, recipient_email)
|
117 |
+
|
118 |
+
return True
|
119 |
+
|
120 |
+
except Exception as e:
|
121 |
+
print(f"Error sending email: {str(e)}")
|
122 |
+
return False
|
123 |
+
|
124 |
+
def validate_email(email: str) -> bool:
|
125 |
+
"""
|
126 |
+
Basic email validation
|
127 |
+
"""
|
128 |
+
pattern = r'^[\w\.-]+@[\w\.-]+\.\w+$'
|
129 |
+
return re.match(pattern, email) is not None
|