isom5240 commited on
Commit
9a60ca7
·
verified ·
1 Parent(s): 149b30a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -19
app.py CHANGED
@@ -3,11 +3,12 @@ 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):
@@ -15,30 +16,28 @@ def text2story(text):
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)
42
 
43
  if __name__ == "__main__":
44
  main()
 
3
  from transformers import pipeline
4
 
5
  # function part
6
+ # function part
7
+ def generate_image_caption(image_path):
8
+ """Generates a caption for the given image using a pre-trained model."""
9
+ img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
10
+ result = img2caption(image_path)
11
+ return result[0]['generated_text']
12
 
13
  # text2story
14
  def text2story(text):
 
16
  story_text = pipe(text)[0]['generated_text']
17
  return story_text
18
 
19
+
 
 
 
 
20
 
21
 
22
  def main():
23
+ # App title
24
+ st.title("Streamlit Demo on Hugging Face")
25
+
26
+ # Write some text
27
+ st.write("Welcome to a demo app showcasing basic Streamlit components!")
28
+
29
+ uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
30
 
31
  if uploaded_file is not None:
32
  print(uploaded_file)
33
+
34
+ st.image(uploaded_image, caption="Uploaded Image", use_column_width=True)
 
 
35
 
36
 
37
  #Stage 1: Image to Text
38
  st.text('Processing img2text...')
39
+ image_caption = generate_image_caption(uploaded_image.name)
40
+ st.write(image_caption)
41
 
42
  if __name__ == "__main__":
43
  main()