File size: 2,183 Bytes
9fb76f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# chat.py
import argparse
import os
from model_utils import SkinGPTModel

def main():
    parser = argparse.ArgumentParser(description="SkinGPT-R1 Multi-turn Chat")
    parser.add_argument("--model_path", type=str, default="../checkpoint")
    parser.add_argument("--image", type=str, required=True, help="Path to initial image")
    args = parser.parse_args()

    # 初始化模型
    bot = SkinGPTModel(args.model_path)

    # 初始化对话历史
    # 系统提示词
    system_prompt = "You are a professional AI dermatology assistant. Analyze the skin condition carefully."
    
    # 构造第一条包含图片的消息
    if not os.path.exists(args.image):
        print(f"Error: Image {args.image} not found.")
        return

    history = [
        {
            "role": "user",
            "content": [
                {"type": "image", "image": args.image},
                {"type": "text", "text": f"{system_prompt}\n\nPlease analyze this image."}
            ]
        }
    ]

    print("\n=== SkinGPT-R1 Chat (Type 'exit' to quit) ===")
    print(f"Image loaded: {args.image}")
    
    # 获取第一轮诊断
    print("\nModel is thinking...", end="", flush=True)
    response = bot.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 = bot.generate_response(history)
            print(f"\rAssistant: {response}\n")

            # 加入助手的新回复
            history.append({"role": "assistant", "content": [{"type": "text", "text": response}]})

        except KeyboardInterrupt:
            break

if __name__ == "__main__":
    main()