File size: 12,658 Bytes
c7906eb
 
91d2c01
218ebfb
 
 
 
 
c7906eb
218ebfb
c7906eb
91d2c01
c7906eb
 
218ebfb
 
 
 
 
 
 
91d2c01
218ebfb
 
91d2c01
c7906eb
91d2c01
218ebfb
 
 
 
 
 
280b089
218ebfb
 
 
91d2c01
c7906eb
 
218ebfb
c7906eb
 
 
 
 
 
 
218ebfb
 
 
 
c7906eb
 
 
218ebfb
 
c7906eb
91d2c01
6ed0791
c7906eb
a9be97c
c7906eb
 
91d2c01
218ebfb
91d2c01
218ebfb
 
fe76282
91d2c01
 
a9be97c
 
218ebfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7906eb
218ebfb
 
c7906eb
218ebfb
 
 
 
c7906eb
218ebfb
c7906eb
218ebfb
c7906eb
218ebfb
 
 
c7906eb
 
218ebfb
 
 
 
 
 
 
 
 
 
 
 
 
 
c7906eb
218ebfb
 
c7906eb
91d2c01
218ebfb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7906eb
218ebfb
 
 
 
c7906eb
 
 
 
218ebfb
 
 
 
 
c7906eb
 
218ebfb
 
 
 
 
c7906eb
218ebfb
 
c7906eb
218ebfb
 
 
 
91d2c01
218ebfb
 
c7906eb
218ebfb
 
c7906eb
218ebfb
 
 
 
c7906eb
218ebfb
 
c7906eb
218ebfb
 
 
 
 
 
 
 
 
 
91d2c01
86a82e4
218ebfb
9a23baa
218ebfb
 
 
 
 
c7906eb
 
218ebfb
 
 
 
91d2c01
 
c7906eb
 
218ebfb
 
 
 
 
 
c7906eb
 
218ebfb
 
 
 
 
 
c7906eb
218ebfb
86a82e4
b8a0d2d
91d2c01
c7906eb
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"""
app.py

A unified Gradio chat application for Multimodal OCR Granite Vision.
Commands (enter these as a prefix in the text input):
  - @rag: For retrieval‐augmented generation (e.g. PDF or text-based queries).
  - @granite: For image understanding.
  - @video-infer: For video understanding (video is downsampled into frames).

The app uses gr.MultimodalTextbox to support text input together with file uploads.
"""

import os
import time
import uuid
import random
import logging
from threading import Thread
from pathlib import Path
from datetime import datetime, timezone

import torch
import numpy as np
import cv2
from PIL import Image
import gradio as gr

from transformers import (
    AutoModelForCausalLM,
    AutoTokenizer,
    AutoProcessor,
    AutoModelForVision2Seq,
)

# ---------------------------
# Utility functions and setup
# ---------------------------

def get_device():
    if torch.backends.mps.is_available():
        return "mps"  # mac GPU
    elif torch.cuda.is_available():
        return "cuda"
    else:
        return "cpu"

device = get_device()

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
)

def downsample_video(video_path):
    """
    Downsamples the video into 10 evenly spaced frames.
    Returns a list of (PIL Image, timestamp in seconds) tuples.
    """
    vidcap = cv2.VideoCapture(video_path)
    total_frames = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
    fps = vidcap.get(cv2.CAP_PROP_FPS)
    frames = []
    frame_indices = np.linspace(0, total_frames - 1, 10, dtype=int)
    for i in frame_indices:
        vidcap.set(cv2.CAP_PROP_POS_FRAMES, i)
        success, image = vidcap.read()
        if success:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
            pil_image = Image.fromarray(image)
            timestamp = round(i / fps, 2)
            frames.append((pil_image, timestamp))
    vidcap.release()
    return frames

# ---------------------------
# HF Embedding and LLM classes
# ---------------------------

class HFEmbedding:
    def __init__(self, model_id: str):
        self.model_name = model_id
        logging.info(f"Loading embeddings model from: {self.model_name}")
        # Using langchain_huggingface for embeddings
        from langchain_huggingface import HuggingFaceEmbeddings  # ensure installed
        # For simplicity, force CPU (adjust if needed)
        self.embeddings_service = HuggingFaceEmbeddings(
            model_name=self.model_name,
            model_kwargs={"device": "cpu"},
        )

    def embed_documents(self, texts: list[str]) -> list[list[float]]:
        return self.embeddings_service.embed_documents(texts)

    def embed_query(self, text: str) -> list[float]:
        return self.embed_documents([text])[0]

class HFLLM:
    def __init__(self, model_name: str):
        self.device = device
        self.model_name = model_name
        logging.info("Loading HF language model...")
        self.tokenizer = AutoTokenizer.from_pretrained(model_name)
        self.model = AutoModelForCausalLM.from_pretrained(model_name).to(self.device)

    def generate(self, prompt: str) -> list:
        # Tokenize prompt and generate text
        model_inputs = self.tokenizer(prompt, return_tensors="pt").to(self.device)
        generated_ids = self.model.generate(**model_inputs, max_new_tokens=1024)
        generated_texts = self.tokenizer.batch_decode(generated_ids, skip_special_tokens=False)
        # Extract answer assuming a marker in the generated text
        response = [{"answer": generated_texts[0].split("<|end_of_role|>")[-1].split("<|end_of_text|>")[0]}]
        return response

# ---------------------------
# LightRAG: Retrieval-Augmented Generation (Dummy)
# ---------------------------

class LightRAG:
    def __init__(self, config: dict):
        self.config = config
        # Load generation and embedding models immediately (or lazy load as needed)
        self.gen_model = HFLLM(config['generation_model_id'])
        self._embedding_model = HFEmbedding(config['embedding_model_id'])

    def search(self, query: str, top_n: int = 5) -> list:
        # Dummy retrieval: In practice, integrate with a vector store
        from langchain_core.documents import Document  # ensure langchain_core is installed
        dummy_doc = Document(
            page_content="Dummy context for query: " + query,
            metadata={"type": "text"}
        )
        return [dummy_doc]

    def generate(self, query, context=None):
        if context is None:
            context = []
        # Build prompt by concatenating retrieved context with the query.
        prompt = ""
        for doc in context:
            prompt += doc.page_content + "\n"
        prompt += "\nQuestion: " + query + "\nAnswer:"
        results = self.gen_model.generate(prompt)
        answer = results[0]["answer"]
        return answer, prompt

# Global configuration for LightRAG
rag_config = {
    "embedding_model_id": "ibm-granite/granite-embedding-125m-english",
    "generation_model_id": "ibm-granite/granite-3.1-8b-instruct",
}
light_rag = LightRAG(rag_config)

# ---------------------------
# Granite Vision functions (for image and video)
# ---------------------------

# Set the Granite Vision model ID (adjust version as needed)
GRANITE_MODEL_ID = "ibm-granite/granite-vision-3.2-2b"
granite_processor = None
granite_model = None

def load_granite_model():
    """Lazy load the Granite vision processor and model."""
    global granite_processor, granite_model
    if granite_processor is None or granite_model is None:
        granite_processor = AutoProcessor.from_pretrained(GRANITE_MODEL_ID)
        granite_model = AutoModelForVision2Seq.from_pretrained(GRANITE_MODEL_ID, device_map="auto").to(device)
    return granite_processor, granite_model

def create_single_turn(image, text):
    """
    Creates a single-turn conversation message.
    If an image is provided, it is added along with the text.
    """
    if image is None:
        return {"role": "user", "content": [{"type": "text", "text": text}]}
    else:
        return {"role": "user", "content": [{"type": "image", "image": image}, {"type": "text", "text": text}]}

def generate_granite(image, prompt_text, max_new_tokens=1024, temperature=0.7, top_p=0.85, top_k=50, repetition_penalty=1.05):
    """
    Generates a response from the Granite Vision model given an image and prompt.
    """
    processor, model = load_granite_model()
    conversation = [create_single_turn(image, prompt_text)]
    inputs = processor.apply_chat_template(
        conversation, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt"
    ).to(device)
    output = model.generate(
        **inputs,
        max_new_tokens=max_new_tokens,
        do_sample=True,
        top_p=top_p,
        top_k=top_k,
        temperature=temperature,
        repetition_penalty=repetition_penalty,
    )
    decoded = processor.decode(output[0], skip_special_tokens=True)
    parts = decoded.strip().split("<|assistant|>")
    return parts[-1].strip()

def generate_video_infer(video_path, prompt_text, max_new_tokens=1024, temperature=0.7, top_p=0.85, top_k=50, repetition_penalty=1.05):
    """
    Processes a video file by downsampling frames and sending them along with a prompt
    to the Granite Vision model.
    """
    frames = downsample_video(video_path)
    conversation_content = []
    for img, ts in frames:
        conversation_content.append({"type": "text", "text": f"Frame at {ts} sec:"})
        conversation_content.append({"type": "image", "image": img})
    conversation_content.append({"type": "text", "text": prompt_text})
    conversation = [{"role": "user", "content": conversation_content}]
    processor, model = load_granite_model()
    inputs = processor.apply_chat_template(
        conversation, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt"
    ).to(device)
    output = model.generate(
        **inputs,
        max_new_tokens=max_new_tokens,
        do_sample=True,
        top_p=top_p,
        top_k=top_k,
        temperature=temperature,
        repetition_penalty=repetition_penalty,
    )
    decoded = processor.decode(output[0], skip_special_tokens=True)
    parts = decoded.strip().split("<|assistant|>")
    return parts[-1].strip()

# ---------------------------
# Unified generation function for ChatInterface
# ---------------------------

def generate(input_dict: dict, chat_history: list[dict],
             max_new_tokens: int, temperature: float,
             top_p: float, top_k: int, repetition_penalty: float):
    """
    Chat function that inspects the input text for special commands and routes:
      - @rag: Uses the RAG pipeline.
      - @granite: Uses Granite Vision for image understanding.
      - @video-infer: Uses Granite Vision for video processing.
    """
    text = input_dict["text"]
    files = input_dict.get("files", [])
    lower_text = text.strip().lower()

    # Optionally yield a progress message
    yield "Processing your request..."
    time.sleep(1)  # simulate processing delay

    if lower_text.startswith("@rag"):
        query = text[len("@rag"):].strip()
        logging.info(f"@rag command: {query}")
        context = light_rag.search(query)
        answer, _ = light_rag.generate(query, context)
        yield answer

    elif lower_text.startswith("@granite"):
        prompt_text = text[len("@granite"):].strip()
        logging.info(f"@granite command: {prompt_text}")
        if files:
            # Expecting an image file (as a PIL image)
            image = files[0]
            answer = generate_granite(image, prompt_text, max_new_tokens, temperature, top_p, top_k, repetition_penalty)
            yield answer
        else:
            yield "No image provided for @granite command."

    elif lower_text.startswith("@video-infer"):
        prompt_text = text[len("@video-infer"):].strip()
        logging.info(f"@video-infer command: {prompt_text}")
        if files:
            # Expecting a video file (the file path)
            video_path = files[0]
            answer = generate_video_infer(video_path, prompt_text, max_new_tokens, temperature, top_p, top_k, repetition_penalty)
            yield answer
        else:
            yield "No video provided for @video-infer command."

    else:
        # Default behavior: use RAG pipeline for text query.
        query = text.strip()
        logging.info(f"Default text query: {query}")
        context = light_rag.search(query)
        answer, _ = light_rag.generate(query, context)
        yield answer

# ---------------------------
# Gradio ChatInterface using MultimodalTextbox
# ---------------------------

demo = gr.ChatInterface(
    fn=generate,
    additional_inputs=[
        gr.Slider(label="Max new tokens", minimum=1, maximum=2048, step=1, value=1024),
        gr.Slider(label="Temperature", minimum=0.1, maximum=2.0, step=0.1, value=0.7),
        gr.Slider(label="Top-p", minimum=0.1, maximum=1.0, step=0.1, value=0.85),
        gr.Slider(label="Top-k", minimum=1, maximum=100, step=1, value=50),
        gr.Slider(label="Repetition penalty", minimum=1.0, maximum=2.0, step=0.05, value=1.05),
    ],
    examples=[
        # Examples show how to use the command prefixes.
        [{"text": "@rag What models are available in Watsonx?"}],
        [{"text": "@granite Describe the image", "files": [str(Path("examples") / "sample_image.png")]}],
        [{"text": "@video-infer Summarize the event in the video", "files": [str(Path("examples") / "sample_video.mp4")]}],
    ],
    cache_examples=False,
    type="messages",
    description=(
        "# **Multimodal OCR Granite Vision**\n\n"
        "Enter a command in the text input (with optional file uploads) using one of the following prefixes:\n\n"
        "- **@rag**: For retrieval-augmented generation (e.g. PDFs, documents).\n"
        "- **@granite**: For image understanding using Granite Vision.\n"
        "- **@video-infer**: For video understanding (video is downsampled into frames).\n\n"
        "For example:\n```\n@rag What is the revenue trend?\n```\n```\n@granite Describe this image\n```\n```\n@video-infer Summarize the event in this video\n```"
    ),
    fill_height=True,
    textbox=gr.MultimodalTextbox(
        label="Query Input",
        file_types=["image", "video", "pdf"],
        file_count="multiple",
        placeholder="@rag, @granite, or @video-infer followed by your prompt"
    ),
    stop_btn="Stop Generation",
    multimodal=True,
)

if __name__ == "__main__":
    demo.queue(max_size=20).launch()