File size: 2,801 Bytes
b6d4cb4
7f8962b
90ac3e9
7f8962b
3975953
90ac3e9
 
 
 
 
7f8962b
90ac3e9
7f8962b
 
 
3975953
90ac3e9
 
 
7f8962b
 
 
 
 
 
 
897cd99
7f8962b
 
 
897cd99
7f8962b
3975953
7f8962b
 
 
 
 
897cd99
7f8962b
 
 
 
 
 
 
 
897cd99
7f8962b
 
897cd99
7f8962b
3975953
7f8962b
 
 
 
 
 
 
 
897cd99
7f8962b
 
 
 
 
 
 
 
897cd99
7f8962b
 
 
 
897cd99
7f8962b
 
 
 
 
 
 
897cd99
7f8962b
 
 
 
 
 
 
 
 
 
897cd99
7f8962b
 
 
 
897cd99
7f8962b
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
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
import torch

# Load base model
base_model_id = "unsloth/Qwen2.5-3B-Instruct"
adapter_model_id = "Sourabh2/qwen-fashion-assistant"

tokenizer = AutoTokenizer.from_pretrained(base_model_id)
model = AutoModelForCausalLM.from_pretrained(
    base_model_id,
    torch_dtype=torch.float16,
    device_map="auto"
)

# Load LoRA adapters
model = PeftModel.from_pretrained(model, adapter_model_id)

def chat(message, history):
    messages = [
        {
            "role": "system",
            "content": "You are a professional fashion shop consultant. Provide helpful, friendly, and knowledgeable advice about fashion, clothing, styling, and shopping."
        }
    ]

    for user_msg, assistant_msg in history:
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": assistant_msg})

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

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

    outputs = model.generate(
        inputs,
        max_new_tokens=1024,
        temperature=0.6,
        top_p=0.85,
        repetition_penalty=1.1,
        do_sample=True
    )

    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    response = response.split("assistant")[-1].strip()

    return response

css = """
#chatbot {
    height: 600px;
}
.message {
    font-size: 16px !important;
}
"""

with gr.Blocks(css=css, theme=gr.themes.Soft()) as demo:
    gr.Markdown(
        """
        # πŸ‘” AI Fashion Assistant
        ### Your Personal Style Consultant powered by Qwen2.5-3B
        Ask me anything about fashion, styling, outfits, colors, trends, and shopping advice!
        """
    )

    chatbot = gr.Chatbot(
        elem_id="chatbot",
        bubble_full_width=False
    )

    with gr.Row():
        msg = gr.Textbox(
            placeholder="Ask me about fashion... (e.g., 'What should I wear to a wedding?')",
            show_label=False,
            scale=9
        )
        submit = gr.Button("Send", scale=1, variant="primary")

    gr.Examples(
        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?"
        ],
        inputs=msg
    )

    msg.submit(chat, [msg, chatbot], chatbot)
    submit.click(chat, [msg, chatbot], chatbot)
    msg.submit(lambda: "", None, msg)
    submit.click(lambda: "", None, msg)

demo.launch()