CR7CAD commited on
Commit
b8a0fec
·
verified ·
1 Parent(s): b038974

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -21
app.py CHANGED
@@ -8,10 +8,10 @@ import os
8
  import traceback
9
 
10
  # Set page title
11
- st.set_page_config(page_title="Kids Story Generator")
12
 
13
  # Title and introduction
14
- st.title("Kids Story Generator")
15
  st.write("Upload a picture and let's create a magical story!")
16
 
17
  # Initialize models with better error handling
@@ -29,8 +29,6 @@ with st.spinner("Loading models..."):
29
  image_to_text, story_generator, error = load_models()
30
  if error:
31
  st.error(f"Failed to load models: {error}")
32
- else:
33
- st.success("Models loaded successfully!")
34
 
35
  # Function to generate caption from image
36
  def generate_caption(image):
@@ -48,9 +46,6 @@ def generate_story(caption):
48
  try:
49
  prompt = f"Once upon a time, {caption} "
50
 
51
- # Debug output
52
- st.write(f"Prompt: {prompt}")
53
-
54
  # Generate with increased timeout and temperature
55
  result = story_generator(
56
  prompt,
@@ -60,9 +55,6 @@ def generate_story(caption):
60
  top_p=0.95
61
  )
62
 
63
- # Debug output
64
- st.write(f"Generation result: {result}")
65
-
66
  if result and len(result) > 0:
67
  story = result[0]['generated_text']
68
 
@@ -78,8 +70,6 @@ def generate_story(caption):
78
  return story, None
79
  return "Story generation failed.", "No story generated"
80
  except Exception as e:
81
- st.error(f"Error in story generation: {str(e)}")
82
- st.error(traceback.format_exc())
83
  return "Once upon a time... (Story generation failed)", str(e)
84
 
85
  # Function to convert text to speech
@@ -107,28 +97,32 @@ if uploaded_file is not None and image_to_text is not None and story_generator i
107
  # Generate caption
108
  caption, caption_error = generate_caption(image)
109
  if caption_error:
110
- st.warning(f"Caption generation issue: {caption_error}")
 
 
111
  st.write("Image caption:", caption)
112
 
113
  # Generate story
114
  story, story_error = generate_story(caption)
115
- if story_error:
116
- st.warning(f"Story generation issue: {story_error}")
 
 
117
  word_count = len(story.split())
118
  st.write(f"### Your Story ({word_count} words)")
119
  st.write(story)
120
 
121
  # Generate audio
122
  audio_file, audio_error = text_to_speech(story)
123
- if audio_error:
124
- st.warning(f"Audio generation issue: {audio_error}")
125
- else:
126
  # Display audio
127
  st.write("### Listen to your story")
128
  st.audio(audio_file)
129
  except Exception as e:
130
- st.error(f"Error processing image: {str(e)}")
131
- st.error(traceback.format_exc())
132
 
133
  st.markdown("---")
134
- st.write("Created for ISOM5240_Assignment1")
 
8
  import traceback
9
 
10
  # Set page title
11
+ st.set_page_config(page_title="Story Generator for Kids")
12
 
13
  # Title and introduction
14
+ st.title("Story Generator for Kids")
15
  st.write("Upload a picture and let's create a magical story!")
16
 
17
  # Initialize models with better error handling
 
29
  image_to_text, story_generator, error = load_models()
30
  if error:
31
  st.error(f"Failed to load models: {error}")
 
 
32
 
33
  # Function to generate caption from image
34
  def generate_caption(image):
 
46
  try:
47
  prompt = f"Once upon a time, {caption} "
48
 
 
 
 
49
  # Generate with increased timeout and temperature
50
  result = story_generator(
51
  prompt,
 
55
  top_p=0.95
56
  )
57
 
 
 
 
58
  if result and len(result) > 0:
59
  story = result[0]['generated_text']
60
 
 
70
  return story, None
71
  return "Story generation failed.", "No story generated"
72
  except Exception as e:
 
 
73
  return "Once upon a time... (Story generation failed)", str(e)
74
 
75
  # Function to convert text to speech
 
97
  # Generate caption
98
  caption, caption_error = generate_caption(image)
99
  if caption_error:
100
+ st.warning(f"Caption generation issue: {caption_error}", icon="⚠️")
101
+
102
+ # Display the caption (without debug information)
103
  st.write("Image caption:", caption)
104
 
105
  # Generate story
106
  story, story_error = generate_story(caption)
107
+ if story_error and not st.session_state.get("deployed", True):
108
+ st.warning(f"Story generation issue: {story_error}", icon="⚠️")
109
+
110
+ # Display the story (without debug information)
111
  word_count = len(story.split())
112
  st.write(f"### Your Story ({word_count} words)")
113
  st.write(story)
114
 
115
  # Generate audio
116
  audio_file, audio_error = text_to_speech(story)
117
+ if audio_error and not st.session_state.get("deployed", True):
118
+ st.warning(f"Audio generation issue: {audio_error}", icon="⚠️")
119
+ elif audio_file:
120
  # Display audio
121
  st.write("### Listen to your story")
122
  st.audio(audio_file)
123
  except Exception as e:
124
+ if not st.session_state.get("deployed", True):
125
+ st.error(f"Error processing image: {str(e)}")
126
 
127
  st.markdown("---")
128
+ st.write("Created for ISOM5240 Assignment 1")