Spaces:
Runtime error
Runtime error
File size: 7,872 Bytes
c0fd849 | 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 | # app.py - For Hugging Face Spaces
import gradio as gr
import os
import json
from pathlib import Path
# Mock your existing agent for now (we'll adapt it)
class MockAgent:
def chat(self, message, history):
responses = {
"hello": "Hello! I'm your AI coding assistant. How can I help with coding today?",
"help": "I can help with:\n• Writing code\n• Code reviews\n• Debugging\n• Explaining concepts\n• File operations",
"write python function": "def example():\n return 'Hello from AI assistant'",
}
# Simple response matching
msg_lower = message.lower()
for key in responses:
if key in msg_lower:
return responses[key]
return f"I received: {message}. This is a mock response. In production, this would call Cohere API."
# Initialize agent
agent = MockAgent()
# File system for workspace
WORKSPACE_DIR = "workspace"
os.makedirs(WORKSPACE_DIR, exist_ok=True)
def chat_with_agent(message, history):
"""Chat interface for the agent"""
response = agent.chat(message, history)
history.append((message, response))
return history, history, "" # Return updated history and clear input
def list_files():
"""List files in workspace"""
files = []
for root, dirs, filenames in os.walk(WORKSPACE_DIR):
for filename in filenames:
if not filename.startswith('.'):
rel_path = os.path.relpath(os.path.join(root, filename), WORKSPACE_DIR)
files.append(rel_path)
return "\n".join(files[:20]) or "No files in workspace"
def read_file(filepath):
"""Read a file"""
full_path = os.path.join(WORKSPACE_DIR, filepath)
if os.path.exists(full_path) and os.path.isfile(full_path):
try:
with open(full_path, 'r') as f:
return f.read()
except:
return "Error reading file"
return "File not found"
def write_file(filepath, content):
"""Write to a file"""
try:
full_path = os.path.join(WORKSPACE_DIR, filepath)
os.makedirs(os.path.dirname(full_path), exist_ok=True)
with open(full_path, 'w') as f:
f.write(content)
return f"✅ File saved: {filepath}"
except Exception as e:
return f"❌ Error: {str(e)}"
def create_example_files():
"""Create example files"""
examples = {
"example.py": """def hello_world():
print("Hello, World!")
def add_numbers(a, b):
return a + b
if __name__ == "__main__":
hello_world()
print(f"2 + 3 = {add_numbers(2, 3)}")""",
"README.md": """# AI Coding Agent Workspace
Welcome to the AI Coding Agent!
This is a demo workspace for the Hugging Face Space."""
}
results = []
for filename, content in examples.items():
result = write_file(filename, content)
results.append(f"{filename}: {result}")
return "\n".join(results)
# Gradio interface
with gr.Blocks(title="AI Coding Agent", theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🤖 AI Coding Agent")
gr.Markdown("An intelligent coding assistant that can help write, review, and explain code.")
with gr.Tabs():
with gr.TabItem("💬 Chat"):
chatbot = gr.Chatbot(label="Conversation", height=400)
msg = gr.Textbox(label="Your message", placeholder="Ask me about coding...")
clear = gr.Button("Clear")
def respond(message, chat_history):
bot_message = agent.chat(message, chat_history)
chat_history.append((message, bot_message))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)
with gr.TabItem("📁 Files"):
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### File Operations")
file_list = gr.Textbox(label="Workspace Files", interactive=False, lines=10)
refresh_btn = gr.Button("🔄 Refresh Files")
file_name = gr.Textbox(label="Filename", placeholder="example.py")
file_content = gr.Textbox(label="Content", lines=10, placeholder="Your code here...")
save_btn = gr.Button("💾 Save File")
create_examples_btn = gr.Button("📝 Create Example Files")
def update_file_list():
return list_files()
refresh_btn.click(update_file_list, outputs=file_list)
create_examples_btn.click(create_example_files, outputs=file_list)
def save_file(fname, content):
result = write_file(fname, content)
return result, update_file_list()
save_btn.click(save_file, [file_name, file_content],
[gr.Textbox(label="Result"), file_list])
with gr.Column(scale=1):
gr.Markdown("### Read File")
read_filename = gr.Textbox(label="Filename to read", placeholder="example.py")
read_btn = gr.Button("📖 Read File")
file_display = gr.Textbox(label="File Content", interactive=False, lines=20)
def display_file(filename):
return read_file(filename)
read_btn.click(display_file, inputs=read_filename, outputs=file_display)
with gr.TabItem("⚙️ Settings"):
gr.Markdown("### Agent Settings")
persona = gr.Radio(
choices=["Coder", "Reviewer", "Architect"],
value="Coder",
label="Agent Persona"
)
temperature = gr.Slider(
minimum=0.0,
maximum=1.0,
value=0.2,
step=0.1,
label="Creativity (Temperature)"
)
api_key = gr.Textbox(
label="Cohere API Key (optional)",
type="password",
placeholder="Enter your API key for full functionality"
)
save_settings = gr.Button("Save Settings")
settings_status = gr.Textbox(label="Status", interactive=False)
def save_settings_func(persona_val, temp_val, api_val):
# In production, save to config
return f"Settings saved: {persona_val} mode, Temperature: {temp_val}"
save_settings.click(
save_settings_func,
[persona, temperature, api_key],
outputs=settings_status
)
gr.Markdown("---")
gr.Markdown("### Quick Examples")
with gr.Row():
examples = [
("Write a Python function to check prime numbers", "💬 Chat"),
("Explain recursion with an example", "💬 Chat"),
("Review this code: def add(a,b): return a+b", "💬 Chat"),
("Create a REST API structure", "💬 Chat"),
]
for example_text, tab in examples:
btn = gr.Button(example_text)
# This would need JavaScript to switch tabs - simplified for demo
if __name__ == "__main__":
demo.launch() |