Spaces:
				
			
			
	
			
			
		Sleeping
		
	
	
	
			
			
	
	
	
	
		
		
		Sleeping
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | 
         @@ -0,0 +1,71 @@ 
     | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
|
| 
         | 
| 
         | 
|
| 1 | 
         
            +
            import gradio as gr
         
     | 
| 2 | 
         
            +
            import torch
         
     | 
| 3 | 
         
            +
            from transformers import AutoModelForCausalLM, AutoTokenizer
         
     | 
| 4 | 
         
            +
             
     | 
| 5 | 
         
            +
            # Load model and tokenizer
         
     | 
| 6 | 
         
            +
            model_name = "Qwen/Qwen2.5-3B-Instruct"
         
     | 
| 7 | 
         
            +
             
     | 
| 8 | 
         
            +
            model = AutoModelForCausalLM.from_pretrained(
         
     | 
| 9 | 
         
            +
                model_name,
         
     | 
| 10 | 
         
            +
                torch_dtype="auto",
         
     | 
| 11 | 
         
            +
                device_map="auto"
         
     | 
| 12 | 
         
            +
            )
         
     | 
| 13 | 
         
            +
            tokenizer = AutoTokenizer.from_pretrained(model_name)
         
     | 
| 14 | 
         
            +
             
     | 
| 15 | 
         
            +
            # Chat history
         
     | 
| 16 | 
         
            +
            chat_history = []
         
     | 
| 17 | 
         
            +
             
     | 
| 18 | 
         
            +
            # System prompt
         
     | 
| 19 | 
         
            +
            SYSTEM_PROMPT = "You are Qwen/Qwen2.5-3B-Instruct, created by Alibaba Cloud. You are a helpful assistant."
         
     | 
| 20 | 
         
            +
             
     | 
| 21 | 
         
            +
            def generate_response(user_input, history):
         
     | 
| 22 | 
         
            +
                # Build message list
         
     | 
| 23 | 
         
            +
                messages = [{"role": "system", "content": SYSTEM_PROMPT}]
         
     | 
| 24 | 
         
            +
                for user_msg, bot_msg in history:
         
     | 
| 25 | 
         
            +
                    messages.append({"role": "user", "content": user_msg})
         
     | 
| 26 | 
         
            +
                    messages.append({"role": "assistant", "content": bot_msg})
         
     | 
| 27 | 
         
            +
                messages.append({"role": "user", "content": user_input})
         
     | 
| 28 | 
         
            +
             
     | 
| 29 | 
         
            +
                # Apply chat template
         
     | 
| 30 | 
         
            +
                prompt_text = tokenizer.apply_chat_template(
         
     | 
| 31 | 
         
            +
                    messages,
         
     | 
| 32 | 
         
            +
                    tokenize=False,
         
     | 
| 33 | 
         
            +
                    add_generation_prompt=True
         
     | 
| 34 | 
         
            +
                )
         
     | 
| 35 | 
         
            +
             
     | 
| 36 | 
         
            +
                # Tokenize
         
     | 
| 37 | 
         
            +
                model_inputs = tokenizer([prompt_text], return_tensors="pt").to(model.device)
         
     | 
| 38 | 
         
            +
             
     | 
| 39 | 
         
            +
                # Generate response
         
     | 
| 40 | 
         
            +
                generated_ids = model.generate(
         
     | 
| 41 | 
         
            +
                    **model_inputs,
         
     | 
| 42 | 
         
            +
                    max_new_tokens=512,
         
     | 
| 43 | 
         
            +
                    do_sample=True,
         
     | 
| 44 | 
         
            +
                    temperature=0.7,
         
     | 
| 45 | 
         
            +
                    top_p=0.9
         
     | 
| 46 | 
         
            +
                )
         
     | 
| 47 | 
         
            +
             
     | 
| 48 | 
         
            +
                # Only return new tokens
         
     | 
| 49 | 
         
            +
                new_tokens = generated_ids[0][model_inputs.input_ids.shape[-1]:]
         
     | 
| 50 | 
         
            +
                response = tokenizer.decode(new_tokens, skip_special_tokens=True)
         
     | 
| 51 | 
         
            +
             
     | 
| 52 | 
         
            +
                # Update chat history
         
     | 
| 53 | 
         
            +
                history.append((user_input, response))
         
     | 
| 54 | 
         
            +
                return history, history
         
     | 
| 55 | 
         
            +
             
     | 
| 56 | 
         
            +
            # Launch Gradio Chatbot UI
         
     | 
| 57 | 
         
            +
            chatbot_ui = gr.ChatInterface(
         
     | 
| 58 | 
         
            +
                fn=generate_response,
         
     | 
| 59 | 
         
            +
                title="🧠 Qwen 2.5 3B - Chatbot",
         
     | 
| 60 | 
         
            +
                description="A simple chatbot interface powered by Qwen2.5-3B-Instruct (Alibaba Cloud).",
         
     | 
| 61 | 
         
            +
                theme="soft",
         
     | 
| 62 | 
         
            +
                examples = [
         
     | 
| 63 | 
         
            +
                "How can virtual reality (VR) influence consumer behavior towards sustainability?",
         
     | 
| 64 | 
         
            +
                "What impact does sustainable packaging have on consumer purchasing decisions?",
         
     | 
| 65 | 
         
            +
                "In what ways can education promote more sustainable consumer behaviors?"
         
     | 
| 66 | 
         
            +
            ],
         
     | 
| 67 | 
         
            +
             
     | 
| 68 | 
         
            +
            )
         
     | 
| 69 | 
         
            +
             
     | 
| 70 | 
         
            +
            if __name__ == "__main__":
         
     | 
| 71 | 
         
            +
                chatbot_ui.launch()
         
     |