File size: 1,668 Bytes
e98a02a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from typing import Optional
import openai
from .image import encode_image_to_base64

def analyze_image(
    image,
    endpoint_url: str,
    api_key: str,
    selected_model: str,
    prompt: str,
    system_prompt: Optional[str] = None,
    temperature: float = 1.0
):
    """Analyze image using LiteLLM with custom prompts"""
    if not endpoint_url or not api_key:
        return "Please configure both the endpoint URL and API key"
    
    if not selected_model:
        return "Please select a model"
    
    print(f"Analyzing image with model: {selected_model}")
    
    try:
        client = openai.OpenAI(
            api_key=api_key,
            base_url=endpoint_url 
        )

        # Convert image to base64
        base64_image = encode_image_to_base64(image)
        
        # Prepare messages
        messages = []
        
        # Add system prompt if provided
        if system_prompt:
            messages.append({
                "role": "system",
                "content": system_prompt
            })
        
        # Add user message with image
        messages.append({
            "role": "user",
            "content": [
                {"type": "text", "text": prompt},
                {"type": "image_url", "image_url": base64_image}
            ]
        })
        
        # Call LiteLLM with custom endpoint
        response = client.chat.completions.create(
            model=selected_model,
            messages=messages,
            temperature=temperature
        )
        
        return str(response.choices[0].message.content)
            
    except Exception as e:
        return f"Error analyzing image: {str(e)}"