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 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()