File size: 2,601 Bytes
01e3dd5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Program Try App - Python Backend
Docker version for HuggingFace Spaces
"""

import gradio as gr
from pydantic import BaseModel
from typing import Optional
import subprocess
import sys

# Models
class Project(BaseModel):
    id: str
    name: str
    type: str
    content: Optional[str] = ""

# Language configurations
LANGUAGES = {
    "python": {"run": lambda code: run_python(code)},
    "html": {"run": lambda code: run_html(code)},
    "css": {"run": lambda code: run_css(code)},
    "javascript": {"run": lambda code: run_javascript(code)},
    "react": {"run": lambda code: run_react(code)},
    "nextjs": {"run": lambda code: run_nextjs(code)}
}

def run_python(code: str) -> str:
    try:
        result = subprocess.run([sys.executable, "-c", code], capture_output=True, text=True, timeout=5)
        return result.stdout if result.returncode == 0 else f"Error: {result.stderr}"
    except Exception as e:
        return f"Error: {str(e)}"

def run_html(code: str) -> str:
    if "<html" in code.lower() or "<!DOCTYPE" in code.upper():
        return "✓ Valid HTML"
    return "Warning: HTML not found"

def run_css(code: str) -> str:
    if "{" in code and "}" in code:
        return "✓ Valid CSS"
    return "Warning: CSS not found"

def run_javascript(code: str) -> str:
    return f"JavaScript validated:\n{code[:200]}"

def run_react(code: str) -> str:
    return "✓ Valid React" if "react" in code.lower() else "Warning: React not found"

def run_nextjs(code: str) -> str:
    return "✓ Valid Next.js" if "next" in code.lower() else "Warning: Next.js not found"

def process_code(language: str, code: str) -> str:
    if language not in LANGUAGES:
        return f"Unknown: {language}"
    try:
        return LANGUAGES[language]["run"](code)
    except Exception as e:
        return f"Error: {str(e)}"

# Gradio Interface
with gr.Blocks(title="Program Try App", theme=gr.themes.Soft()) as demo:
    gr.Markdown("# 🖥️ Program Try App - Language Tester")
    
    with gr.Tab("💻 Code Tester"):
        lang_input = gr.Dropdown(choices=list(LANGUAGES.keys()), label="Language", value="python")
        code_input = gr.Textbox(label="Code", lines=10, placeholder="Write code here...")
        submit_btn = gr.Button("🟢 Run", variant="primary")
        output = gr.Textbox(label="Output", lines=5)
        submit_btn.click(process_code, inputs=[lang_input, code_input], outputs=output)
        
    with gr.Tab("ℹ️ About"):
        gr.Markdown("## About\n- Version: 1.0.0\n- Author: AMKyawDev")

demo.launch(server_name="0.0.0.0", server_port=7860, share=True)