File size: 2,135 Bytes
52a881a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import argparse
from pathlib import Path

try:
    from .model_utils import (
        DEFAULT_MODEL_PATH,
        QuantizedSkinGPTModel,
        build_single_turn_messages,
    )
except ImportError:
    from model_utils import (
        DEFAULT_MODEL_PATH,
        QuantizedSkinGPTModel,
        build_single_turn_messages,
    )


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="SkinGPT-R1 INT4 multi-turn chat")
    parser.add_argument("--model_path", type=str, default=DEFAULT_MODEL_PATH)
    parser.add_argument("--image", type=str, required=True, help="Path to initial image")
    return parser


def main() -> None:
    args = build_parser().parse_args()

    if not Path(args.image).exists():
        print(f"Error: Image {args.image} not found.")
        return

    model = QuantizedSkinGPTModel(args.model_path)
    history = build_single_turn_messages(
        args.image,
        "Please analyze this image.",
        system_prompt="You are a professional AI dermatology assistant. Analyze the skin condition carefully.",
    )

    print("\n=== SkinGPT-R1 INT4 Chat (Type 'exit' to quit) ===")
    print(f"Image loaded: {args.image}")

    print("\nModel is thinking...", end="", flush=True)
    response = model.generate_response(history)
    print(f"\rAssistant: {response}\n")
    history.append({"role": "assistant", "content": [{"type": "text", "text": response}]})

    while True:
        try:
            user_input = input("User: ")
            if user_input.lower() in ["exit", "quit"]:
                break
            if not user_input.strip():
                continue

            history.append({"role": "user", "content": [{"type": "text", "text": user_input}]})
            print("Model is thinking...", end="", flush=True)
            response = model.generate_response(history)
            print(f"\rAssistant: {response}\n")
            history.append({"role": "assistant", "content": [{"type": "text", "text": response}]})
        except KeyboardInterrupt:
            break


if __name__ == "__main__":
    main()