adi-123 commited on
Commit
7a1f380
·
verified ·
1 Parent(s): 42dd876

Update utils.py

Browse files
Files changed (1) hide show
  1. utils.py +24 -61
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 py_mailsender import Mailsender
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
- preferences['continent'] = st.selectbox("Continent", ["North America", "Europe", "Asia", "Africa", "Australia"])
67
- preferences['genre'] = st.selectbox("Genre", ["Science Fiction", "Fantasy", "Mystery", "Romance"])
68
- preferences['setting'] = st.selectbox("Setting", ["Future", "Medieval times", "Modern day", "Alternate reality"])
69
- preferences['plot'] = st.selectbox("Plot", ["Hero's journey", "Solving a mystery", "Love story", "Survival"])
70
- preferences['tone'] = st.selectbox("Tone", ["Serious", "Light-hearted", "Humorous", "Dark"])
71
- preferences['theme'] = st.selectbox("Theme", ["Self-discovery", "Redemption", "Love", "Justice"])
72
- preferences['conflict'] = st.selectbox("Conflict Type", ["Person vs. Society", "Internal struggle", "Person vs. Nature", "Person vs. Person"])
73
- preferences['twist'] = st.selectbox("Mystery/Twist", ["Plot twist", "Hidden identity", "Unexpected ally/enemy", "Time paradox"])
74
- preferences['ending'] = st.selectbox("Ending", ["Happy", "Bittersweet", "Open-ended", "Tragic"])
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 py-mailsender
82
  Returns True if successful, False otherwise
83
  """
84
  try:
85
- # Get email credentials from environment variables
86
- sender_email = os.getenv('SENDER_EMAIL')
87
- sender_password = os.getenv('SENDER_PASSWORD')
88
 
89
- if not sender_email or not sender_password:
90
- raise ValueError("Email credentials not configured")
91
-
92
- # Create email subject and content
93
  subject = "Your Generated Story"
94
- content = f"""Here's your generated story:
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
- mailer.send()
 
 
 
 
 
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