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 
 
 
bd07eb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3e77146
 
 
 
 
 
 
af21194
 
e2adbf3
af21194
e2adbf3
 
 
 
 
af21194
 
5bc95f5
 
af21194
5bc95f5
 
e2adbf3
 
5bc95f5
e2adbf3
5bc95f5
 
 
e2adbf3
 
 
 
 
 
 
 
 
 
 
5bc95f5
e2adbf3
 
 
5bc95f5
 
 
e2adbf3
 
5bc95f5
af21194
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
"""
Python to C++ Code Optimizer - AI-Powered Code Conversion
Modern Gradio app with password protection for secure deployments

Supported Models:
- GPT-4o (OpenAI) - Premium, fastest, most accurate
- Claude-3.5-Sonnet (Anthropic) - Premium, excellent for code

⚠️ SECURITY WARNING:
This app executes arbitrary code. Only run code from trusted sources.
Malicious code can harm the system. Use at your own risk.
"""

import os
import io
import sys
import subprocess
import socket
import httpx
from openai import OpenAI
import anthropic
# Workaround for Python 3.13 where stdlib audioop is removed; instruct pydub to use pure-python fallback
os.environ.setdefault("PYDUB_SIMPLE_AUDIOOP", "1")
import gradio as gr

# Try to load from .env file if available
try:
    from dotenv import load_dotenv
    load_dotenv()
except ImportError:
    pass

# PASSWORD PROTECTION
# Set this as a Hugging Face Secret: APP_PASSWORD
APP_PASSWORD = os.environ.get("APP_PASSWORD", "demo123")  # Change default!

# Lazy initialization of AI clients with explicit HTTP client to avoid Gradio conflicts
def get_openai_client():
    api_key = os.environ.get("OPENAI_API_KEY")
    if not api_key:
        raise ValueError("OPENAI_API_KEY not found. Please set it in your environment or .env file.")
    
    # Create a clean HTTP client without proxies to avoid Gradio conflicts
    http_client = httpx.Client(
        timeout=60.0,
        limits=httpx.Limits(max_keepalive_connections=5, max_connections=10)
    )
    
    return OpenAI(api_key=api_key, http_client=http_client)

def get_claude_client():
    api_key = os.environ.get("ANTHROPIC_API_KEY")
    if not api_key:
        raise ValueError("ANTHROPIC_API_KEY not found. Please set it in your environment or .env file.")
    
    # Create a clean HTTP client without proxies to avoid Gradio conflicts
    http_client = httpx.Client(
        timeout=60.0,
        limits=httpx.Limits(max_keepalive_connections=5, max_connections=10)
    )
    
    return anthropic.Anthropic(api_key=api_key, http_client=http_client)

# Model configurations
OPENAI_MODEL = "gpt-4o"
CLAUDE_MODEL = "claude-3-5-sonnet-20240620"

# System and user prompts
system_message = (
    "You are an assistant that reimplements Python code in high performance C++. "
    "Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. "
    "The C++ response needs to produce an identical output in the fastest possible time."
)

def user_prompt_for(python):
    user_prompt = (
        "Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. "
        "Respond only with C++ code; do not explain your work other than a few comments. "
        "Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\n\n"
    )
    user_prompt += python
    return user_prompt

def messages_for(python):
    return [
        {"role": "system", "content": system_message},
        {"role": "user", "content": user_prompt_for(python)}
    ]

def write_output(cpp):
    """Write C++ code to file for compilation"""
    code = cpp.replace("```cpp","").replace("```","")
    with open("optimized.cpp", "w") as f:
        f.write(code)

def stream_gpt(python):
    """Stream GPT-4o response"""
    try:
        client = get_openai_client()
        stream = client.chat.completions.create(
            model=OPENAI_MODEL, 
            messages=messages_for(python), 
            stream=True
        )
        reply = ""
        for chunk in stream:
            fragment = chunk.choices[0].delta.content or ""
            reply += fragment
            yield reply.replace('```cpp\n','').replace('```','')
    except ValueError as e:
        yield f"❌ Error: {str(e)}"
    except Exception as e:
        yield f"❌ Error: {str(e)}"

def stream_claude(python):
    """Stream Claude response"""
    try:
        client = get_claude_client()
        result = client.messages.stream(
            model=CLAUDE_MODEL,
            max_tokens=2000,
            system=system_message,
            messages=[{"role": "user", "content": user_prompt_for(python)}],
        )
        reply = ""
        with result as stream:
            for text in stream.text_stream:
                reply += text
                yield reply.replace('```cpp\n','').replace('```','')
    except ValueError as e:
        yield f"❌ Error: {str(e)}"
    except Exception as e:
        yield f"❌ Error: {str(e)}"

def optimize(python, model):
    """Convert Python to C++ using selected AI model"""
    if model in ["GPT-4o", "GPT"]:
        result = stream_gpt(python)
    elif model in ["Claude-3.5-Sonnet", "Claude"]:
        result = stream_claude(python)
    else:
        raise ValueError(f"Unknown model: {model}")
    
    for stream_so_far in result:
        yield stream_so_far

def execute_python(code):
    """⚠️ WARNING: Executes arbitrary Python code"""
    try:
        output = io.StringIO()
        sys.stdout = output
        exec(code)
    finally:
        sys.stdout = sys.__stdout__
    return output.getvalue()

def execute_cpp(code):
    """⚠️ WARNING: Compiles and executes arbitrary C++ code"""
    write_output(code)
    try:
        compile_cmd = ["g++", "-O3", "-std=c++17", "-o", "optimized", "optimized.cpp"]
        compile_result = subprocess.run(
            compile_cmd, 
            check=True, 
            text=True, 
            capture_output=True,
            timeout=30
        )
        
        run_cmd = ["./optimized"]
        run_result = subprocess.run(
            run_cmd, 
            check=True, 
            text=True, 
            capture_output=True,
            timeout=30
        )
        return run_result.stdout
    except subprocess.TimeoutExpired:
        return "⚠️ Execution timed out (30 seconds limit)"
    except subprocess.CalledProcessError as e:
        return f"❌ An error occurred:\n{e.stderr}"
    except Exception as e:
        return f"❌ Unexpected error: {str(e)}"

# Example Python code
default_python = """import time

def calculate(iterations, param1, param2):
    result = 1.0
    for i in range(1, iterations+1):
        j = i * param1 - param2
        result -= (1/j)
        j = i * param1 + param2
        result += (1/j)
    return result

start_time = time.time()
result = calculate(100_000_000, 4, 1) * 4
end_time = time.time()

print(f"Result: {result:.12f}")
print(f"Execution Time: {(end_time - start_time):.6f} seconds")
"""

# Modern CSS
modern_css = """
.gradio-container, body, html {
    background: #f9fafb !important; /* light background to avoid dark-mode conflicts */
    color: #111827 !important;      /* ensure readable text */
}

.gradio-container {
    max-width: 1400px !important;
    margin: 0 auto !important;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

.modern-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 24px;
    border-radius: 16px;
    margin-bottom: 24px;
    text-align: center;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
}

.modern-header h1 {
    margin: 0;
    font-size: 32px;
    font-weight: 700;
    letter-spacing: -0.5px;
}

.modern-header p {
    margin: 12px 0 0 0;
    opacity: 0.9;
    font-size: 18px;
    font-weight: 400;
}

.security-warning {
    background: #fee2e2 !important;
    border: 2px solid #dc2626 !important;
    border-radius: 12px !important;
    padding: 16px !important;
    margin: 16px 0 !important;
}

.python-input {
    background: #f8fafc !important;
    border: 2px solid #e2e8f0 !important;
    border-radius: 12px !important;
    padding: 16px !important;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
    font-size: 14px !important;
    color: #1e293b !important;
    line-height: 1.5 !important;
}

.python-input:focus {
    border-color: #3b82f6 !important;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
}

.cpp-output {
    background: #f1f5f9 !important;
    border: 2px solid #cbd5e1 !important;
    border-radius: 12px !important;
    padding: 16px !important;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
    font-size: 14px !important;
    color: #0f172a !important;
    line-height: 1.5 !important;
}

.model-selector {
    background: white !important;
    border: 2px solid #e2e8f0 !important;
    border-radius: 12px !important;
    padding: 12px 16px !important;
    font-size: 16px !important;
    color: #374151 !important;
}

.model-selector:focus {
    border-color: #3b82f6 !important;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
}

.modern-button {
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%) !important;
    color: white !important;
    border: none !important;
    border-radius: 12px !important;
    padding: 14px 28px !important;
    font-weight: 600 !important;
    font-size: 16px !important;
    cursor: pointer !important;
    transition: all 0.2s ease !important;
    box-shadow: 0 4px 6px rgba(59, 130, 246, 0.2) !important;
}

.modern-button:hover {
    transform: translateY(-2px) !important;
    box-shadow: 0 8px 12px rgba(59, 130, 246, 0.3) !important;
}

.run-button {
    background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important;
    color: white !important;
    border: none !important;
    border-radius: 10px !important;
    padding: 12px 24px !important;
    font-weight: 600 !important;
    font-size: 14px !important;
    cursor: pointer !important;
    transition: all 0.2s ease !important;
    box-shadow: 0 4px 6px rgba(16, 185, 129, 0.2) !important;
}

.run-button:hover {
    transform: translateY(-1px) !important;
    box-shadow: 0 6px 8px rgba(16, 185, 129, 0.3) !important;
}

.output-section {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    padding: 16px;
    margin: 12px 0;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: 13px;
    line-height: 1.4;
    color: #374151;
    min-height: 100px;
    overflow-y: auto;
}

.python-output {
    background: #fef3c7 !important;
    border: 2px solid #f59e0b !important;
    color: #92400e !important;
}

.cpp-output-result {
    background: #dbeafe !important;
    border: 2px solid #3b82f6 !important;
    color: #1e40af !important;
}

.performance-card {
    background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
    border: 1px solid #0ea5e9;
    border-radius: 12px;
    padding: 20px;
    margin: 16px 0;
    text-align: center;
}

.performance-card h3 {
    margin: 0 0 12px 0;
    color: #0c4a6e;
    font-size: 18px;
    font-weight: 600;
}

.performance-metric {
    display: inline-block;
    background: white;
    border-radius: 8px;
    padding: 8px 16px;
    margin: 4px;
    font-weight: 600;
    color: #0c4a6e;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

/* Login styling */
.login-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 60vh;
}

.login-card {
    max-width: 480px;
    width: 100%;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.08);
}

.login-title {
    text-align: center;
    margin: 0 0 6px 0;
    font-size: 22px;
    font-weight: 700;
    color: #111827;
}

.login-subtitle {
    text-align: center;
    margin: 0 0 16px 0;
    font-size: 14px;
    color: #6b7280;
}

.login-input {
    background: #f9fafb !important;
    border: 2px solid #e5e7eb !important;
    border-radius: 10px !important;
    padding: 12px !important;
}

.login-error {
    color: #b91c1c;
    background: #fee2e2;
    border: 1px solid #fecaca;
    padding: 10px 12px;
    border-radius: 10px;
}

.login-hero img {
    width: 100% !important;
    height: 160px !important;
    object-fit: cover !important;
    border-radius: 12px !important;
    margin-bottom: 12px !important;
}
"""

# Create the interface with password protection
def create_interface():
    with gr.Blocks(css=modern_css, title="Python to C++ Code Optimizer", theme=gr.themes.Soft()) as app:
        authorized = gr.State(False)
        BANNER_URL = os.getenv("BANNER_URL", "")
        BACKGROUND_URL = os.getenv("BACKGROUND_URL", "")

        # Optional global background image
        if BACKGROUND_URL:
            gr.HTML(f"<style>body, .gradio-container {{ background: url('{BACKGROUND_URL}') center/cover no-repeat fixed !important; }}</style>")

        def check_password(pw):
            ok = pw == APP_PASSWORD
            return (
                gr.update(visible=not ok),  # login hidden when ok
                gr.update(visible=ok),      # main shown when ok
                gr.update(value="" if ok else "Invalid password", visible=not ok)
            )

        # Login gate
        with gr.Group(visible=True) as login_group:
            gr.HTML('<div class="login-wrapper"><div class="login-card">')
            if BANNER_URL:
                gr.Image(value=BANNER_URL, show_label=False, elem_classes=["login-hero"])
            gr.HTML('<div class="login-title">πŸ” Private Access</div>')
            gr.HTML('<div class="login-subtitle">Enter the shared password to continue</div>')
            pw = gr.Textbox(label="Password", type="password", placeholder="Enter password", elem_classes=["login-input"])
            login_btn = gr.Button("Continue", elem_classes=["modern-button"])
            login_error = gr.Markdown(visible=False, elem_classes=["login-error"])
            gr.HTML('</div></div>')

        # Main UI wrapped for toggling visibility
        with gr.Group(visible=False) as main_group:
            main_group.elem_id = "main_group"
            # Header Section
            gr.HTML("""
            <div class="modern-header">
                <h1>πŸš€ Python to C++ Code Optimizer</h1>
                <p>AI-powered code conversion with real-time execution and performance analysis</p>
            </div>
            """)

            # Security Warning
            gr.HTML("""
            <div class="security-warning">
                <h3 style="color: #dc2626; margin: 0 0 8px 0;">⚠️ Security Warning</h3>
                <p style="margin: 0; color: #991b1b; font-weight: 500;">
                    This interface executes arbitrary code. <strong>Only run code from trusted sources.</strong><br>
                    Malicious code can harm your system. Use at your own risk.
                </p>
            </div>
            """)

            # Main Content Area
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("### πŸ“ Python Code Input")
                    python_input = gr.Textbox(
                        label="Python Code:", 
                        value=default_python, 
                        lines=15,
                        elem_classes=["python-input"],
                        placeholder="Enter your Python code here..."
                    )

                    with gr.Row():
                        model_selector = gr.Dropdown(
                            ["GPT-4o", "Claude-3.5-Sonnet"], 
                            label="Select AI Model", 
                            value="GPT-4o",
                            elem_classes=["model-selector"]
                        )

                    convert_button = gr.Button("✨ Convert to C++", elem_classes=["modern-button"])

                with gr.Column(scale=1):
                    gr.Markdown("### ⚑ Optimized C++ Code")
                    cpp_output = gr.Textbox(
                        label="Generated C++ Code:", 
                        lines=15,
                        elem_classes=["cpp-output"],
                        interactive=False
                    )

            # Execution Section
            gr.Markdown("---")
            gr.Markdown("## πŸƒ Code Execution & Performance Comparison")

            with gr.Row():
                with gr.Column():
                    gr.Markdown("### 🐍 Python Output")
                    run_python_button = gr.Button("▢️ Run Python", elem_classes=["run-button"])
                    python_output = gr.Textbox(
                        label="Python Execution Output:",
                        lines=5,
                        elem_classes=["python-output"],
                        interactive=False
                    )

                with gr.Column():
                    gr.Markdown("### ⚑ C++ Output")
                    run_cpp_button = gr.Button("▢️ Run C++", elem_classes=["run-button"])
                    cpp_execution_output = gr.Textbox(
                        label="C++ Execution Output:",
                        lines=5,
                        elem_classes=["cpp-output-result"],
                        interactive=False
                    )

            # Event handlers
            convert_button.click(
                fn=optimize,
                inputs=[python_input, model_selector],
                outputs=cpp_output
            )

            run_python_button.click(
                fn=execute_python,
                inputs=python_input,
                outputs=python_output
            )

            run_cpp_button.click(
                fn=execute_cpp,
                inputs=cpp_output,
                outputs=cpp_execution_output
            )

        # Bind login after main_group is defined
        login_btn.click(
            fn=check_password,
            inputs=[pw],
            outputs=[login_group, main_group, login_error]
        )

    return app

# Launch the app
if __name__ == "__main__":
    print("\n" + "="*50)
    print(f"===== Application Startup at {__import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
    print("="*50)
    
    # Create the app
    app = create_interface()
    
    # Check if running on Hugging Face Spaces
    is_huggingface = os.getenv("SPACE_ID") is not None
    
    if is_huggingface:
        # Hugging Face Spaces configuration
        print("πŸš€ Launching Python to C++ Code Optimizer on Hugging Face Spaces")
        print("πŸ” Password protection enabled")
        app.launch(
            show_error=True
        )
    else:
        # Local development configuration
        def get_available_port(start_port=7860):
            """Find an available port starting from start_port"""
            port = start_port
            while port < start_port + 100:
                try:
                    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
                        s.bind(('', port))
                        return port
                except OSError:
                    port += 1
            return start_port
        
        port = get_available_port()
        print(f"πŸš€ Launching Python to C++ Code Optimizer on port: {port}")
        print(f"πŸ” Password protection enabled. Password: {APP_PASSWORD}")
        
        app.launch(
            server_name="127.0.0.1",
            server_port=port,
            show_error=True
        )