Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Create app.py
Browse files
    	
        app.py
    ADDED
    
    | @@ -0,0 +1,221 @@ | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | 
|  | |
| 1 | 
            +
            import gradio as gr
         | 
| 2 | 
            +
            import torch
         | 
| 3 | 
            +
            from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
         | 
| 4 | 
            +
            from threading import Thread
         | 
| 5 | 
            +
            import spaces
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            # Load model and tokenizer
         | 
| 8 | 
            +
            model_id = "openfree/Darwin-Qwen3-4B"
         | 
| 9 | 
            +
            tokenizer = AutoTokenizer.from_pretrained(model_id)
         | 
| 10 | 
            +
            model = AutoModelForCausalLM.from_pretrained(
         | 
| 11 | 
            +
                model_id,
         | 
| 12 | 
            +
                torch_dtype=torch.float16,
         | 
| 13 | 
            +
                device_map="auto",
         | 
| 14 | 
            +
                trust_remote_code=True
         | 
| 15 | 
            +
            )
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            @spaces.GPU
         | 
| 18 | 
            +
            def generate_response(
         | 
| 19 | 
            +
                message,
         | 
| 20 | 
            +
                history,
         | 
| 21 | 
            +
                temperature=0.7,
         | 
| 22 | 
            +
                max_new_tokens=512,
         | 
| 23 | 
            +
                top_p=0.9,
         | 
| 24 | 
            +
                repetition_penalty=1.1,
         | 
| 25 | 
            +
            ):
         | 
| 26 | 
            +
                # Format conversation history
         | 
| 27 | 
            +
                conversation = []
         | 
| 28 | 
            +
                for user, assistant in history:
         | 
| 29 | 
            +
                    conversation.extend([
         | 
| 30 | 
            +
                        {"role": "user", "content": user},
         | 
| 31 | 
            +
                        {"role": "assistant", "content": assistant}
         | 
| 32 | 
            +
                    ])
         | 
| 33 | 
            +
                conversation.append({"role": "user", "content": message})
         | 
| 34 | 
            +
                
         | 
| 35 | 
            +
                # Apply chat template if available
         | 
| 36 | 
            +
                if hasattr(tokenizer, "apply_chat_template"):
         | 
| 37 | 
            +
                    text = tokenizer.apply_chat_template(
         | 
| 38 | 
            +
                        conversation,
         | 
| 39 | 
            +
                        tokenize=False,
         | 
| 40 | 
            +
                        add_generation_prompt=True
         | 
| 41 | 
            +
                    )
         | 
| 42 | 
            +
                else:
         | 
| 43 | 
            +
                    # Fallback formatting
         | 
| 44 | 
            +
                    text = "\n".join([f"User: {message}" if i["role"] == "user" 
         | 
| 45 | 
            +
                                     else f"Assistant: {message}" 
         | 
| 46 | 
            +
                                     for i in conversation])
         | 
| 47 | 
            +
                    text += "\nAssistant: "
         | 
| 48 | 
            +
                
         | 
| 49 | 
            +
                # Tokenize input
         | 
| 50 | 
            +
                inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=2048)
         | 
| 51 | 
            +
                inputs = inputs.to(model.device)
         | 
| 52 | 
            +
                
         | 
| 53 | 
            +
                # Set up streaming
         | 
| 54 | 
            +
                streamer = TextIteratorStreamer(
         | 
| 55 | 
            +
                    tokenizer, 
         | 
| 56 | 
            +
                    timeout=10.0, 
         | 
| 57 | 
            +
                    skip_prompt=True, 
         | 
| 58 | 
            +
                    skip_special_tokens=True
         | 
| 59 | 
            +
                )
         | 
| 60 | 
            +
                
         | 
| 61 | 
            +
                # Generation parameters
         | 
| 62 | 
            +
                gen_kwargs = dict(
         | 
| 63 | 
            +
                    inputs,
         | 
| 64 | 
            +
                    streamer=streamer,
         | 
| 65 | 
            +
                    max_new_tokens=max_new_tokens,
         | 
| 66 | 
            +
                    temperature=temperature,
         | 
| 67 | 
            +
                    top_p=top_p,
         | 
| 68 | 
            +
                    repetition_penalty=repetition_penalty,
         | 
| 69 | 
            +
                    do_sample=True,
         | 
| 70 | 
            +
                    pad_token_id=tokenizer.eos_token_id,
         | 
| 71 | 
            +
                    eos_token_id=tokenizer.eos_token_id,
         | 
| 72 | 
            +
                )
         | 
| 73 | 
            +
                
         | 
| 74 | 
            +
                # Start generation in separate thread
         | 
| 75 | 
            +
                thread = Thread(target=model.generate, kwargs=gen_kwargs)
         | 
| 76 | 
            +
                thread.start()
         | 
| 77 | 
            +
                
         | 
| 78 | 
            +
                # Stream output
         | 
| 79 | 
            +
                response = ""
         | 
| 80 | 
            +
                for new_text in streamer:
         | 
| 81 | 
            +
                    response += new_text
         | 
| 82 | 
            +
                    yield response
         | 
| 83 | 
            +
                
         | 
| 84 | 
            +
                thread.join()
         | 
| 85 | 
            +
             | 
| 86 | 
            +
            # Create Gradio interface
         | 
| 87 | 
            +
            with gr.Blocks(title="Darwin-Qwen3-4B Chat") as demo:
         | 
| 88 | 
            +
                gr.Markdown(
         | 
| 89 | 
            +
                    """
         | 
| 90 | 
            +
                    # 🌱 Darwin-Qwen3-4B Interactive Chat
         | 
| 91 | 
            +
                    
         | 
| 92 | 
            +
                    Test the evolutionary merged model that combines the strengths of instruction-following and reasoning capabilities.
         | 
| 93 | 
            +
                    
         | 
| 94 | 
            +
                    **Model**: [openfree/Darwin-Qwen3-4B](https://huggingface.co/openfree/Darwin-Qwen3-4B)
         | 
| 95 | 
            +
                    
         | 
| 96 | 
            +
                    This model was created using the Darwin A2AP Enhanced v3.2 evolutionary algorithm, merging:
         | 
| 97 | 
            +
                    - Parent 1: Qwen/Qwen3-4B-Instruct-2507
         | 
| 98 | 
            +
                    - Parent 2: Qwen/Qwen3-4B-Thinking-2507
         | 
| 99 | 
            +
                    """
         | 
| 100 | 
            +
                )
         | 
| 101 | 
            +
                
         | 
| 102 | 
            +
                chatbot = gr.Chatbot(
         | 
| 103 | 
            +
                    label="Chat History",
         | 
| 104 | 
            +
                    bubble_full_width=False,
         | 
| 105 | 
            +
                    height=400
         | 
| 106 | 
            +
                )
         | 
| 107 | 
            +
                
         | 
| 108 | 
            +
                with gr.Row():
         | 
| 109 | 
            +
                    msg = gr.Textbox(
         | 
| 110 | 
            +
                        label="Your Message",
         | 
| 111 | 
            +
                        placeholder="Type your message here and press Enter...",
         | 
| 112 | 
            +
                        lines=2,
         | 
| 113 | 
            +
                        scale=4
         | 
| 114 | 
            +
                    )
         | 
| 115 | 
            +
                    submit_btn = gr.Button("Send", scale=1, variant="primary")
         | 
| 116 | 
            +
                
         | 
| 117 | 
            +
                with gr.Accordion("Advanced Settings", open=False):
         | 
| 118 | 
            +
                    temperature = gr.Slider(
         | 
| 119 | 
            +
                        minimum=0.1,
         | 
| 120 | 
            +
                        maximum=1.5,
         | 
| 121 | 
            +
                        value=0.7,
         | 
| 122 | 
            +
                        step=0.1,
         | 
| 123 | 
            +
                        label="Temperature (higher = more creative)"
         | 
| 124 | 
            +
                    )
         | 
| 125 | 
            +
                    max_new_tokens = gr.Slider(
         | 
| 126 | 
            +
                        minimum=64,
         | 
| 127 | 
            +
                        maximum=2048,
         | 
| 128 | 
            +
                        value=512,
         | 
| 129 | 
            +
                        step=64,
         | 
| 130 | 
            +
                        label="Max New Tokens"
         | 
| 131 | 
            +
                    )
         | 
| 132 | 
            +
                    top_p = gr.Slider(
         | 
| 133 | 
            +
                        minimum=0.1,
         | 
| 134 | 
            +
                        maximum=1.0,
         | 
| 135 | 
            +
                        value=0.9,
         | 
| 136 | 
            +
                        step=0.05,
         | 
| 137 | 
            +
                        label="Top-p (nucleus sampling)"
         | 
| 138 | 
            +
                    )
         | 
| 139 | 
            +
                    repetition_penalty = gr.Slider(
         | 
| 140 | 
            +
                        minimum=1.0,
         | 
| 141 | 
            +
                        maximum=1.5,
         | 
| 142 | 
            +
                        value=1.1,
         | 
| 143 | 
            +
                        step=0.05,
         | 
| 144 | 
            +
                        label="Repetition Penalty"
         | 
| 145 | 
            +
                    )
         | 
| 146 | 
            +
                
         | 
| 147 | 
            +
                with gr.Row():
         | 
| 148 | 
            +
                    clear_btn = gr.Button("Clear Chat", variant="secondary")
         | 
| 149 | 
            +
                    
         | 
| 150 | 
            +
                gr.Examples(
         | 
| 151 | 
            +
                    examples=[
         | 
| 152 | 
            +
                        "Explain quantum computing in simple terms.",
         | 
| 153 | 
            +
                        "Write a Python function to find prime numbers.",
         | 
| 154 | 
            +
                        "What are the key differences between machine learning and deep learning?",
         | 
| 155 | 
            +
                        "Suggest a healthy meal plan for a week.",
         | 
| 156 | 
            +
                        "How does photosynthesis work?",
         | 
| 157 | 
            +
                    ],
         | 
| 158 | 
            +
                    inputs=msg,
         | 
| 159 | 
            +
                    label="Example Prompts"
         | 
| 160 | 
            +
                )
         | 
| 161 | 
            +
                
         | 
| 162 | 
            +
                # Event handlers
         | 
| 163 | 
            +
                def user_submit(message, history):
         | 
| 164 | 
            +
                    return "", history + [[message, None]]
         | 
| 165 | 
            +
                
         | 
| 166 | 
            +
                def bot_respond(history, temperature, max_new_tokens, top_p, repetition_penalty):
         | 
| 167 | 
            +
                    message = history[-1][0]
         | 
| 168 | 
            +
                    history[-1][1] = ""
         | 
| 169 | 
            +
                    
         | 
| 170 | 
            +
                    for response in generate_response(
         | 
| 171 | 
            +
                        message, 
         | 
| 172 | 
            +
                        history[:-1],
         | 
| 173 | 
            +
                        temperature,
         | 
| 174 | 
            +
                        max_new_tokens,
         | 
| 175 | 
            +
                        top_p,
         | 
| 176 | 
            +
                        repetition_penalty
         | 
| 177 | 
            +
                    ):
         | 
| 178 | 
            +
                        history[-1][1] = response
         | 
| 179 | 
            +
                        yield history
         | 
| 180 | 
            +
                
         | 
| 181 | 
            +
                msg.submit(
         | 
| 182 | 
            +
                    user_submit, 
         | 
| 183 | 
            +
                    [msg, chatbot], 
         | 
| 184 | 
            +
                    [msg, chatbot]
         | 
| 185 | 
            +
                ).then(
         | 
| 186 | 
            +
                    bot_respond,
         | 
| 187 | 
            +
                    [chatbot, temperature, max_new_tokens, top_p, repetition_penalty],
         | 
| 188 | 
            +
                    chatbot
         | 
| 189 | 
            +
                )
         | 
| 190 | 
            +
                
         | 
| 191 | 
            +
                submit_btn.click(
         | 
| 192 | 
            +
                    user_submit, 
         | 
| 193 | 
            +
                    [msg, chatbot], 
         | 
| 194 | 
            +
                    [msg, chatbot]
         | 
| 195 | 
            +
                ).then(
         | 
| 196 | 
            +
                    bot_respond,
         | 
| 197 | 
            +
                    [chatbot, temperature, max_new_tokens, top_p, repetition_penalty],
         | 
| 198 | 
            +
                    chatbot
         | 
| 199 | 
            +
                )
         | 
| 200 | 
            +
                
         | 
| 201 | 
            +
                clear_btn.click(lambda: None, None, chatbot, queue=False)
         | 
| 202 | 
            +
                
         | 
| 203 | 
            +
                gr.Markdown(
         | 
| 204 | 
            +
                    """
         | 
| 205 | 
            +
                    ---
         | 
| 206 | 
            +
                    ### About Darwin Project
         | 
| 207 | 
            +
                    
         | 
| 208 | 
            +
                    The Darwin Project demonstrates a new paradigm in AI model creation through evolutionary algorithms.
         | 
| 209 | 
            +
                    This model showcases the fusion of different model capabilities at 1/10,000 the cost of traditional training.
         | 
| 210 | 
            +
                    
         | 
| 211 | 
            +
                    **Key Features:**
         | 
| 212 | 
            +
                    - Automated model merging without manual hyperparameter tuning
         | 
| 213 | 
            +
                    - Multi-objective optimization (accuracy, robustness, generalization)
         | 
| 214 | 
            +
                    - 5,000+ generation evolution process
         | 
| 215 | 
            +
                    
         | 
| 216 | 
            +
                    [GitHub](https://github.com/yourusername/darwin-project) | [Paper](https://arxiv.org/abs/xxxx.xxxxx) (Coming Soon)
         | 
| 217 | 
            +
                    """
         | 
| 218 | 
            +
                )
         | 
| 219 | 
            +
             | 
| 220 | 
            +
            if __name__ == "__main__":
         | 
| 221 | 
            +
                demo.queue().launch(share=True)
         | 

