Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,656 +0,0 @@
|
|
| 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: linear-gradient(135deg, #667eea 0%, #764ba2 100%) !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(135deg, #667eea 0%, #764ba2 100%);
|
| 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 custom login that SHOWS the boxes
|
| 481 |
-
def create_interface():
|
| 482 |
-
with gr.Blocks(css=modern_css, title="Python to C++ Code Optimizer", theme=gr.themes.Soft()) as app:
|
| 483 |
-
|
| 484 |
-
def check_password(pw):
|
| 485 |
-
if pw == APP_PASSWORD:
|
| 486 |
-
return gr.update(visible=False), gr.update(visible=True), gr.update(visible=False)
|
| 487 |
-
else:
|
| 488 |
-
return gr.update(visible=True), gr.update(visible=False), gr.update(value="❌ Wrong password", visible=True)
|
| 489 |
-
|
| 490 |
-
# Login Section - NO HTML wrapper around Gradio components!
|
| 491 |
-
with gr.Column(visible=True, elem_classes=["login-wrapper"]) as login_group:
|
| 492 |
-
gr.HTML("""
|
| 493 |
-
<div style="text-align: center; margin: 60px auto 40px; max-width: 400px;">
|
| 494 |
-
<div style="font-size: 56px; margin-bottom: 16px;">🔐</div>
|
| 495 |
-
<div style="font-size: 28px; font-weight: 700; color: #1f2937; margin-bottom: 8px;">Private Access</div>
|
| 496 |
-
<div style="font-size: 15px; color: #6b7280; margin-bottom: 32px;">Enter password to continue</div>
|
| 497 |
-
</div>
|
| 498 |
-
""")
|
| 499 |
-
pw = gr.Textbox(
|
| 500 |
-
label="Password",
|
| 501 |
-
type="password",
|
| 502 |
-
placeholder="Enter password",
|
| 503 |
-
scale=1,
|
| 504 |
-
container=True
|
| 505 |
-
)
|
| 506 |
-
login_btn = gr.Button("Continue", variant="primary", size="lg")
|
| 507 |
-
login_error = gr.Markdown("", visible=False)
|
| 508 |
-
|
| 509 |
-
# Main App Section
|
| 510 |
-
with gr.Column(visible=False) as main_group:
|
| 511 |
-
# Header Section
|
| 512 |
-
gr.HTML("""
|
| 513 |
-
<div class="modern-header">
|
| 514 |
-
<h1>🚀 Python to C++ Code Optimizer</h1>
|
| 515 |
-
<p>AI-powered code conversion with real-time execution and performance analysis</p>
|
| 516 |
-
</div>
|
| 517 |
-
""")
|
| 518 |
-
|
| 519 |
-
# Security Warning
|
| 520 |
-
gr.HTML("""
|
| 521 |
-
<div class="security-warning">
|
| 522 |
-
<h3 style="color: #dc2626; margin: 0 0 8px 0;">⚠️ Security Warning</h3>
|
| 523 |
-
<p style="margin: 0; color: #991b1b; font-weight: 500;">
|
| 524 |
-
This interface executes arbitrary code. <strong>Only run code from trusted sources.</strong><br>
|
| 525 |
-
Malicious code can harm your system. Use at your own risk.
|
| 526 |
-
</p>
|
| 527 |
-
</div>
|
| 528 |
-
""")
|
| 529 |
-
|
| 530 |
-
# Main Content Area
|
| 531 |
-
with gr.Row():
|
| 532 |
-
with gr.Column(scale=1):
|
| 533 |
-
gr.Markdown("### 📝 Python Code Input")
|
| 534 |
-
python_input = gr.Textbox(
|
| 535 |
-
label="Python Code:",
|
| 536 |
-
value=default_python,
|
| 537 |
-
lines=15,
|
| 538 |
-
elem_classes=["python-input"],
|
| 539 |
-
placeholder="Enter your Python code here..."
|
| 540 |
-
)
|
| 541 |
-
|
| 542 |
-
with gr.Row():
|
| 543 |
-
model_selector = gr.Dropdown(
|
| 544 |
-
["GPT-4o", "Claude-3.5-Sonnet"],
|
| 545 |
-
label="Select AI Model",
|
| 546 |
-
value="GPT-4o",
|
| 547 |
-
elem_classes=["model-selector"]
|
| 548 |
-
)
|
| 549 |
-
|
| 550 |
-
convert_button = gr.Button("✨ Convert to C++", elem_classes=["modern-button"])
|
| 551 |
-
|
| 552 |
-
with gr.Column(scale=1):
|
| 553 |
-
gr.Markdown("### ⚡ Optimized C++ Code")
|
| 554 |
-
cpp_output = gr.Textbox(
|
| 555 |
-
label="Generated C++ Code:",
|
| 556 |
-
lines=15,
|
| 557 |
-
elem_classes=["cpp-output"],
|
| 558 |
-
interactive=False
|
| 559 |
-
)
|
| 560 |
-
|
| 561 |
-
# Execution Section
|
| 562 |
-
gr.Markdown("---")
|
| 563 |
-
gr.Markdown("## 🏃 Code Execution & Performance Comparison")
|
| 564 |
-
|
| 565 |
-
with gr.Row():
|
| 566 |
-
with gr.Column():
|
| 567 |
-
gr.Markdown("### 🐍 Python Output")
|
| 568 |
-
run_python_button = gr.Button("▶️ Run Python", elem_classes=["run-button"])
|
| 569 |
-
python_output = gr.Textbox(
|
| 570 |
-
label="Python Execution Output:",
|
| 571 |
-
lines=5,
|
| 572 |
-
elem_classes=["python-output"],
|
| 573 |
-
interactive=False
|
| 574 |
-
)
|
| 575 |
-
|
| 576 |
-
with gr.Column():
|
| 577 |
-
gr.Markdown("### ⚡ C++ Output")
|
| 578 |
-
run_cpp_button = gr.Button("▶️ Run C++", elem_classes=["run-button"])
|
| 579 |
-
cpp_execution_output = gr.Textbox(
|
| 580 |
-
label="C++ Execution Output:",
|
| 581 |
-
lines=5,
|
| 582 |
-
elem_classes=["cpp-output-result"],
|
| 583 |
-
interactive=False
|
| 584 |
-
)
|
| 585 |
-
|
| 586 |
-
# Event handlers
|
| 587 |
-
convert_button.click(
|
| 588 |
-
fn=optimize,
|
| 589 |
-
inputs=[python_input, model_selector],
|
| 590 |
-
outputs=cpp_output
|
| 591 |
-
)
|
| 592 |
-
|
| 593 |
-
run_python_button.click(
|
| 594 |
-
fn=execute_python,
|
| 595 |
-
inputs=python_input,
|
| 596 |
-
outputs=python_output
|
| 597 |
-
)
|
| 598 |
-
|
| 599 |
-
run_cpp_button.click(
|
| 600 |
-
fn=execute_cpp,
|
| 601 |
-
inputs=cpp_output,
|
| 602 |
-
outputs=cpp_execution_output
|
| 603 |
-
)
|
| 604 |
-
|
| 605 |
-
# Connect login button
|
| 606 |
-
login_btn.click(
|
| 607 |
-
fn=check_password,
|
| 608 |
-
inputs=[pw],
|
| 609 |
-
outputs=[login_group, main_group, login_error]
|
| 610 |
-
)
|
| 611 |
-
|
| 612 |
-
return app
|
| 613 |
-
|
| 614 |
-
# Launch the app
|
| 615 |
-
if __name__ == "__main__":
|
| 616 |
-
print("\n" + "="*50)
|
| 617 |
-
print(f"===== Application Startup at {__import__('datetime').datetime.now().strftime('%Y-%m-%d %H:%M:%S')} =====")
|
| 618 |
-
print("="*50)
|
| 619 |
-
|
| 620 |
-
# Create the app
|
| 621 |
-
app = create_interface()
|
| 622 |
-
|
| 623 |
-
# Check if running on Hugging Face Spaces
|
| 624 |
-
is_huggingface = os.getenv("SPACE_ID") is not None
|
| 625 |
-
|
| 626 |
-
if is_huggingface:
|
| 627 |
-
# Hugging Face Spaces configuration
|
| 628 |
-
print("🚀 Launching Python to C++ Code Optimizer on Hugging Face Spaces")
|
| 629 |
-
print("🔐 Custom in-app password protection enabled")
|
| 630 |
-
app.launch(
|
| 631 |
-
show_error=True
|
| 632 |
-
)
|
| 633 |
-
else:
|
| 634 |
-
# Local development configuration
|
| 635 |
-
def get_available_port(start_port=7860):
|
| 636 |
-
"""Find an available port starting from start_port"""
|
| 637 |
-
port = start_port
|
| 638 |
-
while port < start_port + 100:
|
| 639 |
-
try:
|
| 640 |
-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
|
| 641 |
-
s.bind(('', port))
|
| 642 |
-
return port
|
| 643 |
-
except OSError:
|
| 644 |
-
port += 1
|
| 645 |
-
return start_port
|
| 646 |
-
|
| 647 |
-
port = get_available_port()
|
| 648 |
-
print(f"🚀 Launching Python to C++ Code Optimizer on port: {port}")
|
| 649 |
-
print(f"🔐 Custom in-app password protection enabled. Password: {APP_PASSWORD}")
|
| 650 |
-
|
| 651 |
-
app.launch(
|
| 652 |
-
server_name="127.0.0.1",
|
| 653 |
-
server_port=port,
|
| 654 |
-
show_error=True
|
| 655 |
-
)
|
| 656 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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