File size: 3,203 Bytes
de6d894
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import gradio as gr

MODEL_ID = "Sourabh2/qwen-fashion-assistant-merged"

print("πŸš€ Loading model:", MODEL_ID)

tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
model = AutoModelForCausalLM.from_pretrained(
    MODEL_ID,
    torch_dtype=torch.float16,
    device_map="auto"
)
model.eval()
print(f"βœ“ Model loaded on: {model.device}")


SYSTEM_PROMPT = (
    "You are a professional fashion shop consultant. "
    "Provide helpful, friendly, and knowledgeable advice about fashion, clothing, styling, and shopping."
)


def generate_response(message, history):
    """Chat function for Gradio interface"""
    messages = [{"role": "system", "content": SYSTEM_PROMPT}]

    # Add previous chat
    for human, ai in history:
        messages.append({"role": "user", "content": human})
        messages.append({"role": "assistant", "content": ai})

    # Add current user message
    messages.append({"role": "user", "content": message})

    # Tokenize input
    inputs = tokenizer.apply_chat_template(
        messages,
        tokenize=True,
        add_generation_prompt=True,
        return_tensors="pt"
    ).to(model.device)

    attention_mask = torch.ones_like(inputs)

    with torch.no_grad():
        outputs = model.generate(
            input_ids=inputs,
            attention_mask=attention_mask,
            max_new_tokens=512,
            temperature=0.6,
            top_p=0.85,
            repetition_penalty=1.1,
            do_sample=True,
            pad_token_id=tokenizer.eos_token_id,
            use_cache=True
        )

    response = tokenizer.decode(outputs[0], skip_special_tokens=True)

    # Try to clean extra tokens if model outputs full conversation
    if "assistant" in response:
        response = response.split("assistant")[-1].strip()

    return response


# --- Gradio UI ---
css = """
#chatbot {
    height: 550px !important;
}
footer {
    display: none !important;
}
"""

examples = [
    ["What color shirt goes well with navy blue pants?"],
    ["I have a job interview tomorrow. What should I wear?"],
    ["How do I style a black leather jacket?"],
    ["What are the fashion trends for summer 2025?"],
    ["Can I wear brown shoes with a grey suit?"],
    ["What's a good outfit for a casual date?"],
]

with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # πŸ‘” AI Fashion Assistant
        Welcome to your personal style consultant powered by **Qwen2.5-3B**  
        Ask me anything about fashion, styling, or outfits!
        """
    )

    chatbot = gr.Chatbot(label="Chat with your fashion consultant", height=500)
    msg = gr.Textbox(
        placeholder="Type your question about fashion or style...",
        label="Your Message"
    )

    clear = gr.Button("Clear Chat")

    def user_chat(user_message, history):
        response = generate_response(user_message, history)
        history.append((user_message, response))
        return history, ""

    msg.submit(user_chat, [msg, chatbot], [chatbot, msg])
    clear.click(lambda: None, None, chatbot, queue=False)

    gr.Examples(examples, inputs=msg)

demo.launch()