File size: 6,088 Bytes
defda2f
 
239f89f
 
 
 
 
 
 
fa13896
239f89f
e1a951c
defda2f
239f89f
 
 
 
 
 
 
 
 
 
 
024be8a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a13940c
 
3a0cbad
 
968b855
 
 
 
 
 
 
 
 
 
a13940c
968b855
 
 
 
 
 
 
 
a13940c
 
024be8a
3a0cbad
7829d42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
024be8a
 
 
 
239f89f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66d4d97
239f89f
6556df6
239f89f
 
 
66d4d97
239f89f
e1a951c
239f89f
e1a951c
239f89f
 
 
e1a951c
 
239f89f
defda2f
e1a951c
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
import gradio as gr

# βœ… Placeholder AI logic (will upgrade to real LLM next)
def generate_code(task, language="Python"):
    boilerplate = {
        "Python": "# Python example\n\ndef main():\n    print('Hello World')\n\nmain()",
        "JavaScript": "// JavaScript example\n\nconsole.log('Hello World');",
        "Java": "public class Main {\n    public static void main(String[] args) {\n        System.out.println('Hello World');\n    }\n}",
        "Windows Batch": "@echo off\necho Hello World\npause"
    }
    return f"// Generated Task: {task}\n\n{boilerplate.get(language, '// Language not supported yet')}"

def explain_code(code):
    return "🧠 This code prints a greeting message.\nI will provide deeper analysis in the upgraded AI version."

def fix_code(code):
    return "βœ… Fixed Code:\n" + code.replace(";;", ";").replace("pass", "# TODO: add logic")

def chat(message, history):
    history = history or []
    reply = f"πŸ€– AI Programmer: I understand your message:\n{message}\n\nTell me what you want to build!"
    history.append((message, reply))
    return history

def chat_handler(message, history):
    """
    message: str from textbox
    history: existing chat history list (list of tuples/lists)
    Must return: updated history (list of tuples/lists)
    """
    history = history or []

    # sanitize input
    msg = (message or "").strip()
    if not msg:
        history.append(("", "Please type a question or command."))
        return history

    # simple keyword-driven assistant (expand to real LLM later)
    low = msg.lower()
    if "hello" in low or "hi" in low:
        reply = "Hello! πŸ‘‹ What would you like to build or debug today?"
    elif "generate" in low or "build" in low:
        reply = "Tell me the language and a short description (e.g., 'python: web scraper that saves to csv')."
    elif "fix" in low or "bug" in low:
        reply = "Paste the code (or error message) and I'll suggest fixes and diagnostics."
    elif "explain" in low or "what does" in low:
        reply = "Paste the code you want explained and I'll walk through it step-by-step."
    else:
        # fallback behavior β€” useful when switching to a real model later
        reply = "I can generate, explain, and fix code. Try commands like: `generate`, `fix`, or `explain`."

    # Append user message and bot reply as a tuple (user, bot)
    history.append((message, reply))
    return history

# UI wiring for the Chat tab
with gr.Blocks() as app:

   with gr.Tab("Chat"):  # line 60
    # everything inside the tab must be indented
    chatbot = gr.Chatbot(label="Programming Assistant", type="messages")
    chat_input = gr.Textbox(label="Ask anything...", placeholder="Type a question here...")
    send_btn = gr.Button("Send")
    clear_btn = gr.Button("Clear")

    def chat_handler(message, history):
        history = history or []
        msg = message.strip()
        if not msg:
            history.append({"role": "assistant", "content": "Please type a message."})
            return history
        history.append({"role": "user", "content": msg})
        reply = f"I heard you: {msg}"
        history.append({"role": "assistant", "content": reply})
        return history

    send_btn.click(chat_handler, [chat_input, chatbot], chatbot)
    chat_input.submit(chat_handler, [chat_input, chatbot], chatbot)
    clear_btn.click(lambda: [], None, chatbot)




    # Callback for chat
    def chat_handler(message, history):
        history = history or []
        msg = (message or "").strip()
        if not msg:
            history.append({"role": "assistant", "content": "Please type a question!"})
            return history

        # Simple AI placeholder logic
        low = msg.lower()
        if "generate" in low:
            reply = "Tell me the language and a short description, e.g. 'python: web scraper'"
        elif "fix" in low:
            reply = "Paste your code and error message; I can suggest fixes."
        else:
            reply = "I can generate, explain, and fix code. Try: generate, fix, explain."

        # Use 'messages' format
        history.append({"role": "user", "content": message})
        history.append({"role": "assistant", "content": reply})
        return history

    # Wire buttons and submit inside Blocks context
    send_btn.click(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot)
    chat_input.submit(chat_handler, inputs=[chat_input, chatbot], outputs=chatbot)
    clear_btn.click(lambda: [], None, chatbot, queue=False)


# βœ… UI Layout
with gr.Blocks(title="Synthetic AI Coder") as app:

    gr.Markdown(
        """
        # πŸ€– Synthetic AI Coder
        Build real software β€” Windows, Python, JavaScript, Java.
        """
    )

    with gr.Tab("✨ Generate Code"):
        task = gr.Textbox(label="Describe the program you want")
        lang = gr.Dropdown(
            ["Python", "JavaScript", "Java", "Windows Batch"],
            label="Language",
            value="Python"
        )
        output = gr.Code(label="Generated Code", language="python")
        generate_btn = gr.Button("Generate Software Code")
        generate_btn.click(generate_code, [task, lang], output)

    with gr.Tab("πŸ“˜ Explain Code"):
        code_input = gr.Textbox(label="Paste code here", lines=10)
        explanation = gr.Textbox(label="Explanation", lines=10)
        explain_btn = gr.Button("Explain Code")
        explain_btn.click(explain_code, code_input, explanation)

    with gr.Tab("πŸ›  Fix/Improve Code"):
        broken = gr.Textbox(label="Paste broken code", lines=10)
        fixed = gr.Code(label="Fixed Code")
        fix_btn = gr.Button("Fix Code")
        fix_btn.click(fix_code, broken, fixed)

    with gr.Tab("πŸ’¬ AI Programming Chat"):
        chatbot = gr.Chatbot()
        chat_input = gr.Textbox(label="Ask about software development")
        chat_input.submit(chat, [chat_input, chatbot], chatbot)
        gr.Button("Clear").click(lambda: [], None, chatbot)


# βœ… Required for Hugging Face Spaces
if __name__ == "__main__":
    app.launch()