File size: 6,443 Bytes
d14c764
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
from threading import Thread
import spaces

# Load model and tokenizer
model_id = "openfree/Darwin-Qwen3-4B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    device_map="auto",
    trust_remote_code=True
)

@spaces.GPU
def generate_response(
    message,
    history,
    temperature=0.7,
    max_new_tokens=512,
    top_p=0.9,
    repetition_penalty=1.1,
):
    # Format conversation history
    conversation = []
    for user, assistant in history:
        conversation.extend([
            {"role": "user", "content": user},
            {"role": "assistant", "content": assistant}
        ])
    conversation.append({"role": "user", "content": message})
    
    # Apply chat template if available
    if hasattr(tokenizer, "apply_chat_template"):
        text = tokenizer.apply_chat_template(
            conversation,
            tokenize=False,
            add_generation_prompt=True
        )
    else:
        # Fallback formatting
        text = "\n".join([f"User: {message}" if i["role"] == "user" 
                         else f"Assistant: {message}" 
                         for i in conversation])
        text += "\nAssistant: "
    
    # Tokenize input
    inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=2048)
    inputs = inputs.to(model.device)
    
    # Set up streaming
    streamer = TextIteratorStreamer(
        tokenizer, 
        timeout=10.0, 
        skip_prompt=True, 
        skip_special_tokens=True
    )
    
    # Generation parameters
    gen_kwargs = dict(
        inputs,
        streamer=streamer,
        max_new_tokens=max_new_tokens,
        temperature=temperature,
        top_p=top_p,
        repetition_penalty=repetition_penalty,
        do_sample=True,
        pad_token_id=tokenizer.eos_token_id,
        eos_token_id=tokenizer.eos_token_id,
    )
    
    # Start generation in separate thread
    thread = Thread(target=model.generate, kwargs=gen_kwargs)
    thread.start()
    
    # Stream output
    response = ""
    for new_text in streamer:
        response += new_text
        yield response
    
    thread.join()

# Create Gradio interface
with gr.Blocks(title="Darwin-Qwen3-4B Chat") as demo:
    gr.Markdown(
        """
        # 🌱 Darwin-Qwen3-4B Interactive Chat
        
        Test the evolutionary merged model that combines the strengths of instruction-following and reasoning capabilities.
        
        **Model**: [openfree/Darwin-Qwen3-4B](https://huggingface.co/openfree/Darwin-Qwen3-4B)
        
        This model was created using the Darwin A2AP Enhanced v3.2 evolutionary algorithm, merging:
        - Parent 1: Qwen/Qwen3-4B-Instruct-2507
        - Parent 2: Qwen/Qwen3-4B-Thinking-2507
        """
    )
    
    chatbot = gr.Chatbot(
        label="Chat History",
        bubble_full_width=False,
        height=400
    )
    
    with gr.Row():
        msg = gr.Textbox(
            label="Your Message",
            placeholder="Type your message here and press Enter...",
            lines=2,
            scale=4
        )
        submit_btn = gr.Button("Send", scale=1, variant="primary")
    
    with gr.Accordion("Advanced Settings", open=False):
        temperature = gr.Slider(
            minimum=0.1,
            maximum=1.5,
            value=0.7,
            step=0.1,
            label="Temperature (higher = more creative)"
        )
        max_new_tokens = gr.Slider(
            minimum=64,
            maximum=2048,
            value=512,
            step=64,
            label="Max New Tokens"
        )
        top_p = gr.Slider(
            minimum=0.1,
            maximum=1.0,
            value=0.9,
            step=0.05,
            label="Top-p (nucleus sampling)"
        )
        repetition_penalty = gr.Slider(
            minimum=1.0,
            maximum=1.5,
            value=1.1,
            step=0.05,
            label="Repetition Penalty"
        )
    
    with gr.Row():
        clear_btn = gr.Button("Clear Chat", variant="secondary")
        
    gr.Examples(
        examples=[
            "Explain quantum computing in simple terms.",
            "Write a Python function to find prime numbers.",
            "What are the key differences between machine learning and deep learning?",
            "Suggest a healthy meal plan for a week.",
            "How does photosynthesis work?",
        ],
        inputs=msg,
        label="Example Prompts"
    )
    
    # Event handlers
    def user_submit(message, history):
        return "", history + [[message, None]]
    
    def bot_respond(history, temperature, max_new_tokens, top_p, repetition_penalty):
        message = history[-1][0]
        history[-1][1] = ""
        
        for response in generate_response(
            message, 
            history[:-1],
            temperature,
            max_new_tokens,
            top_p,
            repetition_penalty
        ):
            history[-1][1] = response
            yield history
    
    msg.submit(
        user_submit, 
        [msg, chatbot], 
        [msg, chatbot]
    ).then(
        bot_respond,
        [chatbot, temperature, max_new_tokens, top_p, repetition_penalty],
        chatbot
    )
    
    submit_btn.click(
        user_submit, 
        [msg, chatbot], 
        [msg, chatbot]
    ).then(
        bot_respond,
        [chatbot, temperature, max_new_tokens, top_p, repetition_penalty],
        chatbot
    )
    
    clear_btn.click(lambda: None, None, chatbot, queue=False)
    
    gr.Markdown(
        """
        ---
        ### About Darwin Project
        
        The Darwin Project demonstrates a new paradigm in AI model creation through evolutionary algorithms.
        This model showcases the fusion of different model capabilities at 1/10,000 the cost of traditional training.
        
        **Key Features:**
        - Automated model merging without manual hyperparameter tuning
        - Multi-objective optimization (accuracy, robustness, generalization)
        - 5,000+ generation evolution process
        
        [GitHub](https://github.com/yourusername/darwin-project) | [Paper](https://arxiv.org/abs/xxxx.xxxxx) (Coming Soon)
        """
    )

if __name__ == "__main__":
    demo.queue().launch(share=True)