File size: 6,206 Bytes
c49fd47
 
 
26122e6
 
 
 
 
 
 
 
 
c49fd47
26122e6
 
 
 
 
c49fd47
 
26122e6
c49fd47
26122e6
c49fd47
 
26122e6
c49fd47
26122e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c49fd47
26122e6
 
c49fd47
26122e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c49fd47
26122e6
 
 
c49fd47
26122e6
c49fd47
26122e6
 
 
 
 
 
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
import gradio as gr
import os

print("🚀 AI Coding Agent starting...")

# Simple workspace
WORKSPACE = "workspace"
os.makedirs(WORKSPACE, exist_ok=True)

# AI Assistant function
def ai_chat(message, history):
    """Simple AI response"""
    responses = {
        "hello": "👋 Hello! I'm your AI Coding Assistant! Ready to help with programming.",
        "help": "I can help with:\n• Writing code (Python/JS)\n• Code reviews\n• Debugging\n• Explanations\n• File operations",
        "python": "```python\nprint('Hello from Python!')\n```",
        "write": "```python\ndef greet(name):\n    return f'Hello, {name}!'\n```",
        "test": "✅ Agent is working correctly!",
    }
    
    msg = message.lower()
    for key in responses:
        if key in msg:
            return responses[key]
    
    return f"🤖 I can help with: {message}\n\nTry: 'Write a function', 'Explain loops', or 'Help debug'"

# File operations
def list_files():
    try:
        files = os.listdir(WORKSPACE)
        if files:
            return "\\n".join([f for f in files if not f.startswith('.')])
        return "📁 Workspace is empty"
    except:
        return "📁 Workspace is empty"

def save_file(filename, content):
    try:
        if not filename:
            return "Please enter a filename", list_files()
        with open(os.path.join(WORKSPACE, filename), 'w') as f:
            f.write(content)
        return f"✅ Saved: {filename}", list_files()
    except Exception as e:
        return f"❌ Error: {str(e)}", list_files()

def create_examples():
    examples = {
        "hello.py": "print('Hello World!')\nprint('AI Coding Agent is working!')\n",
        "README.md": "# My Project\\nCreated with AI Coding Agent",
        "test.py": "def add(a, b):\\n    return a + b\\n\\nprint(add(5, 3))"
    }
    for name, content in examples.items():
        with open(os.path.join(WORKSPACE, name), 'w') as f:
            f.write(content)
    return "✅ Created example files", list_files()

# Build Gradio interface
with gr.Blocks(
    title="🤖 AI Coding Agent",
    theme=gr.themes.Soft(),
    css="""
    .gradio-container { max-width: 900px; margin: auto; padding: 20px; }
    .chatbot { min-height: 300px; border-radius: 10px; }
    """
) as demo:
    
    gr.Markdown("# 🤖 AI Coding Agent")
    gr.Markdown("*An intelligent assistant for programming help*")
    
    with gr.Tabs():
        # Tab 1: Chat
        with gr.Tab("💬 Chat"):
            chatbot = gr.Chatbot(
                value=[("AI", "👋 Hello! I'm your AI Coding Assistant. How can I help?")],
                height=350
            )
            
            with gr.Row():
                msg_input = gr.Textbox(
                    placeholder="Ask about coding...",
                    scale=4,
                    container=False
                )
                send_btn = gr.Button("Send", variant="primary", scale=1)
            
            clear_btn = gr.Button("Clear Chat", variant="secondary")
            
            # Chat function
            def chat_respond(message, history):
                if not message.strip():
                    return "", history
                response = ai_chat(message, history)
                return "", history + [(message, response)]
            
            msg_input.submit(chat_respond, [msg_input, chatbot], [msg_input, chatbot])
            send_btn.click(chat_respond, [msg_input, chatbot], [msg_input, chatbot])
            clear_btn.click(lambda: [], None, chatbot)
        
        # Tab 2: Files
        with gr.Tab("📁 Files"):
            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("### Workspace")
                    file_display = gr.Textbox(
                        label="Files",
                        value=list_files(),
                        interactive=False,
                        lines=8
                    )
                    refresh_btn = gr.Button("🔄 Refresh")
                    examples_btn = gr.Button("📝 Create Examples")
                
                with gr.Column(scale=2):
                    gr.Markdown("### Edit Files")
                    filename = gr.Textbox(label="Filename", placeholder="example.py")
                    file_content = gr.Textbox(
                        label="Content", 
                        lines=12,
                        placeholder="Write your code here...",
                        value="# Write your code here\\nprint('Hello World')"
                    )
                    save_btn = gr.Button("💾 Save File", variant="primary")
                    status = gr.Textbox(label="Status", interactive=False)
            
            # File operations
            refresh_btn.click(list_files, outputs=file_display)
            examples_btn.click(create_examples, outputs=[status, file_display])
            save_btn.click(save_file, [filename, file_content], outputs=[status, file_display])
        
        # Tab 3: About
        with gr.Tab("ℹ️ About"):
            gr.Markdown("""
            ## About AI Coding Agent
            
            This is an intelligent coding assistant that helps with:
            
            - **Code Writing**: Generate Python/JavaScript code
            - **Code Review**: Get feedback on your code
            - **Debugging**: Help fix errors
            - **Explanations**: Understand programming concepts
            - **File Management**: Edit and organize code files
            
            ### Quick Start
            1. Go to **Chat** tab to ask questions
            2. Go to **Files** tab to create/edit code
            3. Try these examples:
               - "Write a Python function"
               - "Explain recursion"
               - "Create a simple API"
            
            ### Creator
            Made by [ali4566544](https://huggingface.co/ali4566544)
            """)
    
    # Footer
    gr.Markdown("---")
    gr.Markdown("<center>🚀 AI Coding Agent is now working!</center>")

# Launch
if __name__ == "__main__":
    demo.launch(
        server_name="0.0.0.0",
        server_port=7860,
        show_error=True,
        debug=False
    )