Update app.py
Browse files
app.py
CHANGED
|
@@ -4,69 +4,146 @@ import requests
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from typing import Dict
|
| 6 |
from together import Together
|
| 7 |
-
from utils import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
# Main function
|
| 11 |
def main():
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
st.title("Turn the Image into Audio Story")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
st.sidebar.markdown("# LLM
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
st.
|
| 26 |
-
preferences = get_user_preferences()
|
| 27 |
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
f"The story should have a {preferences['twist']} and end with a {preferences['ending']} ending."
|
| 45 |
-
|
| 46 |
-
story = txt2story(prompt, top_k, top_p, temperature) # Generates a story based on the image text, LLM params, and user preferences
|
| 47 |
-
|
| 48 |
-
txt2speech(story) # Converts the story to audio
|
| 49 |
|
| 50 |
-
|
| 51 |
-
st.
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
if __name__ == '__main__':
|
| 64 |
-
main()
|
| 65 |
-
|
| 66 |
-
# Credits
|
| 67 |
-
st.markdown("### Credits")
|
| 68 |
-
st.caption('''
|
| 69 |
-
Made with β€οΈ by @Aditya-Neural-Net-Ninja\n
|
| 70 |
-
Utilizes Image-to-Text model, Text Generation model, Google Text-to-Speech libraary\n
|
| 71 |
-
Gratitude to Streamlit, π€ Spaces for Deployment & Hosting
|
| 72 |
-
''')
|
|
|
|
| 4 |
from transformers import pipeline
|
| 5 |
from typing import Dict
|
| 6 |
from together import Together
|
| 7 |
+
from utils import (
|
| 8 |
+
img2txt,
|
| 9 |
+
txt2story,
|
| 10 |
+
txt2speech,
|
| 11 |
+
get_user_preferences,
|
| 12 |
+
send_story_email,
|
| 13 |
+
validate_email,
|
| 14 |
+
create_gmail_config
|
| 15 |
+
)
|
| 16 |
|
|
|
|
|
|
|
| 17 |
def main():
|
| 18 |
+
# Page configuration
|
| 19 |
+
st.set_page_config(
|
| 20 |
+
page_title="π¨ Image-to-Audio Story π§",
|
| 21 |
+
page_icon="πΌοΈ",
|
| 22 |
+
layout="wide"
|
| 23 |
+
)
|
| 24 |
st.title("Turn the Image into Audio Story")
|
| 25 |
|
| 26 |
+
# Check for Gmail configuration in sidebar
|
| 27 |
+
if not os.path.exists('pysnail.conf'):
|
| 28 |
+
st.sidebar.markdown("## π§ Email Configuration")
|
| 29 |
+
with st.sidebar.expander("Configure Gmail Settings", expanded=True):
|
| 30 |
+
sender_email = st.text_input("Gmail Address:", type="default", key="gmail")
|
| 31 |
+
sender_password = st.text_input("App Password:", type="password", key="password")
|
| 32 |
+
|
| 33 |
+
if st.button("Save Configuration"):
|
| 34 |
+
if sender_email and sender_password:
|
| 35 |
+
os.environ['SENDER_EMAIL'] = sender_email
|
| 36 |
+
os.environ['SENDER_PASSWORD'] = sender_password
|
| 37 |
+
create_gmail_config()
|
| 38 |
+
st.success("Email configuration saved successfully!")
|
| 39 |
+
else:
|
| 40 |
+
st.error("Please provide both email and password.")
|
| 41 |
|
| 42 |
+
# LLM Parameters in sidebar
|
| 43 |
+
st.sidebar.markdown("# βοΈ LLM Configuration")
|
| 44 |
+
with st.sidebar.expander("Model Parameters", expanded=False):
|
| 45 |
+
top_k = st.number_input("Top-K", min_value=1, max_value=100, value=5)
|
| 46 |
+
top_p = st.number_input("Top-P", min_value=0.0, max_value=1.0, value=0.8)
|
| 47 |
+
temperature = st.number_input("Temperature", min_value=0.1, max_value=2.0, value=1.5)
|
| 48 |
|
| 49 |
+
# Main content area
|
| 50 |
+
col1, col2 = st.columns([2, 3])
|
|
|
|
| 51 |
|
| 52 |
+
with col1:
|
| 53 |
+
# Image upload section
|
| 54 |
+
st.markdown("## π· Upload Image")
|
| 55 |
+
uploaded_file = st.file_uploader(
|
| 56 |
+
"Choose an image...",
|
| 57 |
+
type=["jpg", "jpeg", "png"],
|
| 58 |
+
help="Upload an image to generate a story from"
|
| 59 |
+
)
|
| 60 |
|
| 61 |
+
# Story preferences section
|
| 62 |
+
st.markdown("## π Story Preferences")
|
| 63 |
+
preferences = get_user_preferences()
|
| 64 |
|
| 65 |
+
with col2:
|
| 66 |
+
if uploaded_file is not None:
|
| 67 |
+
# Display uploaded image
|
| 68 |
+
st.markdown("## πΌοΈ Your Image")
|
| 69 |
+
bytes_data = uploaded_file.read()
|
| 70 |
+
with open("uploaded_image.jpg", "wb") as file:
|
| 71 |
+
file.write(bytes_data)
|
| 72 |
+
st.image(uploaded_file, use_column_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Process image and generate story
|
| 75 |
+
if st.button("π¨ Generate Story"):
|
| 76 |
+
with st.spinner("π€ AI is working its magic..."):
|
| 77 |
+
try:
|
| 78 |
+
# Get image description
|
| 79 |
+
scenario = img2txt("uploaded_image.jpg")
|
| 80 |
+
|
| 81 |
+
# Create story prompt
|
| 82 |
+
prompt = f"""Based on the image description: '{scenario}',
|
| 83 |
+
create a {preferences['genre']} story set in {preferences['setting']}
|
| 84 |
+
in {preferences['continent']}. The story should have a {preferences['tone']}
|
| 85 |
+
tone and explore the theme of {preferences['theme']}. The main conflict
|
| 86 |
+
should be {preferences['conflict']}. The story should have a {preferences['twist']}
|
| 87 |
+
and end with a {preferences['ending']} ending."""
|
| 88 |
+
|
| 89 |
+
# Generate story
|
| 90 |
+
story = txt2story(prompt, top_k, top_p, temperature)
|
| 91 |
+
|
| 92 |
+
# Convert to audio
|
| 93 |
+
txt2speech(story)
|
| 94 |
|
| 95 |
+
# Display results
|
| 96 |
+
st.markdown("---")
|
| 97 |
+
|
| 98 |
+
# Image caption
|
| 99 |
+
with st.expander("π Image Caption", expanded=True):
|
| 100 |
+
st.write(scenario)
|
| 101 |
+
|
| 102 |
+
# Story text
|
| 103 |
+
with st.expander("π Generated Story", expanded=True):
|
| 104 |
+
st.write(story)
|
| 105 |
+
|
| 106 |
+
# Audio player
|
| 107 |
+
with st.expander("π§ Audio Version", expanded=True):
|
| 108 |
+
st.audio("audio_story.mp3")
|
| 109 |
+
|
| 110 |
+
# Email section
|
| 111 |
+
st.markdown("---")
|
| 112 |
+
st.markdown("## π§ Get Story via Email")
|
| 113 |
+
|
| 114 |
+
# Only show email input if configuration exists
|
| 115 |
+
if os.path.exists('pysnail.conf'):
|
| 116 |
+
email = st.text_input(
|
| 117 |
+
"Enter your email address:",
|
| 118 |
+
help="We'll send you the story text and audio file"
|
| 119 |
+
)
|
| 120 |
+
|
| 121 |
+
if st.button("π€ Send to Email"):
|
| 122 |
+
if not email:
|
| 123 |
+
st.warning("Please enter an email address.")
|
| 124 |
+
elif not validate_email(email):
|
| 125 |
+
st.error("Please enter a valid email address.")
|
| 126 |
+
else:
|
| 127 |
+
with st.spinner("π¨ Sending email..."):
|
| 128 |
+
if send_story_email(email, story, "audio_story.mp3"):
|
| 129 |
+
st.success("βοΈ Story sent successfully! Check your email.")
|
| 130 |
+
else:
|
| 131 |
+
st.error("β Failed to send email. Please try again.")
|
| 132 |
+
else:
|
| 133 |
+
st.warning("β οΈ Please configure Gmail settings in the sidebar to enable email delivery.")
|
| 134 |
|
| 135 |
+
except Exception as e:
|
| 136 |
+
st.error(f"An error occurred: {str(e)}")
|
| 137 |
+
st.warning("Please try again or contact support if the problem persists.")
|
| 138 |
|
| 139 |
+
# Footer
|
| 140 |
+
st.markdown("---")
|
| 141 |
+
st.markdown("### Credits")
|
| 142 |
+
st.caption('''
|
| 143 |
+
Made with β€οΈ by @Aditya-Neural-Net-Ninja\n
|
| 144 |
+
Utilizes Image-to-Text model, Text Generation model, Google Text-to-Speech library\n
|
| 145 |
+
Gratitude to Streamlit, π€ Spaces for Deployment & Hosting
|
| 146 |
+
''')
|
| 147 |
|
| 148 |
if __name__ == '__main__':
|
| 149 |
+
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|