File size: 3,606 Bytes
2bdb7fc
2cc0fa9
 
 
 
74a4018
2cc0fa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
!sudo apt-get install -y poppler-utils
import streamlit as st
from PIL import Image
from byaldi import RAGMultiModalModel
import tempfile
import torch

# Function to upload image, run inference, and display output
def upload_image_and_infer():
    # Step 1: Allow user to upload an image file
    uploaded_file = st.file_uploader("Upload an image file", type=["jpg", "png", "jpeg"])
    
    if uploaded_file is not None:
        # Step 2: Save uploaded image to temporary file
        with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
            temp_file.write(uploaded_file.read())
            temp_path = temp_file.name

        # Step 3: Display the uploaded image
        image = Image.open(temp_path)
        st.image(image, caption="Uploaded Image", use_column_width=True)

        # Step 4: Load the RAGMultiModalModel and processor
        RAG = RAGMultiModalModel.from_pretrained("vidore/colpali")
        model = Qwen2VLForConditionalGeneration.from_pretrained(
            "Qwen/Qwen2-VL-2B-Instruct-GPTQ-Int8", torch_dtype="auto", device_map="auto"
        )
        processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-2B-Instruct-GPTQ-Int8")
        
        # Assuming `results` contains the page number information
        text_query = "extract the details?"
        RAG.index(
            input_path=temp_path,  # Using the uploaded image's temporary path
            index_name="image_index",
            store_collection_with_index=False,
            overwrite=True
        )
        results = RAG.search(text_query, k=1)
        
        # Step 5: Prepare messages for inference
        image_index = results[0]["page_num"] - 1  # Get page number from the search result
        messages = [
            {
                "role": "user",
                "content": [
                    {
                        "type": "image",
                        "image": image,  # Use the uploaded image
                    },
                    {"type": "text", "text": text_query},
                ],
            }
        ]

        # Step 6: Prepare input for the model
        text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
        image_inputs, video_inputs = process_vision_info(messages)  # Assuming process_vision_info is defined
        
        # Tokenizing and preparing inputs
        inputs = processor(
            text=[text],
            images=image_inputs,
            videos=video_inputs,
            padding=True,
            return_tensors="pt",
        )
        inputs = inputs.to("cuda")
        
        # Step 7: Inference and generate output
        generated_ids = model.generate(**inputs, max_new_tokens=128)
        generated_ids_trimmed = [
            out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
        ]
        
        # Decode the generated output
        output_text = processor.batch_decode(
            generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
        )

        # Step 8: Display the output in Streamlit
        st.write("Generated Output:", output_text)
    else:
        st.write("Please upload an image.")

# Helper function to process images (replace with actual implementation if needed)
def process_vision_info(messages):
    image_inputs = [msg['content'][0]['image'] for msg in messages if 'image' in msg['content'][0]]
    video_inputs = []  # Assuming no video inputs for now
    return image_inputs, video_inputs

# Run the function inside the Streamlit app
upload_image_and_infer()