CR7CAD commited on
Commit
8fe6281
·
verified ·
1 Parent(s): efe4c0f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -23
app.py CHANGED
@@ -1,7 +1,11 @@
1
- # Only the two imports you requested
2
  import streamlit as st
3
  from transformers import pipeline
4
  from PIL import Image
 
 
 
 
5
 
6
  # Simple image-to-text function
7
  def img2text(image):
@@ -9,7 +13,7 @@ def img2text(image):
9
  text = image_to_text(image)[0]["generated_text"]
10
  return text
11
 
12
- # Simple text-to-story function
13
  def text2story(text):
14
  generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
15
  prompt = f"Write a short children's story based on this: {text}. The story should have a clear beginning, middle, and end. Keep it under 150 words. Once upon a time, "
@@ -52,11 +56,25 @@ def text2story(text):
52
  # If no good ending is found, return as is
53
  return story_text
54
 
55
- # Simple text-to-audio function
56
  def text2audio(story_text):
57
- synthesizer = pipeline("text-to-speech", model="HelpingAI/HelpingAI-TTS-v1")
58
- speech = synthesizer(story_text)
59
- return speech
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  # Basic Streamlit interface
62
  st.title("Image to Audio Story")
@@ -70,26 +88,22 @@ if uploaded_file is not None:
70
  image = Image.open(uploaded_file)
71
 
72
  # Image to Text
73
- st.write("Generating caption...")
74
- caption = img2text(image)
75
  st.write(f"Caption: {caption}")
76
 
77
  # Text to Story
78
- st.write("Creating story...")
79
- story = text2story(caption)
80
  st.write(f"Story: {story}")
81
 
82
  # Text to Audio
83
- st.write("Generating audio...")
84
- speech_output = text2audio(story)
85
-
86
- # Play audio
87
- try:
88
- if 'audio' in speech_output and 'sampling_rate' in speech_output:
89
- st.audio(speech_output['audio'], sample_rate=speech_output['sampling_rate'])
90
- elif 'audio_array' in speech_output and 'sampling_rate' in speech_output:
91
- st.audio(speech_output['audio_array'], sample_rate=speech_output['sampling_rate'])
92
- else:
93
- st.write("Audio generated but could not be played.")
94
- except Exception as e:
95
- st.error(f"Error playing audio: {e}")
 
1
+ # Imports
2
  import streamlit as st
3
  from transformers import pipeline
4
  from PIL import Image
5
+ import torch
6
+ from gtts import gTTS
7
+ import os
8
+ import tempfile
9
 
10
  # Simple image-to-text function
11
  def img2text(image):
 
13
  text = image_to_text(image)[0]["generated_text"]
14
  return text
15
 
16
+ # Improved text-to-story function with natural ending
17
  def text2story(text):
18
  generator = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0")
19
  prompt = f"Write a short children's story based on this: {text}. The story should have a clear beginning, middle, and end. Keep it under 150 words. Once upon a time, "
 
56
  # If no good ending is found, return as is
57
  return story_text
58
 
59
+ # Updated text-to-audio function using gTTS instead of transformers
60
  def text2audio(story_text):
61
+ # Create a temporary file
62
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
63
+ temp_filename = temp_file.name
64
+ temp_file.close()
65
+
66
+ # Use gTTS to convert text to speech
67
+ tts = gTTS(text=story_text, lang='en', slow=False)
68
+ tts.save(temp_filename)
69
+
70
+ # Read the audio file
71
+ with open(temp_filename, 'rb') as audio_file:
72
+ audio_bytes = audio_file.read()
73
+
74
+ # Clean up the temporary file
75
+ os.unlink(temp_filename)
76
+
77
+ return audio_bytes
78
 
79
  # Basic Streamlit interface
80
  st.title("Image to Audio Story")
 
88
  image = Image.open(uploaded_file)
89
 
90
  # Image to Text
91
+ with st.spinner("Generating caption..."):
92
+ caption = img2text(image)
93
  st.write(f"Caption: {caption}")
94
 
95
  # Text to Story
96
+ with st.spinner("Creating story..."):
97
+ story = text2story(caption)
98
  st.write(f"Story: {story}")
99
 
100
  # Text to Audio
101
+ with st.spinner("Generating audio..."):
102
+ try:
103
+ audio_bytes = text2audio(story)
104
+
105
+ # Play audio
106
+ st.audio(audio_bytes, format='audio/mp3')
107
+ except Exception as e:
108
+ st.error(f"Error generating or playing audio: {e}")
109
+ st.write("Make sure gTTS is installed with: pip install gTTS")