AidenYan commited on
Commit
4e51217
1 Parent(s): ee29e30

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -23
app.py CHANGED
@@ -4,32 +4,53 @@ from PIL import Image
4
  import requests
5
  from io import BytesIO
6
 
7
- # Load the pipeline
8
  image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
9
 
10
- st.title('Upload the pictures you would like to purchase')
11
 
12
- # Upload an image
13
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
14
 
15
- if uploaded_file is not None:
16
- # Convert the file to an image
17
- image = Image.open(uploaded_file)
18
- st.image(image, caption='Uploaded Image', use_column_width=True)
19
-
20
- # Save image to a buffer
21
- buf = BytesIO()
22
- image.save(buf, format='JPEG')
23
- byte_img = buf.getvalue()
24
 
25
- # You might need to adjust this part depending on your model's needs
26
- # For demonstration, assuming we need a URL - typically you'd need a model that accepts image bytes directly
27
- # Example: result = your_model_that_accepts_bytes(byte_img)
 
 
 
 
 
 
 
 
28
 
29
- # For this example, we just display a placeholder text
30
- st.write("It seems like you want...")
31
-
32
- # If your model accepts image bytes directly, you can call it like:
33
- # result = image_to_text(byte_img)
34
- # And then display the result
35
- # st.write(result)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
  import requests
5
  from io import BytesIO
6
 
7
+ # Initialize the pipeline
8
  image_to_text = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
9
 
10
+ st.title('Image Captioning Application')
11
 
12
+ # Function to load images from URL
13
+ def load_image_from_url(url):
14
+ try:
15
+ response = requests.get(url)
16
+ img = Image.open(BytesIO(response.content))
17
+ return img
18
+ except Exception as e:
19
+ st.error(f"Error loading image from URL: {e}")
20
+ return None
21
 
22
+ # User option to select input type: Upload or URL
23
+ input_type = st.radio("Select input type:", ("Upload Image", "Image URL"))
 
 
 
 
 
 
 
24
 
25
+ if input_type == "Upload Image":
26
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
27
+ if uploaded_file is not None:
28
+ image = Image.open(uploaded_file)
29
+ st.image(image, caption='Uploaded Image', use_column_width=True)
30
+ elif input_type == "Image URL":
31
+ image_url = st.text_input("Enter the image URL here:", "")
32
+ if image_url:
33
+ image = load_image_from_url(image_url)
34
+ if image:
35
+ st.image(image, caption='Image from URL', use_column_width=True)
36
 
37
+ # Generate caption button
38
+ if st.button('Generate Caption'):
39
+ if not image:
40
+ st.warning("Please upload an image or enter an image URL.")
41
+ else:
42
+ with st.spinner("Generating caption..."):
43
+ # Process the image and generate caption
44
+ if input_type == "Upload Image":
45
+ # Save the uploaded image to a temporary file to pass its path to the model
46
+ with open("temp_image.jpg", "wb") as f:
47
+ f.write(uploaded_file.getbuffer())
48
+ result = image_to_text("temp_image.jpg")
49
+ elif input_type == "Image URL" and image_url:
50
+ result = image_to_text(image_url)
51
+
52
+ if result:
53
+ generated_text = result[0]['generated_text']
54
+ st.success(f'Generated Caption: {generated_text}')
55
+ else:
56
+ st.error("Failed to generate caption.")