File size: 2,241 Bytes
8374b0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, AutoProcessor
from peft import PeftModel
from PIL import Image
import base64
import io

# Load model and tokenizer
@torch.no_grad()
def load_model():
    """Load the ViTCM_LLM model for Traditional Chinese Medicine Tongue diagnosis."""
    tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct")
    processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-32B-Instruct")
    
    base_model = AutoModelForCausalLM.from_pretrained(
        "Qwen/Qwen2.5-VL-32B-Instruct",
        torch_dtype=torch.float16,
        device_map="auto"
    )
    
    model = PeftModel.from_pretrained(base_model, "Mark-CHAE/shezhen")
    return model, tokenizer, processor

# Initialize model
model, tokenizer, processor = load_model()

def query(question: str, image: str) -> str:
    """

    Analyze tongue image for Traditional Chinese Medicine diagnosis.

    

    Args:

        question: The question about the tongue image (e.g., "根据图片判断舌诊内容")

        image: Base64 encoded image string

    

    Returns:

        The TCM diagnosis analysis of the tongue

    """
    try:
        # Decode base64 image
        image_data = base64.b64decode(image)
        image_pil = Image.open(io.BytesIO(image_data))
        
        # Construct prompt for TCM tongue diagnosis
        prompt = f"<|im_start|>user\n<image>\n{question}<|im_end|>\n<|im_start|>assistant\n"
        
        # Process inputs
        inputs = processor(
            text=prompt,
            images=image_pil,
            return_tensors="pt"
        )
        
        # Generate response
        outputs = model.generate(
            **inputs,
            max_length=512,
            temperature=0.7,
            top_p=0.9,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id
        )
        
        # Decode response
        response = tokenizer.decode(outputs[0], skip_special_tokens=True)
        answer = response.split("<|im_start|>assistant")[-1].strip()
        
        return answer
        
    except Exception as e:
        return f"Error processing request: {str(e)}"