123LETSPLAY commited on
Commit
c920c24
1 Parent(s): f22cbe7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -0
app.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import VisionEncoderDecoderModel, ViTImageProcessor, AutoTokenizer
3
+ from PIL import Image
4
+
5
+ # Load the pre-trained model and processor
6
+ model = VisionEncoderDecoderModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
7
+ processor = ViTImageProcessor.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
8
+ tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
9
+
10
+ # Streamlit app title
11
+ st.title("Image to Text App")
12
+
13
+ # File uploader
14
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
15
+
16
+ if uploaded_file is not None:
17
+ # Load and display the image
18
+ image = Image.open(uploaded_file)
19
+ st.image(image, caption='Uploaded Image', use_column_width=True)
20
+
21
+ # Process the image
22
+ pixel_values = processor(images=image, return_tensors="pt").pixel_values
23
+
24
+ # Generate text
25
+ output_ids = model.generate(pixel_values)
26
+ text = tokenizer.decode(output_ids[0], skip_special_tokens=True)
27
+
28
+ # Display the generated text
29
+ st.write("Generated Text:")
30
+ st.write(text)