adi-123 commited on
Commit
a814e5a
Β·
verified Β·
1 Parent(s): 2862b53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +128 -51
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 img2txt, txt2story, txt2speech, get_user_preferences
 
 
 
 
 
 
 
 
8
 
9
-
10
- # Main function
11
  def main():
12
- st.set_page_config(page_title="🎨 Image-to-Audio Story 🎧", page_icon="πŸ–ΌοΈ")
 
 
 
 
 
13
  st.title("Turn the Image into Audio Story")
14
 
15
- # Allows users to upload an image file
16
- uploaded_file = st.file_uploader("# πŸ“· Upload an image...", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- # Parameters for LLM model (in the sidebar)
19
- st.sidebar.markdown("# LLM Inference Configuration Parameters")
20
- top_k = st.sidebar.number_input("Top-K", min_value=1, max_value=100, value=5)
21
- top_p = st.sidebar.number_input("Top-P", min_value=0.0, max_value=1.0, value=0.8)
22
- temperature = st.sidebar.number_input("Temperature", min_value=0.1, max_value=2.0, value=1.5)
 
23
 
24
- # Get user preferences for the story
25
- st.markdown("## Story Preferences")
26
- preferences = get_user_preferences()
27
 
28
- if uploaded_file is not None:
29
- # Reads and saves uploaded image file
30
- bytes_data = uploaded_file.read()
31
- with open("uploaded_image.jpg", "wb") as file:
32
- file.write(bytes_data)
 
 
 
33
 
34
- st.image(uploaded_file, caption='πŸ–ΌοΈ Uploaded Image', use_column_width=True)
 
 
35
 
36
- # Initiates AI processing and story generation
37
- with st.spinner("## πŸ€– AI is at Work! "):
38
- scenario = img2txt("uploaded_image.jpg") # Extracts text from the image
39
-
40
- # Modify the prompt to include user preferences
41
- prompt = f"Based on the image description: '{scenario}', create a {preferences['genre']} story set in {preferences['setting']} in {preferences['continent']}. " \
42
- f"The story should have a {preferences['tone']} tone and explore the theme of {preferences['theme']}. " \
43
- f"The main conflict should be {preferences['conflict']}. " \
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
- st.markdown("---")
51
- st.markdown("## πŸ“œ Image Caption")
52
- st.write(scenario)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
 
54
- st.markdown("---")
55
- st.markdown("## πŸ“– Story")
56
- st.write(story)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
 
58
- st.markdown("---")
59
- st.markdown("## 🎧 Audio Story")
60
- st.audio("audio_story.mp3")
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()