File size: 11,395 Bytes
a151dea
 
2867a45
a151dea
 
 
0557063
 
2867a45
 
a151dea
2867a45
 
 
 
 
a151dea
2867a45
 
 
 
 
 
9eb3680
2867a45
 
 
 
 
9eb3680
 
a151dea
9eb3680
a151dea
2867a45
a151dea
2867a45
9eb3680
 
 
 
 
 
 
2867a45
 
9eb3680
2867a45
 
 
 
0557063
9eb3680
b920ac7
0557063
 
 
 
 
 
 
 
 
b920ac7
9eb3680
2867a45
 
 
 
 
 
 
 
 
9eb3680
2867a45
 
 
 
 
b920ac7
2867a45
 
 
 
 
 
9eb3680
 
 
 
2867a45
9eb3680
 
2867a45
 
 
 
0557063
2867a45
9eb3680
 
 
2867a45
 
 
0557063
 
 
 
 
 
 
 
9eb3680
0557063
2867a45
0557063
2867a45
 
a151dea
2867a45
9eb3680
 
 
a151dea
0557063
9eb3680
0557063
 
 
 
 
 
 
9eb3680
0557063
a151dea
 
0557063
 
 
 
 
 
 
 
9eb3680
a151dea
2867a45
 
0557063
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b920ac7
2867a45
 
b920ac7
 
 
9eb3680
0557063
2867a45
 
 
b920ac7
 
 
2867a45
b920ac7
 
 
0557063
b920ac7
 
 
 
9eb3680
b920ac7
 
 
 
 
 
 
a151dea
0557063
b920ac7
 
 
2867a45
0557063
2867a45
0557063
2867a45
0557063
a151dea
 
9475c70
9eb3680
 
0557063
9eb3680
 
 
2867a45
 
 
0557063
 
2867a45
b920ac7
a151dea
b920ac7
2867a45
 
0557063
2867a45
 
 
b920ac7
 
a151dea
2867a45
 
b920ac7
 
9eb3680
0557063
cd5c7f3
 
 
 
 
 
2867a45
 
0557063
2867a45
0557063
 
 
9475c70
a151dea
2867a45
0557063
 
 
9475c70
a151dea
0557063
b920ac7
0557063
 
a151dea
9475c70
9eb3680
0557063
b920ac7
0557063
9eb3680
 
 
2867a45
a151dea
9475c70
9eb3680
0557063
b920ac7
0557063
9eb3680
 
 
2867a45
a151dea
2867a45
0557063
 
2867a45
a151dea
2867a45
a151dea
2867a45
a151dea
9475c70
 
 
0557063
9475c70
 
0557063
 
 
2867a45
 
9eb3680
 
0557063
9eb3680
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# app.py
import gradio as gr
from openai import OpenAI
import os
import time

print(f"Gradio version: {gr.__version__}")

# ==================== Configuration ====================
DEFAULT_SYSTEM_PROMPT = "You are DeepSeek-V4, an advanced AI assistant with strong reasoning capabilities. Provide accurate, helpful, and well-reasoned responses."

REASONING_EFFORT_MAP = {
    "Non-think": "minimal",
    "Think High": "high", 
    "Think Max": "maximum"
}

THINKING_TYPE_MAP = {
    "Non-think": "disabled",
    "Think High": "enabled",
    "Think Max": "enabled"
}

# ==================== API Client ====================
def get_client():
    api_key = os.environ.get('DEEPSEEK_API_KEY')
    if not api_key:
        raise ValueError(
            "⚠️ DEEPSEEK_API_KEY not found!\n\n"
            "Get your key: https://platform.deepseek.com/api_keys\n"
            "Then set: DEEPSEEK_API_KEY=your-key-here"
        )
    return OpenAI(api_key=api_key, base_url="https://api.deepseek.com")

# ==================== Response Generation ====================
def generate_response(
    message: str,
    history: list,
    thinking_mode: str,
    max_tokens: int,
    temperature: float,
    top_p: float,
    system_prompt: str,
    show_thinking: bool
):
    if not message.strip():
        yield history, "", "Please enter a message."
        return
    
    client = get_client()
    
    # Build messages from history
    api_messages = [{"role": "system", "content": system_prompt}]
    
    if history:
        for item in history:
            if isinstance(item, dict):
                api_messages.append({"role": item["role"], "content": item["content"]})
            elif isinstance(item, (list, tuple)):
                # Handle [(user, assistant), ...] format
                api_messages.append({"role": "user", "content": item[0]})
                if item[1]:
                    api_messages.append({"role": "assistant", "content": item[1]})
    
    api_messages.append({"role": "user", "content": message})
    
    reasoning_effort = REASONING_EFFORT_MAP.get(thinking_mode, "high")
    thinking_type = THINKING_TYPE_MAP.get(thinking_mode, "enabled")
    
    try:
        start_time = time.time()
        
        stream = client.chat.completions.create(
            model="deepseek-v4-pro",
            messages=api_messages,
            stream=True,
            max_tokens=max_tokens,
            temperature=temperature,
            top_p=top_p,
            reasoning_effort=reasoning_effort,
            extra_body={"thinking": {"type": thinking_type}}
        )
        
        content_chunks = []
        thinking_chunks = []
        
        for chunk in stream:
            delta = chunk.choices[0].delta
            
            if delta.content:
                content_chunks.append(delta.content)
            
            if hasattr(delta, 'reasoning_content') and delta.reasoning_content:
                thinking_chunks.append(delta.reasoning_content)
            
            current_content = ''.join(content_chunks)
            current_thinking = ''.join(thinking_chunks)
            
            # Build response
            if show_thinking and current_thinking:
                response_text = f"**[Thinking]**\n{current_thinking}\n\n**[Response]**\n{current_content}"
            else:
                response_text = current_content
            
            elapsed = time.time() - start_time
            
            # Try dict format, fallback to tuple format
            try:
                new_history = history.copy() if history else []
                new_history.append({"role": "user", "content": message})
                new_history.append({"role": "assistant", "content": response_text})
            except:
                new_history = list(history) if history else []
                new_history.append((message, response_text))
            
            yield new_history, current_thinking, f"πŸ”„ {elapsed:.1f}s"
        
        # Final
        final_content = ''.join(content_chunks)
        final_thinking = ''.join(thinking_chunks)
        
        if show_thinking and final_thinking:
            final_response = f"**[Thinking]**\n{final_thinking}\n\n**[Response]**\n{final_content}"
        else:
            final_response = final_content
        
        elapsed = time.time() - start_time
        
        try:
            final_history = history.copy() if history else []
            final_history.append({"role": "user", "content": message})
            final_history.append({"role": "assistant", "content": final_response})
        except:
            final_history = list(history) if history else []
            final_history.append((message, final_response))
        
        yield final_history, final_thinking, f"βœ… {elapsed:.1f}s"
        
    except Exception as e:
        error_msg = f"❌ {str(e)}"
        try:
            new_history = history.copy() if history else []
            new_history.append({"role": "user", "content": message})
            new_history.append({"role": "assistant", "content": error_msg})
        except:
            new_history = list(history) if history else []
            new_history.append((message, error_msg))
        yield new_history, "", error_msg

# ==================== Gradio Interface ====================
def create_demo():
    # Try with type parameter, fallback without
    try:
        # Try newest format
        chatbot = gr.Chatbot(
            label="πŸ’¬ Chat with DeepSeek-V4 Pro",
            height=500,
            type="messages"
        )
    except:
        try:
            # Try without type
            chatbot = gr.Chatbot(
                label="πŸ’¬ Chat with DeepSeek-V4 Pro",
                height=500
            )
        except:
            # Minimal
            chatbot = gr.Chatbot(label="πŸ’¬ Chat with DeepSeek-V4 Pro")
    
    with gr.Blocks(title="DeepSeek-V4 Pro Demo") as demo:
        
        # Header
        gr.Markdown("""
        # πŸš€ DeepSeek-V4 Pro
        
        **Million-Token Context Intelligence**
        1.6T Parameters | 49B Activated | 1M Context
        """)
        
        with gr.Row():
            # Left sidebar
            with gr.Column(scale=1, min_width=300):
                gr.Markdown("### βš™οΈ Settings")
                
                thinking_mode = gr.Radio(
                    choices=["Non-think", "Think High", "Think Max"],
                    value="Think High",
                    label="🧠 Reasoning Mode"
                )
                
                show_thinking = gr.Checkbox(
                    value=True,
                    label="πŸ“ Show thinking process"
                )
                
                system_prompt = gr.Textbox(
                    label="πŸ“‹ System Prompt",
                    value=DEFAULT_SYSTEM_PROMPT,
                    lines=3
                )
                
                with gr.Accordion("πŸ”§ Advanced", open=False):
                    max_tokens = gr.Slider(64, 32768, value=4096, step=64, label="Max Tokens")
                    temperature = gr.Slider(0.0, 2.0, value=0.7, step=0.05, label="Temperature")
                    top_p = gr.Slider(0.0, 1.0, value=1.0, step=0.05, label="Top P")
            
            # Right - Chat
            with gr.Column(scale=2):
                chatbot_component = gr.Chatbot(
                    label="πŸ’¬ Chat with DeepSeek-V4 Pro",
                    height=500
                )
                
                with gr.Accordion("🧠 Thinking Process", open=True):
                    thinking_display = gr.Textbox(
                        label="Reasoning",
                        value="*Waiting...*",
                        lines=5,
                        interactive=False
                    )
                
                with gr.Row():
                    message_input = gr.Textbox(
                        label="Message",
                        placeholder="Type your message...",
                        lines=2,
                        scale=9
                    )
                    send_btn = gr.Button("πŸš€ Send", variant="primary", scale=1)
                
                with gr.Row():
                    clear_btn = gr.Button("πŸ—‘οΈ Clear", size="sm")
                
                status_display = gr.Textbox(
                    label="Status",
                    value="βœ… Ready",
                    interactive=False
                )
        
        # Footer
        gr.Markdown("""
        ---
        πŸ”‘ [Get API Key](https://platform.deepseek.com/api_keys) | 
        πŸ“¦ [Model](https://huggingface.co/deepseek-ai/DeepSeek-V4-Pro)
        
        ### πŸ”₯ Trending AI Tools β€” Free to Try
        - πŸ–ΌοΈ [Free AI Image Upscaler 4K/8K β€” Enhance Any Photo Instantly](https://dlss5nvidia.com)
        - 🏠 [Free AI House Design Generator β€” Floor Plans & 3D Render in Seconds](https://housepluspus.com)
        - 🎨 [Free GPT-4o Image Generator β€” Text to Stunning Visuals](https://gpst-image2.com)
        - ⚑ [DLSS 5 AI Rendering Boost β€” 2x FPS for Free](https://dlss5.app)
        """)
        
        # ==================== Events ====================
        
        def on_send(msg, hist, mode, show, sys_prompt, max_tok, temp, top_p_val):
            if not msg.strip():
                yield hist, "*Waiting...*", "Enter a message."
                return
            
            if not os.environ.get('DEEPSEEK_API_KEY'):
                new_hist = list(hist) if hist else []
                new_hist.append((msg, "⚠️ Set DEEPSEEK_API_KEY"))
                yield new_hist, "*Error*", "❌ API Key missing"
                return
            
            hist = list(hist) if hist else []
            
            for h, t, s in generate_response(msg, hist, mode, max_tok, temp, top_p_val, sys_prompt, show):
                yield h, t if t else "*No reasoning*", s
        
        send_btn.click(
            fn=on_send,
            inputs=[message_input, chatbot_component, thinking_mode, show_thinking,
                    system_prompt, max_tokens, temperature, top_p],
            outputs=[chatbot_component, thinking_display, status_display]
        ).then(
            fn=lambda: "",
            outputs=[message_input]
        )
        
        message_input.submit(
            fn=on_send,
            inputs=[message_input, chatbot_component, thinking_mode, show_thinking,
                    system_prompt, max_tokens, temperature, top_p],
            outputs=[chatbot_component, thinking_display, status_display]
        ).then(
            fn=lambda: "",
            outputs=[message_input]
        )
        
        clear_btn.click(
            fn=lambda: ([], "*Cleared*", "βœ… Cleared"),
            outputs=[chatbot_component, thinking_display, status_display]
        )
    
    return demo

# ==================== Main ====================
if __name__ == "__main__":
    try:
        from dotenv import load_dotenv
        load_dotenv()
    except:
        pass
    
    if not os.environ.get('DEEPSEEK_API_KEY'):
        print("\n⚠️  Set DEEPSEEK_API_KEY environment variable!")
        print("   export DEEPSEEK_API_KEY='your-key'\n")
    
    demo = create_demo()
    demo.queue(max_size=50).launch(
        server_name="0.0.0.0",
        server_port=7860
    )