isom5240 commited on
Commit
4ef111f
·
verified ·
1 Parent(s): c9fe61f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -16
app.py CHANGED
@@ -4,22 +4,21 @@ from PIL import Image
4
  import time
5
  from transformers import pipeline
6
 
 
 
 
 
7
  # function part
8
- def generate_image_caption(image_path):
9
  """Generates a caption for the given image using a pre-trained model."""
10
- img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
11
- result = img2caption(image_path)
12
  return result[0]['generated_text']
13
 
14
- # text2story
15
  def text2story(text):
16
- pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
17
- story_text = pipe(text)[0]['generated_text']
18
  return story_text
19
 
20
-
21
-
22
-
23
  # main part
24
  # App title
25
  st.title("Assignment")
@@ -27,18 +26,22 @@ st.title("Assignment")
27
  # Write some text
28
  st.write("Image to Story")
29
 
30
- # File uploader for image and audio
31
  uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
32
 
33
-
34
  # Display image with spinner
35
  if uploaded_image is not None:
36
- with st.spinner("Loading image..."):
37
  time.sleep(1) # Simulate a delay
38
  image = Image.open(uploaded_image)
39
  st.image(image, caption="Uploaded Image", use_column_width=True)
 
 
40
  caption = generate_image_caption(uploaded_image)
41
- st.write("Generated Caption: {caption}")
42
-
43
-
44
-
 
 
 
 
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")
 
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)
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)