ai-coding-agent / app.py
ali4566544's picture
Update app.py
26122e6 verified
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
)