File size: 14,894 Bytes
f64528c
 
 
 
13b7b20
f64528c
 
13b7b20
 
 
 
 
f64528c
13b7b20
f64528c
13b7b20
 
 
f64528c
13b7b20
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
f64528c
13b7b20
 
f64528c
 
 
 
13b7b20
 
 
f64528c
13b7b20
f64528c
13b7b20
f64528c
 
 
 
 
 
 
13b7b20
 
 
 
 
 
 
 
f64528c
13b7b20
f64528c
13b7b20
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
13b7b20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f64528c
 
13b7b20
 
 
 
 
 
 
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
import gradio as gr
import torch
from PIL import Image
import requests
from io import BytesIO
import json
import time
import os
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoConfig
from transformers import CLIPVisionModel, CLIPImageProcessor
import warnings
warnings.filterwarnings("ignore")

print("πŸš€ Starting LLaVA deployment...")

# Check GPU availability
device = "cuda" if torch.cuda.is_available() else "cpu"
print(f"πŸ’» Using device: {device}")

# Global variables for model components
tokenizer = None
model = None
image_processor = None
vision_tower = None

def load_model():
    """Load LLaVA model components"""
    global tokenizer, model, image_processor, vision_tower
    
    try:
        print("πŸ“¦ Loading tokenizer...")
        # Use the smaller 7B model for free tier
        model_path = "liuhaotian/llava-v1.5-7b"
        
        tokenizer = AutoTokenizer.from_pretrained(model_path)
        
        print("🧠 Loading language model...")
        model = AutoModelForCausalLM.from_pretrained(
            model_path,
            torch_dtype=torch.float16 if device == "cuda" else torch.float32,
            low_cpu_mem_usage=True,
            device_map="auto" if device == "cuda" else None
        )
        
        print("πŸ‘οΈ Loading vision components...")
        # Load vision tower
        vision_tower = CLIPVisionModel.from_pretrained("openai/clip-vit-large-patch14-336")
        image_processor = CLIPImageProcessor.from_pretrained("openai/clip-vit-large-patch14-336")
        
        if device == "cuda":
            vision_tower = vision_tower.to(device)
        
        print("βœ… Model loaded successfully!")
        return True
        
    except Exception as e:
        print(f"❌ Error loading model: {str(e)}")
        return False

def process_image(image):
    """Process image for the model"""
    if image is None:
        return None
    
    try:
        # Convert to RGB if needed
        if image.mode != 'RGB':
            image = image.convert('RGB')
        
        # Process image
        image_tensor = image_processor.preprocess(image, return_tensors='pt')['pixel_values']
        
        if device == "cuda":
            image_tensor = image_tensor.to(device)
        
        # Get image features
        with torch.no_grad():
            image_features = vision_tower(image_tensor).last_hidden_state
        
        return image_features
        
    except Exception as e:
        print(f"Error processing image: {str(e)}")
        return None

def generate_response(message, image=None, system_prompt="", max_tokens=1024, temperature=0.7):
    """Generate response using LLaVA"""
    global tokenizer, model, image_processor, vision_tower
    
    if model is None:
        return "❌ Model not loaded. Please wait for initialization."
    
    try:
        # Process image if provided
        image_features = None
        if image is not None:
            image_features = process_image(image)
            if image_features is None:
                return "❌ Error processing image."
        
        # Prepare prompt
        if system_prompt:
            full_prompt = f"System: {system_prompt}\n\nUser: {message}\n\nAssistant:"
        else:
            if image is not None:
                full_prompt = f"USER: <image>\n{message}\nASSISTANT:"
            else:
                full_prompt = f"USER: {message}\nASSISTANT:"
        
        # Tokenize
        inputs = tokenizer(full_prompt, return_tensors="pt")
        
        if device == "cuda":
            inputs = {k: v.to(device) for k, v in inputs.items()}
        
        # Generate
        with torch.no_grad():
            if image_features is not None:
                # For multimodal input, we need to handle image features
                # This is a simplified version - real LLaVA has more complex integration
                outputs = model.generate(
                    **inputs,
                    max_new_tokens=max_tokens,
                    temperature=temperature,
                    do_sample=True,
                    pad_token_id=tokenizer.eos_token_id
                )
            else:
                # Text-only generation
                outputs = model.generate(
                    **inputs,
                    max_new_tokens=max_tokens,
                    temperature=temperature,
                    do_sample=True,
                    pad_token_id=tokenizer.eos_token_id
                )
        
        # Decode response
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        
        # Clean up response (remove the input prompt)
        response = response[len(full_prompt):].strip()
        
        return response
        
    except Exception as e:
        return f"❌ Error generating response: {str(e)}"

def api_endpoint(request_json):
    """API endpoint for programmatic access"""
    try:
        data = json.loads(request_json)
        
        message = data.get("message", "")
        system_prompt = data.get("system_prompt", "")
        image_url = data.get("image_url", None)
        max_tokens = int(data.get("max_tokens", 1024))
        temperature = float(data.get("temperature", 0.7))
        
        # Process image if URL provided
        image = None
        if image_url:
            try:
                response = requests.get(image_url, timeout=10)
                if response.status_code == 200:
                    image = Image.open(BytesIO(response.content))
            except Exception as e:
                return json.dumps({"error": f"Failed to load image: {str(e)}"})
        
        # Generate response
        response_text = generate_response(
            message=message,
            image=image,
            system_prompt=system_prompt,
            max_tokens=max_tokens,
            temperature=temperature
        )
        
        # Return API response
        return json.dumps({
            "id": f"chatcmpl-{int(time.time())}",
            "object": "chat.completion", 
            "created": int(time.time()),
            "model": "llava-v1.5-7b",
            "choices": [{
                "message": {
                    "role": "assistant",
                    "content": response_text
                },
                "index": 0,
                "finish_reason": "stop"
            }],
            "usage": {
                "prompt_tokens": 0,  # Simplified
                "completion_tokens": 0,  # Simplified
                "total_tokens": 0  # Simplified
            }
        })
        
    except Exception as e:
        return json.dumps({"error": str(e)})

# Initialize model on startup
print("πŸ”„ Initializing model...")
model_loaded = load_model()

# Create Gradio interface
with gr.Blocks(title="LLaVA - Large Language and Vision Assistant", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ¦™ LLaVA - Large Language and Vision Assistant
    
    An open-source chatbot trained by fine-tuning LLaMA/Vicuna on GPT-generated multimodal instruction-following data.
    
    **Features:**
    - πŸ’¬ Text-based conversation
    - πŸ–ΌοΈ Image understanding and description
    - πŸ”§ API endpoint for integration
    """)
    
    with gr.Tab("πŸ’¬ Chat Interface"):
        with gr.Row():
            with gr.Column(scale=1):
                image_input = gr.Image(
                    type="pil", 
                    label="πŸ“Έ Upload Image (Optional)",
                    height=300
                )
                system_prompt = gr.Textbox(
                    label="🎯 System Prompt (Optional)",
                    placeholder="You are a helpful assistant that can analyze images...",
                    lines=2
                )
                
            with gr.Column(scale=2):
                chatbot = gr.Chatbot(
                    label="πŸ’­ Conversation",
                    height=400
                )
                
                msg = gr.Textbox(
                    label="✍️ Your Message",
                    placeholder="Type your message here... You can ask about the uploaded image!",
                    lines=2
                )
                
                with gr.Row():
                    submit_btn = gr.Button("πŸš€ Send", variant="primary")
                    clear_btn = gr.Button("πŸ—‘οΈ Clear", variant="secondary")
        
        with gr.Accordion("βš™οΈ Advanced Settings", open=False):
            max_tokens = gr.Slider(
                minimum=1, 
                maximum=2048, 
                value=1024, 
                step=1,
                label="πŸ“ Max Tokens"
            )
            temperature = gr.Slider(
                minimum=0.1, 
                maximum=2.0, 
                value=0.7, 
                step=0.1,
                label="🌑️ Temperature"
            )
    
    with gr.Tab("πŸ”Œ API Documentation"):
        gr.Markdown("""
        ## API Endpoint Usage
        
        **Endpoint**: `https://your-space-name.hf.space/api/predict`
        
        **Method**: POST
        
        ### Request Format:
        ```json
        {
            "data": [
                "{
                    \"message\": \"Describe this image in detail\",
                    \"system_prompt\": \"You are a helpful assistant\",
                    \"image_url\": \"https://example.com/image.jpg\",
                    \"max_tokens\": 1024,
                    \"temperature\": 0.7
                }"
            ]
        }
        ```
        
        ### Response Format:
        ```json
        {
            "data": [
                "{
                    \"id\": \"chatcmpl-123456789\",
                    \"object\": \"chat.completion\",
                    \"created\": 1683123456,
                    \"model\": \"llava-v1.5-7b\",
                    \"choices\": [
                        {
                            \"message\": {
                                \"role\": \"assistant\",
                                \"content\": \"This image shows...\"
                            },
                            \"index\": 0,
                            \"finish_reason\": \"stop\"
                        }
                    ]
                }"
            ]
        }
        ```
        
        ### Python Client Example:
        ```python
        import requests
        import json
        
        def query_llava(message, image_url=None, system_prompt=""):
            payload = {
                "data": [json.dumps({
                    "message": message,
                    "image_url": image_url,
                    "system_prompt": system_prompt,
                    "max_tokens": 1024,
                    "temperature": 0.7
                })]
            }
            
            response = requests.post(
                "https://your-space-name.hf.space/api/predict",
                json=payload
            )
            
            if response.status_code == 200:
                result = response.json()
                api_response = json.loads(result["data"][0])
                return api_response["choices"][0]["message"]["content"]
            else:
                return f"Error: {response.status_code}"
        
        # Example usage
        result = query_llava(
            "What do you see in this image?",
            image_url="https://example.com/image.jpg"
        )
        print(result)
        ```
        """)
        
        # API testing interface
        gr.Markdown("### πŸ§ͺ Test API")
        api_input = gr.Textbox(
            label="πŸ“ API Request (JSON)",
            placeholder='{"message": "Hello!", "max_tokens": 1024}',
            lines=4
        )
        api_output = gr.Textbox(
            label="πŸ“€ API Response",
            lines=8
        )
        api_test_btn = gr.Button("πŸ§ͺ Test API", variant="primary")
    
    with gr.Tab("ℹ️ About"):
        gr.Markdown("""
        ## About LLaVA
        
        **LLaVA (Large Language and Vision Assistant)** is an open-source multimodal AI assistant that combines:
        
        - 🧠 **Language Understanding**: Based on Vicuna/LLaMA architecture
        - πŸ‘οΈ **Vision Capabilities**: Uses CLIP vision encoder
        - πŸ”— **Multimodal Integration**: Connects vision and language seamlessly
        
        ### Key Features:
        - **Visual Question Answering**: Ask questions about images
        - **Image Description**: Get detailed descriptions of uploaded images  
        - **General Conversation**: Chat about any topic
        - **API Integration**: Easy integration with your applications
        
        ### Model Information:
        - **Base Model**: LLaVA-v1.5-7B
        - **Vision Encoder**: CLIP ViT-L/14@336px
        - **Language Model**: Vicuna-7B
        - **Training Data**: LLaVA-Instruct-150K
        
        ### Citation:
        ```
        @misc{liu2023llava,
            title={Visual Instruction Tuning}, 
            author={Haotian Liu and Chunyuan Li and Qingyang Wu and Yong Jae Lee},
            year={2023},
            eprint={2304.08485},
            archivePrefix={arXiv},
            primaryClass={cs.CV}
        }
        ```
        
        **GitHub**: [https://github.com/haotian-liu/LLaVA](https://github.com/haotian-liu/LLaVA)
        """)
    
    # Event handlers
    def respond(message, chat_history, image, system_prompt, max_tokens, temperature):
        if not message.strip():
            return "", chat_history
        
        # Add user message to chat
        chat_history.append([message, None])
        
        # Generate response
        response = generate_response(
            message=message,
            image=image,
            system_prompt=system_prompt if system_prompt.strip() else "",
            max_tokens=int(max_tokens),
            temperature=temperature
        )
        
        # Add assistant response to chat
        chat_history[-1][1] = response
        
        return "", chat_history
    
    def clear_chat():
        return None, []
    
    # Connect event handlers
    submit_btn.click(
        respond,
        [msg, chatbot, image_input, system_prompt, max_tokens, temperature],
        [msg, chatbot]
    )
    
    msg.submit(
        respond,
        [msg, chatbot, image_input, system_prompt, max_tokens, temperature],
        [msg, chatbot]
    )
    
    clear_btn.click(clear_chat, outputs=[chatbot, msg])
    
    api_test_btn.click(api_endpoint, inputs=api_input, outputs=api_output)
    
    # Add API endpoint
    api_interface = gr.Interface(
        fn=api_endpoint,
        inputs=gr.Textbox(),
        outputs=gr.Textbox(),
        api_name="predict"
    )

# Launch the app
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        share=False
    )