File size: 13,387 Bytes
6b36184
 
 
2cf117f
6b36184
 
55b2cb1
0b96209
f1199d3
6b36184
 
2cf117f
f1199d3
 
 
2cf117f
1c943af
 
 
 
0b96209
f1199d3
0b96209
6b36184
5599f5a
f1199d3
1c943af
 
f1199d3
1c943af
5599f5a
 
 
 
 
 
 
 
 
 
d5d8986
 
 
 
 
 
5599f5a
 
6b36184
 
5599f5a
 
 
 
 
 
 
 
 
 
 
6b36184
5599f5a
 
 
 
 
 
d5d8986
 
 
 
 
 
6b36184
5599f5a
6b36184
 
5599f5a
 
 
 
 
 
 
 
 
 
 
6b36184
5599f5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d8986
 
 
 
 
 
5599f5a
 
 
6b36184
 
5599f5a
 
 
1c943af
5599f5a
 
 
 
f1199d3
5599f5a
f1199d3
1c943af
 
 
f1199d3
 
1c943af
e670b79
6b36184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0b96209
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6b36184
0b96209
d5d8986
c3c9d97
 
 
 
 
 
 
d5d8986
 
0b96209
d5d8986
 
5599f5a
d5d8986
c3c9d97
2ba99d9
0b96209
d5d8986
 
 
 
 
 
 
 
 
 
 
 
2ba99d9
0b96209
7077007
 
 
 
 
 
d5d8986
 
7077007
 
d5d8986
 
 
0b96209
 
7077007
6b36184
 
d5d8986
6b36184
 
7077007
0b96209
f1199d3
d5d8986
 
 
 
6b36184
 
 
 
 
1c943af
6b36184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
563f88c
6b36184
 
 
 
 
 
 
 
 
 
 
 
 
d5d8986
 
6b36184
 
e670b79
d5d8986
6b36184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5599f5a
6b36184
 
d5d8986
 
 
 
6b36184
 
 
 
 
 
 
 
 
 
 
 
 
d5d8986
 
6b36184
 
 
 
563f88c
6b36184
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d5d8986
6b36184
 
 
 
2ba99d9
d5d8986
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from pydantic import BaseModel
from PIL import Image
from io import BytesIO
import base64
import torch
import re
import logging
import asyncio
from contextlib import asynccontextmanager

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Initialize global variables
model = None
processor = None
tokenizer = None
model_name = "microsoft/GUI-Actor-2B-Qwen2-VL"
model_loaded = False

async def load_model():
    """Load model with proper error handling and fallback strategies"""
    global model, processor, tokenizer, model_loaded
    
    try:
        logger.info("Starting model loading...")
        
        # Try specific Qwen2VL classes first
        try:
            logger.info("Attempting to load with Qwen2VL specific classes...")
            from transformers import Qwen2VLProcessor, Qwen2VLForConditionalGeneration
            
            processor = Qwen2VLProcessor.from_pretrained(
                model_name, 
                trust_remote_code=True
            )
            
            # Configure padding for processor
            if hasattr(processor, 'tokenizer'):
                processor.tokenizer.padding_side = "left"  # Important for Qwen2-VL
                if processor.tokenizer.pad_token is None:
                    processor.tokenizer.pad_token = processor.tokenizer.eos_token
            
            model = Qwen2VLForConditionalGeneration.from_pretrained(
                model_name,
                torch_dtype=torch.float32,
                device_map=None,  # CPU only
                trust_remote_code=True,
                low_cpu_mem_usage=True
            ).eval()
            
            logger.info("Successfully loaded with Qwen2VL specific classes")
            
        except Exception as e1:
            logger.warning(f"Failed with Qwen2VL classes: {e1}")
            logger.info("Trying AutoProcessor and AutoModel fallback...")
            
            try:
                from transformers import AutoProcessor, AutoModel
                
                processor = AutoProcessor.from_pretrained(
                    model_name, 
                    trust_remote_code=True
                )
                
                # Configure padding for processor
                if hasattr(processor, 'tokenizer'):
                    processor.tokenizer.padding_side = "left"
                    if processor.tokenizer.pad_token is None:
                        processor.tokenizer.pad_token = processor.tokenizer.eos_token
                
                model = AutoModel.from_pretrained(
                    model_name,
                    torch_dtype=torch.float32,
                    device_map=None,
                    trust_remote_code=True,
                    low_cpu_mem_usage=True
                ).eval()
                
                logger.info("Successfully loaded with Auto classes")
                
            except Exception as e2:
                logger.warning(f"Failed with Auto classes: {e2}")
                logger.info("Trying generic transformers approach...")
                
                # Last fallback - try loading as generic model
                from transformers import AutoConfig, AutoTokenizer
                import transformers
                
                config = AutoConfig.from_pretrained(model_name, trust_remote_code=True)
                logger.info(f"Model config type: {type(config)}")
                
                # Try to find the right model class
                if hasattr(transformers, 'Qwen2VLForConditionalGeneration'):
                    ModelClass = getattr(transformers, 'Qwen2VLForConditionalGeneration')
                elif hasattr(transformers, 'AutoModelForVision2Seq'):
                    ModelClass = getattr(transformers, 'AutoModelForVision2Seq')
                else:
                    raise Exception("No suitable model class found")
                
                processor = AutoProcessor.from_pretrained(
                    model_name, 
                    trust_remote_code=True
                )
                
                # Configure padding
                if hasattr(processor, 'tokenizer'):
                    processor.tokenizer.padding_side = "left"
                    if processor.tokenizer.pad_token is None:
                        processor.tokenizer.pad_token = processor.tokenizer.eos_token
                
                model = ModelClass.from_pretrained(
                    model_name,
                    config=config,
                    torch_dtype=torch.float32,
                    device_map=None,
                    trust_remote_code=True,
                    low_cpu_mem_usage=True
                ).eval()
        
        # Verify processor and model are loaded
        if processor is None or model is None:
            raise Exception("Failed to load processor or model")
            
        tokenizer = processor.tokenizer
        logger.info("Model and processor loaded successfully!")
        model_loaded = True
        return True
        
    except Exception as e:
        logger.error(f"Error loading model: {e}")
        model_loaded = False
        return False

@asynccontextmanager
async def lifespan(app: FastAPI):
    # Startup
    logger.info("Starting up GUI-Actor API...")
    await load_model()
    yield
    # Shutdown
    logger.info("Shutting down GUI-Actor API...")

# Initialize FastAPI app with lifespan
app = FastAPI(
    title="GUI-Actor API", 
    version="1.0.0",
    lifespan=lifespan
)

class Base64Request(BaseModel):
    image_base64: str
    instruction: str

def extract_coordinates(text):
    """
    Extract coordinates from model output text
    """
    # Pattern untuk mencari koordinat dalam berbagai format
    patterns = [
        r'click\s*\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*\)',  # click(x, y)
        r'\[\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*\]',          # [x, y]
        r'(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)',                    # x, y
        r'point:\s*\(\s*(\d+(?:\.\d+)?)\s*,\s*(\d+(?:\.\d+)?)\s*\)', # point: (x, y)
    ]
    
    for pattern in patterns:
        matches = re.findall(pattern, text.lower())
        if matches:
            try:
                x, y = float(matches[0][0]), float(matches[0][1])
                # Normalize jika koordinat > 1 (asumsi pixel coordinates)
                if x > 1 or y > 1:
                    # Asumsi resolusi 1920x1080 untuk normalisasi
                    x = x / 1920 if x > 1 else x
                    y = y / 1080 if y > 1 else y
                return [(x, y)]
            except (ValueError, IndexError):
                continue
    
    # Default ke center jika tidak ditemukan
    return [(0.5, 0.5)]

def cpu_inference(conversation, model, tokenizer, processor):
    try:
        # Apply chat template
        prompt = processor.apply_chat_template(
            conversation, 
            tokenize=False,
            add_generation_prompt=True
        )

        image = conversation[1]["content"][0]["image"]
        
        # FIXED: Process inputs dengan padding yang benar
        inputs = processor(
            text=[prompt],  # Wrap dalam list untuk batch processing
            images=[image],  # Wrap dalam list untuk batch processing
            return_tensors="pt",
            padding=True,  # Enable padding
            truncation=True,
            max_length=512
        )
        
        # FIXED: Pastikan semua tensor memiliki batch dimension yang konsisten
        for key, value in inputs.items():
            if isinstance(value, torch.Tensor):
                logger.debug(f"Input {key} shape: {value.shape}")
        
        # FIXED: Set pad_token_id jika belum ada
        pad_token_id = tokenizer.pad_token_id
        if pad_token_id is None:
            pad_token_id = tokenizer.eos_token_id
        if pad_token_id is None:
            pad_token_id = 0  # Fallback

        with torch.no_grad():
            outputs = model.generate(
                **inputs,
                max_new_tokens=256,
                do_sample=True,
                temperature=0.3,
                top_p=0.8,
                pad_token_id=pad_token_id,
                attention_mask=inputs.get('attention_mask', None)  # FIXED: Explicit attention mask
            )

        # FIXED: Extract generated tokens correctly
        input_length = inputs["input_ids"].shape[1]
        generated_ids = outputs[0][input_length:]
        response = tokenizer.decode(generated_ids, skip_special_tokens=True)
        coordinates = extract_coordinates(response)

        return {
            "topk_points": coordinates,
            "response": response.strip(),
            "success": True
        }

    except Exception as e:
        logger.error(f"Inference error: {e}")
        # FIXED: More detailed error logging
        import traceback
        logger.error(f"Full traceback: {traceback.format_exc()}")
        
        return {
            "topk_points": [(0.5, 0.5)],
            "response": f"Error during inference: {str(e)}",
            "success": False
        }

@app.get("/")
async def root():
    return {
        "message": "GUI-Actor API is running", 
        "status": "healthy",
        "model_loaded": model_loaded,
        "model_name": model_name
    }

@app.post("/click/base64")
async def predict_click_base64(data: Base64Request):
    if not model_loaded:
        raise HTTPException(
            status_code=503,
            detail="Model not loaded properly"
        )
    
    try:
        # Decode base64 to image
        try:
            # Handle data URL format
            if "," in data.image_base64:
                image_data = base64.b64decode(data.image_base64.split(",")[-1])
            else:
                image_data = base64.b64decode(data.image_base64)
        except Exception as e:
            raise HTTPException(status_code=400, detail=f"Invalid base64 image: {e}")
        
        try:
            pil_image = Image.open(BytesIO(image_data)).convert("RGB")
            # FIXED: Log image dimensions for debugging
            logger.debug(f"Image dimensions: {pil_image.size}")
        except Exception as e:
            raise HTTPException(status_code=400, detail=f"Invalid image format: {e}")

        # FIXED: Improved conversation structure
        conversation = [
            {
                "role": "system",
                "content": [
                    {
                        "type": "text",
                        "text": "You are a GUI agent. You are given a task and a screenshot of the screen. You need to perform a series of pyautogui actions to complete the task. Please provide the click coordinates.",
                    }
                ]
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "image",
                        "image": pil_image,
                    },
                    {
                        "type": "text",
                        "text": data.instruction,
                    },
                ],
            },
        ]

        # Run inference
        pred = cpu_inference(conversation, model, tokenizer, processor)
        
        if not pred["success"]:
            logger.warning(f"Inference failed: {pred['response']}")
        
        px, py = pred["topk_points"][0]
        
        return JSONResponse(content={
            "x": round(px, 4), 
            "y": round(py, 4),
            "response": pred["response"],
            "success": pred["success"]
        })
        
    except HTTPException:
        raise
    except Exception as e:
        logger.error(f"Prediction error: {e}")
        import traceback
        logger.error(f"Full traceback: {traceback.format_exc()}")
        raise HTTPException(
            status_code=500,
            detail=f"Internal server error: {str(e)}"
        )

@app.get("/health")
async def health_check():
    return {
        "status": "healthy" if model_loaded else "unhealthy",
        "model": model_name,
        "device": "cpu",
        "torch_dtype": "float32",
        "model_loaded": model_loaded
    }

@app.get("/debug")
async def debug_info():
    """Debug endpoint to check model loading status"""
    import transformers
    available_classes = [attr for attr in dir(transformers) if 'Qwen' in attr or 'VL' in attr]
    
    debug_info = {
        "model_loaded": model_loaded,
        "processor_type": type(processor).__name__ if processor else None,
        "model_type": type(model).__name__ if model else None,
        "available_qwen_classes": available_classes,
        "transformers_version": transformers.__version__
    }
    
    # FIXED: Add tokenizer info for debugging
    if processor and hasattr(processor, 'tokenizer'):
        debug_info.update({
            "tokenizer_type": type(processor.tokenizer).__name__,
            "pad_token": processor.tokenizer.pad_token,
            "pad_token_id": processor.tokenizer.pad_token_id,
            "eos_token": processor.tokenizer.eos_token,
            "eos_token_id": processor.tokenizer.eos_token_id,
            "padding_side": processor.tokenizer.padding_side
        })
    
    return debug_info