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1 |
+
"""
|
| 2 |
+
Python to C++ Code Optimizer - AI-Powered Code Conversion
|
| 3 |
+
Modern Gradio app with password protection for secure deployments
|
| 4 |
+
|
| 5 |
+
Supported Models:
|
| 6 |
+
- GPT-4o (OpenAI) - Premium, fastest, most accurate
|
| 7 |
+
- Claude-3.5-Sonnet (Anthropic) - Premium, excellent for code
|
| 8 |
+
|
| 9 |
+
β οΈ SECURITY WARNING:
|
| 10 |
+
This app executes arbitrary code. Only run code from trusted sources.
|
| 11 |
+
Malicious code can harm the system. Use at your own risk.
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
import os
|
| 15 |
+
import io
|
| 16 |
+
import sys
|
| 17 |
+
import subprocess
|
| 18 |
+
import socket
|
| 19 |
+
import httpx
|
| 20 |
+
from openai import OpenAI
|
| 21 |
+
import anthropic
|
| 22 |
+
# Workaround for Python 3.13 where stdlib audioop is removed; instruct pydub to use pure-python fallback
|
| 23 |
+
os.environ.setdefault("PYDUB_SIMPLE_AUDIOOP", "1")
|
| 24 |
+
import gradio as gr
|
| 25 |
+
|
| 26 |
+
# Try to load from .env file if available
|
| 27 |
+
try:
|
| 28 |
+
from dotenv import load_dotenv
|
| 29 |
+
load_dotenv()
|
| 30 |
+
except ImportError:
|
| 31 |
+
pass
|
| 32 |
+
|
| 33 |
+
# PASSWORD PROTECTION
|
| 34 |
+
# Set this as a Hugging Face Secret: APP_PASSWORD
|
| 35 |
+
APP_PASSWORD = os.environ.get("APP_PASSWORD", "demo123") # Change default!
|
| 36 |
+
|
| 37 |
+
# Lazy initialization of AI clients with explicit HTTP client to avoid Gradio conflicts
|
| 38 |
+
def get_openai_client():
|
| 39 |
+
api_key = os.environ.get("OPENAI_API_KEY")
|
| 40 |
+
if not api_key:
|
| 41 |
+
raise ValueError("OPENAI_API_KEY not found. Please set it in your environment or .env file.")
|
| 42 |
+
|
| 43 |
+
# Create a clean HTTP client without proxies to avoid Gradio conflicts
|
| 44 |
+
http_client = httpx.Client(
|
| 45 |
+
timeout=60.0,
|
| 46 |
+
limits=httpx.Limits(max_keepalive_connections=5, max_connections=10)
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
return OpenAI(api_key=api_key, http_client=http_client)
|
| 50 |
+
|
| 51 |
+
def get_claude_client():
|
| 52 |
+
api_key = os.environ.get("ANTHROPIC_API_KEY")
|
| 53 |
+
if not api_key:
|
| 54 |
+
raise ValueError("ANTHROPIC_API_KEY not found. Please set it in your environment or .env file.")
|
| 55 |
+
|
| 56 |
+
# Create a clean HTTP client without proxies to avoid Gradio conflicts
|
| 57 |
+
http_client = httpx.Client(
|
| 58 |
+
timeout=60.0,
|
| 59 |
+
limits=httpx.Limits(max_keepalive_connections=5, max_connections=10)
|
| 60 |
+
)
|
| 61 |
+
|
| 62 |
+
return anthropic.Anthropic(api_key=api_key, http_client=http_client)
|
| 63 |
+
|
| 64 |
+
# Model configurations
|
| 65 |
+
OPENAI_MODEL = "gpt-4o"
|
| 66 |
+
CLAUDE_MODEL = "claude-3-5-sonnet-20240620"
|
| 67 |
+
|
| 68 |
+
# System and user prompts
|
| 69 |
+
system_message = (
|
| 70 |
+
"You are an assistant that reimplements Python code in high performance C++. "
|
| 71 |
+
"Respond only with C++ code; use comments sparingly and do not provide any explanation other than occasional comments. "
|
| 72 |
+
"The C++ response needs to produce an identical output in the fastest possible time."
|
| 73 |
+
)
|
| 74 |
+
|
| 75 |
+
def user_prompt_for(python):
|
| 76 |
+
user_prompt = (
|
| 77 |
+
"Rewrite this Python code in C++ with the fastest possible implementation that produces identical output in the least time. "
|
| 78 |
+
"Respond only with C++ code; do not explain your work other than a few comments. "
|
| 79 |
+
"Pay attention to number types to ensure no int overflows. Remember to #include all necessary C++ packages such as iomanip.\n\n"
|
| 80 |
+
)
|
| 81 |
+
user_prompt += python
|
| 82 |
+
return user_prompt
|
| 83 |
+
|
| 84 |
+
def messages_for(python):
|
| 85 |
+
return [
|
| 86 |
+
{"role": "system", "content": system_message},
|
| 87 |
+
{"role": "user", "content": user_prompt_for(python)}
|
| 88 |
+
]
|
| 89 |
+
|
| 90 |
+
def write_output(cpp):
|
| 91 |
+
"""Write C++ code to file for compilation"""
|
| 92 |
+
code = cpp.replace("```cpp","").replace("```","")
|
| 93 |
+
with open("optimized.cpp", "w") as f:
|
| 94 |
+
f.write(code)
|
| 95 |
+
|
| 96 |
+
def stream_gpt(python):
|
| 97 |
+
"""Stream GPT-4o response"""
|
| 98 |
+
try:
|
| 99 |
+
client = get_openai_client()
|
| 100 |
+
stream = client.chat.completions.create(
|
| 101 |
+
model=OPENAI_MODEL,
|
| 102 |
+
messages=messages_for(python),
|
| 103 |
+
stream=True
|
| 104 |
+
)
|
| 105 |
+
reply = ""
|
| 106 |
+
for chunk in stream:
|
| 107 |
+
fragment = chunk.choices[0].delta.content or ""
|
| 108 |
+
reply += fragment
|
| 109 |
+
yield reply.replace('```cpp\n','').replace('```','')
|
| 110 |
+
except ValueError as e:
|
| 111 |
+
yield f"β Error: {str(e)}"
|
| 112 |
+
except Exception as e:
|
| 113 |
+
yield f"β Error: {str(e)}"
|
| 114 |
+
|
| 115 |
+
def stream_claude(python):
|
| 116 |
+
"""Stream Claude response"""
|
| 117 |
+
try:
|
| 118 |
+
client = get_claude_client()
|
| 119 |
+
result = client.messages.stream(
|
| 120 |
+
model=CLAUDE_MODEL,
|
| 121 |
+
max_tokens=2000,
|
| 122 |
+
system=system_message,
|
| 123 |
+
messages=[{"role": "user", "content": user_prompt_for(python)}],
|
| 124 |
+
)
|
| 125 |
+
reply = ""
|
| 126 |
+
with result as stream:
|
| 127 |
+
for text in stream.text_stream:
|
| 128 |
+
reply += text
|
| 129 |
+
yield reply.replace('```cpp\n','').replace('```','')
|
| 130 |
+
except ValueError as e:
|
| 131 |
+
yield f"β Error: {str(e)}"
|
| 132 |
+
except Exception as e:
|
| 133 |
+
yield f"β Error: {str(e)}"
|
| 134 |
+
|
| 135 |
+
def optimize(python, model):
|
| 136 |
+
"""Convert Python to C++ using selected AI model"""
|
| 137 |
+
if model in ["GPT-4o", "GPT"]:
|
| 138 |
+
result = stream_gpt(python)
|
| 139 |
+
elif model in ["Claude-3.5-Sonnet", "Claude"]:
|
| 140 |
+
result = stream_claude(python)
|
| 141 |
+
else:
|
| 142 |
+
raise ValueError(f"Unknown model: {model}")
|
| 143 |
+
|
| 144 |
+
for stream_so_far in result:
|
| 145 |
+
yield stream_so_far
|
| 146 |
+
|
| 147 |
+
def execute_python(code):
|
| 148 |
+
"""β οΈ WARNING: Executes arbitrary Python code"""
|
| 149 |
+
try:
|
| 150 |
+
output = io.StringIO()
|
| 151 |
+
sys.stdout = output
|
| 152 |
+
exec(code)
|
| 153 |
+
finally:
|
| 154 |
+
sys.stdout = sys.__stdout__
|
| 155 |
+
return output.getvalue()
|
| 156 |
+
|
| 157 |
+
def execute_cpp(code):
|
| 158 |
+
"""β οΈ WARNING: Compiles and executes arbitrary C++ code"""
|
| 159 |
+
write_output(code)
|
| 160 |
+
try:
|
| 161 |
+
compile_cmd = ["g++", "-O3", "-std=c++17", "-o", "optimized", "optimized.cpp"]
|
| 162 |
+
compile_result = subprocess.run(
|
| 163 |
+
compile_cmd,
|
| 164 |
+
check=True,
|
| 165 |
+
text=True,
|
| 166 |
+
capture_output=True,
|
| 167 |
+
timeout=30
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
run_cmd = ["./optimized"]
|
| 171 |
+
run_result = subprocess.run(
|
| 172 |
+
run_cmd,
|
| 173 |
+
check=True,
|
| 174 |
+
text=True,
|
| 175 |
+
capture_output=True,
|
| 176 |
+
timeout=30
|
| 177 |
+
)
|
| 178 |
+
return run_result.stdout
|
| 179 |
+
except subprocess.TimeoutExpired:
|
| 180 |
+
return "β οΈ Execution timed out (30 seconds limit)"
|
| 181 |
+
except subprocess.CalledProcessError as e:
|
| 182 |
+
return f"β An error occurred:\n{e.stderr}"
|
| 183 |
+
except Exception as e:
|
| 184 |
+
return f"β Unexpected error: {str(e)}"
|
| 185 |
+
|
| 186 |
+
# Example Python code
|
| 187 |
+
default_python = """import time
|
| 188 |
+
|
| 189 |
+
def calculate(iterations, param1, param2):
|
| 190 |
+
result = 1.0
|
| 191 |
+
for i in range(1, iterations+1):
|
| 192 |
+
j = i * param1 - param2
|
| 193 |
+
result -= (1/j)
|
| 194 |
+
j = i * param1 + param2
|
| 195 |
+
result += (1/j)
|
| 196 |
+
return result
|
| 197 |
+
|
| 198 |
+
start_time = time.time()
|
| 199 |
+
result = calculate(100_000_000, 4, 1) * 4
|
| 200 |
+
end_time = time.time()
|
| 201 |
+
|
| 202 |
+
print(f"Result: {result:.12f}")
|
| 203 |
+
print(f"Execution Time: {(end_time - start_time):.6f} seconds")
|
| 204 |
+
"""
|
| 205 |
+
|
| 206 |
+
# Modern CSS
|
| 207 |
+
modern_css = """
|
| 208 |
+
.gradio-container, body, html {
|
| 209 |
+
background: #f9fafb !important; /* light background to avoid dark-mode conflicts */
|
| 210 |
+
color: #111827 !important; /* ensure readable text */
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
.gradio-container {
|
| 214 |
+
max-width: 1400px !important;
|
| 215 |
+
margin: 0 auto !important;
|
| 216 |
+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
.modern-header {
|
| 220 |
+
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
| 221 |
+
color: white;
|
| 222 |
+
padding: 24px;
|
| 223 |
+
border-radius: 16px;
|
| 224 |
+
margin-bottom: 24px;
|
| 225 |
+
text-align: center;
|
| 226 |
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
|
| 227 |
+
}
|
| 228 |
+
|
| 229 |
+
.modern-header h1 {
|
| 230 |
+
margin: 0;
|
| 231 |
+
font-size: 32px;
|
| 232 |
+
font-weight: 700;
|
| 233 |
+
letter-spacing: -0.5px;
|
| 234 |
+
}
|
| 235 |
+
|
| 236 |
+
.modern-header p {
|
| 237 |
+
margin: 12px 0 0 0;
|
| 238 |
+
opacity: 0.9;
|
| 239 |
+
font-size: 18px;
|
| 240 |
+
font-weight: 400;
|
| 241 |
+
}
|
| 242 |
+
|
| 243 |
+
.security-warning {
|
| 244 |
+
background: #fee2e2 !important;
|
| 245 |
+
border: 2px solid #dc2626 !important;
|
| 246 |
+
border-radius: 12px !important;
|
| 247 |
+
padding: 16px !important;
|
| 248 |
+
margin: 16px 0 !important;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
.python-input {
|
| 252 |
+
background: #f8fafc !important;
|
| 253 |
+
border: 2px solid #e2e8f0 !important;
|
| 254 |
+
border-radius: 12px !important;
|
| 255 |
+
padding: 16px !important;
|
| 256 |
+
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
|
| 257 |
+
font-size: 14px !important;
|
| 258 |
+
color: #1e293b !important;
|
| 259 |
+
line-height: 1.5 !important;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
.python-input:focus {
|
| 263 |
+
border-color: #3b82f6 !important;
|
| 264 |
+
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
.cpp-output {
|
| 268 |
+
background: #f1f5f9 !important;
|
| 269 |
+
border: 2px solid #cbd5e1 !important;
|
| 270 |
+
border-radius: 12px !important;
|
| 271 |
+
padding: 16px !important;
|
| 272 |
+
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace !important;
|
| 273 |
+
font-size: 14px !important;
|
| 274 |
+
color: #0f172a !important;
|
| 275 |
+
line-height: 1.5 !important;
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
.model-selector {
|
| 279 |
+
background: white !important;
|
| 280 |
+
border: 2px solid #e2e8f0 !important;
|
| 281 |
+
border-radius: 12px !important;
|
| 282 |
+
padding: 12px 16px !important;
|
| 283 |
+
font-size: 16px !important;
|
| 284 |
+
color: #374151 !important;
|
| 285 |
+
}
|
| 286 |
+
|
| 287 |
+
.model-selector:focus {
|
| 288 |
+
border-color: #3b82f6 !important;
|
| 289 |
+
box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1) !important;
|
| 290 |
+
}
|
| 291 |
+
|
| 292 |
+
.modern-button {
|
| 293 |
+
background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%) !important;
|
| 294 |
+
color: white !important;
|
| 295 |
+
border: none !important;
|
| 296 |
+
border-radius: 12px !important;
|
| 297 |
+
padding: 14px 28px !important;
|
| 298 |
+
font-weight: 600 !important;
|
| 299 |
+
font-size: 16px !important;
|
| 300 |
+
cursor: pointer !important;
|
| 301 |
+
transition: all 0.2s ease !important;
|
| 302 |
+
box-shadow: 0 4px 6px rgba(59, 130, 246, 0.2) !important;
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
+
.modern-button:hover {
|
| 306 |
+
transform: translateY(-2px) !important;
|
| 307 |
+
box-shadow: 0 8px 12px rgba(59, 130, 246, 0.3) !important;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
.run-button {
|
| 311 |
+
background: linear-gradient(135deg, #10b981 0%, #059669 100%) !important;
|
| 312 |
+
color: white !important;
|
| 313 |
+
border: none !important;
|
| 314 |
+
border-radius: 10px !important;
|
| 315 |
+
padding: 12px 24px !important;
|
| 316 |
+
font-weight: 600 !important;
|
| 317 |
+
font-size: 14px !important;
|
| 318 |
+
cursor: pointer !important;
|
| 319 |
+
transition: all 0.2s ease !important;
|
| 320 |
+
box-shadow: 0 4px 6px rgba(16, 185, 129, 0.2) !important;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
.run-button:hover {
|
| 324 |
+
transform: translateY(-1px) !important;
|
| 325 |
+
box-shadow: 0 6px 8px rgba(16, 185, 129, 0.3) !important;
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
.output-section {
|
| 329 |
+
background: #f8fafc;
|
| 330 |
+
border: 1px solid #e2e8f0;
|
| 331 |
+
border-radius: 12px;
|
| 332 |
+
padding: 16px;
|
| 333 |
+
margin: 12px 0;
|
| 334 |
+
font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
|
| 335 |
+
font-size: 13px;
|
| 336 |
+
line-height: 1.4;
|
| 337 |
+
color: #374151;
|
| 338 |
+
min-height: 100px;
|
| 339 |
+
overflow-y: auto;
|
| 340 |
+
}
|
| 341 |
+
|
| 342 |
+
.python-output {
|
| 343 |
+
background: #fef3c7 !important;
|
| 344 |
+
border: 2px solid #f59e0b !important;
|
| 345 |
+
color: #92400e !important;
|
| 346 |
+
}
|
| 347 |
+
|
| 348 |
+
.cpp-output-result {
|
| 349 |
+
background: #dbeafe !important;
|
| 350 |
+
border: 2px solid #3b82f6 !important;
|
| 351 |
+
color: #1e40af !important;
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
.performance-card {
|
| 355 |
+
background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
|
| 356 |
+
border: 1px solid #0ea5e9;
|
| 357 |
+
border-radius: 12px;
|
| 358 |
+
padding: 20px;
|
| 359 |
+
margin: 16px 0;
|
| 360 |
+
text-align: center;
|
| 361 |
+
}
|
| 362 |
+
|
| 363 |
+
.performance-card h3 {
|
| 364 |
+
margin: 0 0 12px 0;
|
| 365 |
+
color: #0c4a6e;
|
| 366 |
+
font-size: 18px;
|
| 367 |
+
font-weight: 600;
|
| 368 |
+
}
|
| 369 |
+
|
| 370 |
+
.performance-metric {
|
| 371 |
+
display: inline-block;
|
| 372 |
+
background: white;
|
| 373 |
+
border-radius: 8px;
|
| 374 |
+
padding: 8px 16px;
|
| 375 |
+
margin: 4px;
|
| 376 |
+
font-weight: 600;
|
| 377 |
+
color: #0c4a6e;
|
| 378 |
+
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
|
| 379 |
+
}
|
| 380 |
+
|
| 381 |
+
/* Login styling */
|
| 382 |
+
body, .gradio-container {
|
| 383 |
+
background: url('assets/3656064.jpg') center/cover no-repeat fixed !important;
|
| 384 |
+
}
|
| 385 |
+
|
| 386 |
+
.login-wrapper {
|
| 387 |
+
display: flex;
|
| 388 |
+
align-items: center;
|
| 389 |
+
justify-content: center;
|
| 390 |
+
min-height: 100vh;
|
| 391 |
+
padding: 24px;
|
| 392 |
+
background: linear-gradient(rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0.4)), url('assets/3656064.jpg') center/cover no-repeat fixed;
|
| 393 |
+
}
|
| 394 |
+
|
| 395 |
+
.login-card {
|
| 396 |
+
max-width: 400px;
|
| 397 |
+
width: 100%;
|
| 398 |
+
background: rgba(255, 255, 255, 0.95);
|
| 399 |
+
backdrop-filter: blur(10px);
|
| 400 |
+
border: 1px solid rgba(255, 255, 255, 0.3);
|
| 401 |
+
border-radius: 20px;
|
| 402 |
+
padding: 48px 40px;
|
| 403 |
+
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
|
| 404 |
+
}
|
| 405 |
+
|
| 406 |
+
.login-icon {
|
| 407 |
+
font-size: 56px;
|
| 408 |
+
text-align: center;
|
| 409 |
+
margin-bottom: 16px;
|
| 410 |
+
filter: drop-shadow(0 2px 4px rgba(0,0,0,0.1));
|
| 411 |
+
}
|
| 412 |
+
|
| 413 |
+
.login-title {
|
| 414 |
+
text-align: center;
|
| 415 |
+
margin: 0 0 8px 0;
|
| 416 |
+
font-size: 28px;
|
| 417 |
+
font-weight: 700;
|
| 418 |
+
color: #1f2937;
|
| 419 |
+
letter-spacing: -0.02em;
|
| 420 |
+
}
|
| 421 |
+
|
| 422 |
+
.login-subtitle {
|
| 423 |
+
text-align: center;
|
| 424 |
+
margin: 0 0 32px 0;
|
| 425 |
+
font-size: 15px;
|
| 426 |
+
color: #6b7280;
|
| 427 |
+
font-weight: 400;
|
| 428 |
+
}
|
| 429 |
+
|
| 430 |
+
.login-input label {
|
| 431 |
+
color: #374151 !important;
|
| 432 |
+
font-weight: 600 !important;
|
| 433 |
+
font-size: 14px !important;
|
| 434 |
+
}
|
| 435 |
+
|
| 436 |
+
.login-input input {
|
| 437 |
+
background: white !important;
|
| 438 |
+
border: 2px solid #e5e7eb !important;
|
| 439 |
+
border-radius: 10px !important;
|
| 440 |
+
padding: 12px 16px !important;
|
| 441 |
+
font-size: 16px !important;
|
| 442 |
+
color: #111827 !important;
|
| 443 |
+
}
|
| 444 |
+
|
| 445 |
+
.login-input input:focus {
|
| 446 |
+
border-color: #667eea !important;
|
| 447 |
+
background: white !important;
|
| 448 |
+
outline: none !important;
|
| 449 |
+
box-shadow: 0 0 0 4px rgba(102, 126, 234, 0.1) !important;
|
| 450 |
+
}
|
| 451 |
+
|
| 452 |
+
.login-error {
|
| 453 |
+
color: #dc2626;
|
| 454 |
+
background: #fee2e2;
|
| 455 |
+
border: 1px solid #fca5a5;
|
| 456 |
+
padding: 12px;
|
| 457 |
+
border-radius: 10px;
|
| 458 |
+
text-align: center;
|
| 459 |
+
font-weight: 500;
|
| 460 |
+
}
|
| 461 |
+
|
| 462 |
+
.freepik-credit {
|
| 463 |
+
margin-top: 24px;
|
| 464 |
+
text-align: center;
|
| 465 |
+
font-size: 12px;
|
| 466 |
+
color: #9ca3af;
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
.freepik-credit a {
|
| 470 |
+
color: #667eea;
|
| 471 |
+
text-decoration: none;
|
| 472 |
+
font-weight: 500;
|
| 473 |
+
}
|
| 474 |
+
|
| 475 |
+
.freepik-credit a:hover {
|
| 476 |
+
text-decoration: underline;
|
| 477 |
+
}
|
| 478 |
+
"""
|
| 479 |
+
|
| 480 |
+
# Create the interface with password protection
|
| 481 |
+
def create_interface():
|
| 482 |
+
with gr.Blocks(css=modern_css, title="Python to C++ Code Optimizer", theme=gr.themes.Soft()) as app:
|
| 483 |
+
authorized = gr.State(False)
|
| 484 |
+
# Background image is handled via CSS: assets/3656064.jpg
|
| 485 |
+
# For Hugging Face Space: use /file=assets/3656064.jpg in CSS
|
| 486 |
+
# For local: use assets/3656064.jpg in CSS
|
| 487 |
+
|
| 488 |
+
def check_password(pw):
|
| 489 |
+
ok = pw == APP_PASSWORD
|
| 490 |
+
return (
|
| 491 |
+
gr.update(visible=not ok), # login hidden when ok
|
| 492 |
+
gr.update(visible=ok), # main shown when ok
|
| 493 |
+
gr.update(value="" if ok else "Invalid password", visible=not ok)
|
| 494 |
+
)
|
| 495 |
+
|
| 496 |
+
# Login gate
|
| 497 |
+
with gr.Group(visible=True) as login_group:
|
| 498 |
+
gr.HTML("""
|
| 499 |
+
<div class="login-wrapper">
|
| 500 |
+
<div class="login-card">
|
| 501 |
+
<div class="login-icon">π</div>
|
| 502 |
+
<div class="login-title">Private Access</div>
|
| 503 |
+
<div class="login-subtitle">Enter password to continue</div>
|
| 504 |
+
""")
|
| 505 |
+
pw = gr.Textbox(
|
| 506 |
+
label="Password",
|
| 507 |
+
type="password",
|
| 508 |
+
placeholder="Enter password",
|
| 509 |
+
elem_classes=["login-input"],
|
| 510 |
+
container=True
|
| 511 |
+
)
|
| 512 |
+
login_btn = gr.Button("Continue", elem_classes=["modern-button"], size="lg")
|
| 513 |
+
login_error = gr.Markdown(visible=False, elem_classes=["login-error"])
|
| 514 |
+
gr.HTML("""
|
| 515 |
+
<div class="freepik-credit">Background image by <a href="https://www.freepik.com" target="_blank">Freepik</a></div>
|
| 516 |
+
</div>
|
| 517 |
+
</div>
|
| 518 |
+
""")
|
| 519 |
+
|
| 520 |
+
# Main UI wrapped for toggling visibility
|
| 521 |
+
with gr.Group(visible=False) as main_group:
|
| 522 |
+
main_group.elem_id = "main_group"
|
| 523 |
+
# Header Section
|
| 524 |
+
gr.HTML("""
|
| 525 |
+
<div class="modern-header">
|
| 526 |
+
<h1>π Python to C++ Code Optimizer</h1>
|
| 527 |
+
<p>AI-powered code conversion with real-time execution and performance analysis</p>
|
| 528 |
+
</div>
|
| 529 |
+
""")
|
| 530 |
+
|
| 531 |
+
# Security Warning
|
| 532 |
+
gr.HTML("""
|
| 533 |
+
<div class="security-warning">
|
| 534 |
+
<h3 style="color: #dc2626; margin: 0 0 8px 0;">β οΈ Security Warning</h3>
|
| 535 |
+
<p style="margin: 0; color: #991b1b; font-weight: 500;">
|
| 536 |
+
This interface executes arbitrary code. <strong>Only run code from trusted sources.</strong><br>
|
| 537 |
+
Malicious code can harm your system. Use at your own risk.
|
| 538 |
+
</p>
|
| 539 |
+
</div>
|
| 540 |
+
""")
|
| 541 |
+
|
| 542 |
+
# Main Content Area
|
| 543 |
+
with gr.Row():
|
| 544 |
+
with gr.Column(scale=1):
|
| 545 |
+
gr.Markdown("### π Python Code Input")
|
| 546 |
+
python_input = gr.Textbox(
|
| 547 |
+
label="Python Code:",
|
| 548 |
+
value=default_python,
|
| 549 |
+
lines=15,
|
| 550 |
+
elem_classes=["python-input"],
|
| 551 |
+
placeholder="Enter your Python code here..."
|
| 552 |
+
)
|
| 553 |
+
|
| 554 |
+
with gr.Row():
|
| 555 |
+
model_selector = gr.Dropdown(
|
| 556 |
+
["GPT-4o", "Claude-3.5-Sonnet"],
|
| 557 |
+
label="Select AI Model",
|
| 558 |
+
value="GPT-4o",
|
| 559 |
+
elem_classes=["model-selector"]
|
| 560 |
+
)
|
| 561 |
+
|
| 562 |
+
convert_button = gr.Button("β¨ Convert to C++", elem_classes=["modern-button"])
|
| 563 |
+
|
| 564 |
+
with gr.Column(scale=1):
|
| 565 |
+
gr.Markdown("### β‘ Optimized C++ Code")
|
| 566 |
+
cpp_output = gr.Textbox(
|
| 567 |
+
label="Generated C++ Code:",
|
| 568 |
+
lines=15,
|
| 569 |
+
elem_classes=["cpp-output"],
|
| 570 |
+
interactive=False
|
| 571 |
+
)
|
| 572 |
+
|
| 573 |
+
# Execution Section
|
| 574 |
+
gr.Markdown("---")
|
| 575 |
+
gr.Markdown("## π Code Execution & Performance Comparison")
|
| 576 |
+
|
| 577 |
+
with gr.Row():
|
| 578 |
+
with gr.Column():
|
| 579 |
+
gr.Markdown("### π Python Output")
|
| 580 |
+
run_python_button = gr.Button("βΆοΈ Run Python", elem_classes=["run-button"])
|
| 581 |
+
python_output = gr.Textbox(
|
| 582 |
+
label="Python Execution Output:",
|
| 583 |
+
lines=5,
|
| 584 |
+
elem_classes=["python-output"],
|
| 585 |
+
interactive=False
|
| 586 |
+
)
|
| 587 |
+
|
| 588 |
+
with gr.Column():
|
| 589 |
+
gr.Markdown("### β‘ C++ Output")
|
| 590 |
+
run_cpp_button = gr.Button("βΆοΈ Run C++", elem_classes=["run-button"])
|
| 591 |
+
cpp_execution_output = gr.Textbox(
|
| 592 |
+
label="C++ Execution Output:",
|
| 593 |
+
lines=5,
|
| 594 |
+
elem_classes=["cpp-output-result"],
|
| 595 |
+
interactive=False
|
| 596 |
+
)
|
| 597 |
+
|
| 598 |
+
# Event handlers
|
| 599 |
+
convert_button.click(
|
| 600 |
+
fn=optimize,
|
| 601 |
+
inputs=[python_input, model_selector],
|
| 602 |
+
outputs=cpp_output
|
| 603 |
+
)
|
| 604 |
+
|
| 605 |
+
run_python_button.click(
|
| 606 |
+
fn=execute_python,
|
| 607 |
+
inputs=python_input,
|
| 608 |
+
outputs=python_output
|
| 609 |
+
)
|
| 610 |
+
|
| 611 |
+
run_cpp_button.click(
|
| 612 |
+
fn=execute_cpp,
|
| 613 |
+
inputs=cpp_output,
|
| 614 |
+
outputs=cpp_execution_output
|
| 615 |
+
)
|
| 616 |
+
|
| 617 |
+
# Bind login after main_group is defined
|
| 618 |
+
login_btn.click(
|
| 619 |
+
fn=check_password,
|
| 620 |
+
inputs=[pw],
|
| 621 |
+
outputs=[login_group, main_group, login_error]
|
| 622 |
+
)
|
| 623 |
+
|
| 624 |
+
return app
|
| 625 |
+
|
| 626 |
+
# Launch the app
|
| 627 |
+
if __name__ == "__main__":
|
| 628 |
+
print("\n" + "="*50)
|
| 629 |
+
print(f"===== Application Startup at {__import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
|
| 630 |
+
print("="*50)
|
| 631 |
+
|
| 632 |
+
# Create the app
|
| 633 |
+
app = create_interface()
|
| 634 |
+
|
| 635 |
+
# Check if running on Hugging Face Spaces
|
| 636 |
+
is_huggingface = os.getenv("SPACE_ID") is not None
|
| 637 |
+
|
| 638 |
+
if is_huggingface:
|
| 639 |
+
# Hugging Face Spaces configuration
|
| 640 |
+
print("π Launching Python to C++ Code Optimizer on Hugging Face Spaces")
|
| 641 |
+
print("π Password protection enabled")
|
| 642 |
+
app.launch(
|
| 643 |
+
show_error=True
|
| 644 |
+
)
|
| 645 |
+
else:
|
| 646 |
+
# Local development configuration
|
| 647 |
+
def get_available_port(start_port=7860):
|
| 648 |
+
"""Find an available port starting from start_port"""
|
| 649 |
+
port = start_port
|
| 650 |
+
while port < start_port + 100:
|
| 651 |
+
try:
|
| 652 |
+
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 653 |
+
s.bind(('', port))
|
| 654 |
+
return port
|
| 655 |
+
except OSError:
|
| 656 |
+
port += 1
|
| 657 |
+
return start_port
|
| 658 |
+
|
| 659 |
+
port = get_available_port()
|
| 660 |
+
print(f"π Launching Python to C++ Code Optimizer on port: {port}")
|
| 661 |
+
print(f"π Password protection enabled. Password: {APP_PASSWORD}")
|
| 662 |
+
|
| 663 |
+
app.launch(
|
| 664 |
+
server_name="127.0.0.1",
|
| 665 |
+
server_port=port,
|
| 666 |
+
show_error=True
|
| 667 |
+
)
|
| 668 |
+
|