isom5240 commited on
Commit
c3c7832
·
verified ·
1 Parent(s): 860fb32

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -38
app.py CHANGED
@@ -1,47 +1,41 @@
1
  # import part
2
  import streamlit as st
3
- from PIL import Image
4
- import time
5
  from transformers import pipeline
6
 
7
- # Load models (once globally)
8
- img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
- story_gen = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
10
-
11
  # function part
12
- def generate_image_caption(image):
13
- """Generates a caption for the given image using a pre-trained model."""
14
- result = img2caption(image)
15
- return result[0]['generated_text']
 
16
 
 
17
  def text2story(text):
18
- """Generates a story from the given caption using a story generation model."""
19
- story_text = story_gen(text)[0]['generated_text']
20
  return story_text
21
 
22
- # main part
23
- # App title
24
- st.title("Assignment")
25
-
26
- # Write some text
27
- st.write("Image to Story")
28
-
29
- # File uploader for image
30
- uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
31
-
32
- # Display image with spinner
33
- if uploaded_image is not None:
34
- with st.spinner("Processing..."):
35
- time.sleep(1) # Simulate a delay
36
- image = Image.open(uploaded_image)
37
- st.image(image, caption="Uploaded Image", use_column_width=True)
38
-
39
- # Generate caption
40
- caption = generate_image_caption(uploaded_image.name)
41
- st.write(f"**Generated Caption:** {caption}")
42
-
43
- # Button to generate story
44
- if st.button("Generate Story from Caption"):
45
- story = text2story(caption)
46
- st.markdown("**Generated Story:**")
47
- st.write(story)
 
1
  # import part
2
  import streamlit as st
 
 
3
  from transformers import pipeline
4
 
 
 
 
 
5
  # function part
6
+ # img2text
7
+ def img2text(url):
8
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
+ text = image_to_text_model(url)[0]["generated_text"]
10
+ return text
11
 
12
+ # text2story
13
  def text2story(text):
14
+ pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
15
+ story_text = pipe(text)[0]['generated_text']
16
  return story_text
17
 
18
+ # text2audio
19
+ def text2audio(story_text):
20
+ pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
21
+ audio_data = pipe(story_text)
22
+ return audio_data
23
+
24
+
25
+ def main():
26
+ st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
27
+ st.header("Turn Your Image to Audio Story")
28
+ uploaded_file = st.file_uploader("Select an Image...")
29
+
30
+ if uploaded_file is not None:
31
+ print(uploaded_file)
32
+ bytes_data = uploaded_file.getvalue()
33
+ with open(uploaded_file.name, "wb") as file:
34
+ file.write(bytes_data)
35
+ st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
36
+
37
+
38
+ #Stage 1: Image to Text
39
+ st.text('Processing img2text...')
40
+ scenario = img2text(uploaded_file.name)
41
+ st.write(scenario)