Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,15 +2,19 @@ import gradio as gr
|
|
| 2 |
import boto3
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
-
from typing import Optional, Tuple
|
| 6 |
import nbformat
|
| 7 |
from io import StringIO
|
| 8 |
import sys
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
class CodingCopilot:
|
| 11 |
def __init__(self):
|
| 12 |
"""Initialize the coding copilot with AWS Bedrock client."""
|
| 13 |
self.setup_aws_client()
|
|
|
|
| 14 |
|
| 15 |
def setup_aws_client(self):
|
| 16 |
"""Setup AWS Bedrock client with credentials from environment variables."""
|
|
@@ -83,75 +87,109 @@ class CodingCopilot:
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"Error reading notebook: {str(e)}"
|
| 85 |
|
| 86 |
-
def
|
| 87 |
-
"""
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
"review": f"""Please review this code and provide:
|
| 90 |
1. Code quality assessment
|
| 91 |
2. Potential bugs or issues
|
| 92 |
3. Performance improvements
|
| 93 |
4. Best practices suggestions
|
| 94 |
-
5. Security considerations
|
| 95 |
-
|
| 96 |
-
Code:
|
| 97 |
-
```python
|
| 98 |
-
{code_content}
|
| 99 |
-
```""",
|
| 100 |
|
| 101 |
"explain": f"""Please explain this code in detail:
|
| 102 |
1. What does this code do?
|
| 103 |
2. How does it work?
|
| 104 |
3. Key functions and their purposes
|
| 105 |
4. Dependencies and requirements
|
| 106 |
-
5. Usage examples
|
| 107 |
-
|
| 108 |
-
Code:
|
| 109 |
-
```python
|
| 110 |
-
{code_content}
|
| 111 |
-
```""",
|
| 112 |
|
| 113 |
"optimize": f"""Please optimize this code:
|
| 114 |
1. Suggest performance improvements
|
| 115 |
2. Refactor for better readability
|
| 116 |
3. Reduce complexity where possible
|
| 117 |
4. Improve memory usage
|
| 118 |
-
5. Provide the optimized version
|
| 119 |
-
|
| 120 |
-
Code:
|
| 121 |
-
```python
|
| 122 |
-
{code_content}
|
| 123 |
-
```""",
|
| 124 |
|
| 125 |
"debug": f"""Please help debug this code:
|
| 126 |
1. Identify potential bugs
|
| 127 |
2. Suggest fixes
|
| 128 |
3. Add error handling
|
| 129 |
4. Improve robustness
|
| 130 |
-
5. Provide corrected version
|
| 131 |
-
|
| 132 |
-
Code:
|
| 133 |
-
```python
|
| 134 |
-
{code_content}
|
| 135 |
-
```""",
|
| 136 |
|
| 137 |
"document": f"""Please add comprehensive documentation to this code:
|
| 138 |
1. Add docstrings to functions/classes
|
| 139 |
2. Add inline comments
|
| 140 |
3. Create usage examples
|
| 141 |
4. Document parameters and return values
|
| 142 |
-
5. Add type hints where appropriate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 143 |
|
| 144 |
Code:
|
| 145 |
```python
|
| 146 |
{code_content}
|
| 147 |
-
```
|
| 148 |
-
|
|
|
|
| 149 |
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
prompt = f"""Please generate {language} code based on this description:
|
| 156 |
|
| 157 |
{description}
|
|
@@ -162,172 +200,411 @@ Requirements:
|
|
| 162 |
3. Add type hints where appropriate
|
| 163 |
4. Provide usage examples
|
| 164 |
5. Follow best practices
|
|
|
|
| 165 |
|
| 166 |
-
Please provide
|
| 167 |
|
| 168 |
return self.call_claude(prompt)
|
| 169 |
|
| 170 |
-
def chat_with_code(self, code_content: str, question: str) -> str:
|
| 171 |
-
"""Chat about specific code."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
prompt = f"""Given this code:
|
| 173 |
|
| 174 |
```python
|
| 175 |
{code_content}
|
| 176 |
```
|
|
|
|
| 177 |
|
| 178 |
-
|
| 179 |
|
| 180 |
-
Please provide a detailed answer about the code."""
|
| 181 |
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
# Initialize the copilot
|
| 185 |
copilot = CodingCopilot()
|
| 186 |
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 203 |
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
content, status =
|
| 210 |
if not content or content.startswith("Error"):
|
| 211 |
return status or content
|
| 212 |
|
| 213 |
-
|
|
|
|
| 214 |
|
| 215 |
-
def generate_new_code(description, language):
|
| 216 |
-
"""Generate new code based on description."""
|
| 217 |
if not description.strip():
|
| 218 |
-
return "Please provide a description of what you want to generate."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
-
def chat_about_code(
|
| 223 |
-
"""Chat about uploaded code."""
|
| 224 |
-
if
|
| 225 |
-
return "Please upload
|
| 226 |
|
| 227 |
if not question.strip():
|
| 228 |
-
return "Please ask a question about the code."
|
| 229 |
|
| 230 |
-
content, status =
|
| 231 |
if not content or content.startswith("Error"):
|
| 232 |
-
return status or content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
|
| 234 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 235 |
|
| 236 |
-
# Create Gradio interface
|
| 237 |
-
with gr.Blocks(title="Coding Copilot", theme=gr.themes.
|
| 238 |
-
gr.Markdown("
|
| 239 |
-
|
|
|
|
|
|
|
| 240 |
|
| 241 |
with gr.Tabs():
|
| 242 |
-
# File Analysis Tab
|
| 243 |
with gr.TabItem("π Analyze Code"):
|
|
|
|
| 244 |
with gr.Row():
|
| 245 |
-
with gr.Column():
|
| 246 |
file_input = gr.File(
|
| 247 |
-
label="Upload
|
| 248 |
-
|
|
|
|
|
|
|
| 249 |
)
|
| 250 |
analysis_type = gr.Dropdown(
|
| 251 |
-
choices=["review", "explain", "optimize", "debug", "document"],
|
| 252 |
value="review",
|
| 253 |
-
label="Analysis Type"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 254 |
)
|
| 255 |
-
analyze_btn = gr.Button("Analyze Code", variant="primary")
|
| 256 |
|
| 257 |
-
with gr.Column():
|
| 258 |
analysis_output = gr.Textbox(
|
| 259 |
-
label="Analysis Result",
|
| 260 |
-
lines=
|
| 261 |
-
max_lines=
|
|
|
|
| 262 |
)
|
| 263 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
analyze_btn.click(
|
| 265 |
analyze_uploaded_code,
|
| 266 |
-
inputs=[file_input, analysis_type],
|
| 267 |
outputs=analysis_output
|
| 268 |
)
|
| 269 |
|
| 270 |
-
# Code Generation Tab
|
| 271 |
with gr.TabItem("β‘ Generate Code"):
|
|
|
|
| 272 |
with gr.Row():
|
| 273 |
-
with gr.Column():
|
| 274 |
code_description = gr.Textbox(
|
| 275 |
-
label="Describe what you want to code",
|
| 276 |
lines=5,
|
| 277 |
-
placeholder="E.g., Create a
|
| 278 |
)
|
| 279 |
code_language = gr.Dropdown(
|
| 280 |
-
choices=["python", "javascript", "java", "cpp", "c"],
|
| 281 |
value="python",
|
| 282 |
-
label="Programming Language"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
)
|
| 284 |
-
generate_btn = gr.Button("Generate Code", variant="primary")
|
| 285 |
|
| 286 |
-
with gr.Column():
|
| 287 |
generated_code = gr.Code(
|
| 288 |
-
label="Generated Code",
|
| 289 |
language="python",
|
| 290 |
-
lines=
|
|
|
|
| 291 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 292 |
|
| 293 |
generate_btn.click(
|
| 294 |
-
|
| 295 |
-
inputs=[code_description, code_language],
|
| 296 |
-
outputs=generated_code
|
| 297 |
)
|
| 298 |
|
| 299 |
-
# Code Chat Tab
|
| 300 |
with gr.TabItem("π¬ Chat About Code"):
|
|
|
|
| 301 |
with gr.Row():
|
| 302 |
-
with gr.Column():
|
| 303 |
chat_file_input = gr.File(
|
| 304 |
-
label="Upload Code
|
| 305 |
-
|
|
|
|
|
|
|
| 306 |
)
|
| 307 |
code_question = gr.Textbox(
|
| 308 |
-
label="Ask a question about your code",
|
| 309 |
lines=3,
|
| 310 |
-
placeholder="E.g., How can I
|
| 311 |
)
|
| 312 |
-
chat_btn = gr.Button("Ask Question", variant="primary")
|
|
|
|
| 313 |
|
| 314 |
-
with gr.Column():
|
| 315 |
chat_output = gr.Textbox(
|
| 316 |
-
label="
|
| 317 |
lines=15,
|
| 318 |
-
max_lines=25
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 319 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
|
| 321 |
chat_btn.click(
|
| 322 |
-
|
| 323 |
-
inputs=[chat_file_input, code_question],
|
| 324 |
-
outputs=chat_output
|
| 325 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 326 |
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
gr.Markdown("**Note:** This copilot uses Claude 3 Haiku for intelligent code analysis and generation.")
|
| 330 |
|
| 331 |
# Launch the app
|
| 332 |
if __name__ == "__main__":
|
| 333 |
-
app.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import boto3
|
| 3 |
import json
|
| 4 |
import os
|
| 5 |
+
from typing import Optional, Tuple, List
|
| 6 |
import nbformat
|
| 7 |
from io import StringIO
|
| 8 |
import sys
|
| 9 |
+
import tempfile
|
| 10 |
+
import zipfile
|
| 11 |
+
from datetime import datetime
|
| 12 |
|
| 13 |
class CodingCopilot:
|
| 14 |
def __init__(self):
|
| 15 |
"""Initialize the coding copilot with AWS Bedrock client."""
|
| 16 |
self.setup_aws_client()
|
| 17 |
+
self.chat_history = []
|
| 18 |
|
| 19 |
def setup_aws_client(self):
|
| 20 |
"""Setup AWS Bedrock client with credentials from environment variables."""
|
|
|
|
| 87 |
except Exception as e:
|
| 88 |
return f"Error reading notebook: {str(e)}"
|
| 89 |
|
| 90 |
+
def process_multiple_files(self, files: List) -> Tuple[str, str]:
|
| 91 |
+
"""Process multiple uploaded files and return combined content."""
|
| 92 |
+
if not files:
|
| 93 |
+
return "", "No files uploaded"
|
| 94 |
+
|
| 95 |
+
all_content = []
|
| 96 |
+
file_info = []
|
| 97 |
+
|
| 98 |
+
for file in files:
|
| 99 |
+
file_path = file.name
|
| 100 |
+
file_name = os.path.basename(file_path)
|
| 101 |
+
file_extension = os.path.splitext(file_path)[1].lower()
|
| 102 |
+
|
| 103 |
+
if file_extension == '.py':
|
| 104 |
+
content = self.read_python_file(file_path)
|
| 105 |
+
elif file_extension == '.ipynb':
|
| 106 |
+
content = self.read_notebook_file(file_path)
|
| 107 |
+
else:
|
| 108 |
+
continue
|
| 109 |
+
|
| 110 |
+
if content and not content.startswith("Error"):
|
| 111 |
+
all_content.append(f"# File: {file_name}")
|
| 112 |
+
all_content.append(f"# Type: {file_extension}")
|
| 113 |
+
all_content.append("# " + "="*50)
|
| 114 |
+
all_content.append(content)
|
| 115 |
+
all_content.append("\n" + "="*60 + "\n")
|
| 116 |
+
file_info.append(f"{file_name} ({file_extension})")
|
| 117 |
+
|
| 118 |
+
combined_content = '\n'.join(all_content)
|
| 119 |
+
status = f"Successfully loaded {len(file_info)} files: {', '.join(file_info)}"
|
| 120 |
+
|
| 121 |
+
return combined_content, status
|
| 122 |
+
|
| 123 |
+
def analyze_code(self, code_content: str, analysis_type: str, custom_prompt: str = "") -> str:
|
| 124 |
+
"""Analyze code using Claude with optional custom prompt."""
|
| 125 |
+
base_prompts = {
|
| 126 |
"review": f"""Please review this code and provide:
|
| 127 |
1. Code quality assessment
|
| 128 |
2. Potential bugs or issues
|
| 129 |
3. Performance improvements
|
| 130 |
4. Best practices suggestions
|
| 131 |
+
5. Security considerations""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
"explain": f"""Please explain this code in detail:
|
| 134 |
1. What does this code do?
|
| 135 |
2. How does it work?
|
| 136 |
3. Key functions and their purposes
|
| 137 |
4. Dependencies and requirements
|
| 138 |
+
5. Usage examples""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 139 |
|
| 140 |
"optimize": f"""Please optimize this code:
|
| 141 |
1. Suggest performance improvements
|
| 142 |
2. Refactor for better readability
|
| 143 |
3. Reduce complexity where possible
|
| 144 |
4. Improve memory usage
|
| 145 |
+
5. Provide the optimized version""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
"debug": f"""Please help debug this code:
|
| 148 |
1. Identify potential bugs
|
| 149 |
2. Suggest fixes
|
| 150 |
3. Add error handling
|
| 151 |
4. Improve robustness
|
| 152 |
+
5. Provide corrected version""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
"document": f"""Please add comprehensive documentation to this code:
|
| 155 |
1. Add docstrings to functions/classes
|
| 156 |
2. Add inline comments
|
| 157 |
3. Create usage examples
|
| 158 |
4. Document parameters and return values
|
| 159 |
+
5. Add type hints where appropriate""",
|
| 160 |
+
|
| 161 |
+
"custom": custom_prompt
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
if analysis_type == "custom" and not custom_prompt.strip():
|
| 165 |
+
return "Please provide a custom prompt for analysis."
|
| 166 |
+
|
| 167 |
+
prompt_text = base_prompts.get(analysis_type, base_prompts["review"])
|
| 168 |
+
|
| 169 |
+
full_prompt = f"""{prompt_text}
|
| 170 |
|
| 171 |
Code:
|
| 172 |
```python
|
| 173 |
{code_content}
|
| 174 |
+
```
|
| 175 |
+
|
| 176 |
+
Please provide a detailed response and if fixes are needed, provide the corrected code."""
|
| 177 |
|
| 178 |
+
return self.call_claude(full_prompt)
|
| 179 |
+
|
| 180 |
+
def generate_code(self, description: str, language: str = "python", reference_files: str = "") -> str:
|
| 181 |
+
"""Generate code based on description with optional reference files."""
|
| 182 |
+
reference_context = ""
|
| 183 |
+
if reference_files.strip():
|
| 184 |
+
reference_context = f"""
|
| 185 |
|
| 186 |
+
Reference files for context:
|
| 187 |
+
```
|
| 188 |
+
{reference_files}
|
| 189 |
+
```
|
| 190 |
+
|
| 191 |
+
Please use these files as reference when generating the new code."""
|
| 192 |
+
|
| 193 |
prompt = f"""Please generate {language} code based on this description:
|
| 194 |
|
| 195 |
{description}
|
|
|
|
| 200 |
3. Add type hints where appropriate
|
| 201 |
4. Provide usage examples
|
| 202 |
5. Follow best practices
|
| 203 |
+
{reference_context}
|
| 204 |
|
| 205 |
+
Please provide the complete, ready-to-use code with detailed comments."""
|
| 206 |
|
| 207 |
return self.call_claude(prompt)
|
| 208 |
|
| 209 |
+
def chat_with_code(self, code_content: str, question: str, chat_history: List = None) -> Tuple[str, List]:
|
| 210 |
+
"""Chat about specific code with history."""
|
| 211 |
+
if chat_history is None:
|
| 212 |
+
chat_history = []
|
| 213 |
+
|
| 214 |
+
# Add context from previous conversation
|
| 215 |
+
context = ""
|
| 216 |
+
if chat_history:
|
| 217 |
+
context = "\n\nPrevious conversation:\n"
|
| 218 |
+
for i, (q, a) in enumerate(chat_history[-3:]): # Last 3 exchanges for context
|
| 219 |
+
context += f"Q{i+1}: {q}\nA{i+1}: {a}\n"
|
| 220 |
+
|
| 221 |
prompt = f"""Given this code:
|
| 222 |
|
| 223 |
```python
|
| 224 |
{code_content}
|
| 225 |
```
|
| 226 |
+
{context}
|
| 227 |
|
| 228 |
+
Current question: {question}
|
| 229 |
|
| 230 |
+
Please provide a detailed answer about the code. If you're suggesting changes, provide the complete corrected code."""
|
| 231 |
|
| 232 |
+
response = self.call_claude(prompt)
|
| 233 |
+
|
| 234 |
+
# Update chat history
|
| 235 |
+
chat_history.append((question, response))
|
| 236 |
+
|
| 237 |
+
return response, chat_history
|
| 238 |
+
|
| 239 |
+
def create_downloadable_file(self, content: str, filename: str = "generated_code.py") -> str:
|
| 240 |
+
"""Create a downloadable file with the given content."""
|
| 241 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 242 |
+
filename = f"{timestamp}_{filename}"
|
| 243 |
+
|
| 244 |
+
# Create temporary file
|
| 245 |
+
temp_file = tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False)
|
| 246 |
+
temp_file.write(content)
|
| 247 |
+
temp_file.close()
|
| 248 |
+
|
| 249 |
+
return temp_file.name
|
| 250 |
|
| 251 |
# Initialize the copilot
|
| 252 |
copilot = CodingCopilot()
|
| 253 |
|
| 254 |
+
# Custom CSS for black and red theme
|
| 255 |
+
custom_css = """
|
| 256 |
+
.gradio-container {
|
| 257 |
+
background: linear-gradient(135deg, #0a0a0a 0%, #1a0a0a 100%) !important;
|
| 258 |
+
color: #ffffff !important;
|
| 259 |
+
}
|
| 260 |
+
|
| 261 |
+
.dark {
|
| 262 |
+
background: #0a0a0a !important;
|
| 263 |
+
color: #ffffff !important;
|
| 264 |
+
}
|
| 265 |
+
|
| 266 |
+
/* Tab styling */
|
| 267 |
+
.tab-nav {
|
| 268 |
+
background: #1a1a1a !important;
|
| 269 |
+
border-bottom: 2px solid #dc2626 !important;
|
| 270 |
+
}
|
| 271 |
+
|
| 272 |
+
.tab-nav button {
|
| 273 |
+
background: #2a2a2a !important;
|
| 274 |
+
color: #ffffff !important;
|
| 275 |
+
border: 1px solid #dc2626 !important;
|
| 276 |
+
margin-right: 5px !important;
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
.tab-nav button[aria-selected="true"] {
|
| 280 |
+
background: #dc2626 !important;
|
| 281 |
+
color: #ffffff !important;
|
| 282 |
+
}
|
| 283 |
+
|
| 284 |
+
/* Input styling */
|
| 285 |
+
.gr-textbox, .gr-dropdown, .gr-file {
|
| 286 |
+
background: #2a2a2a !important;
|
| 287 |
+
border: 1px solid #dc2626 !important;
|
| 288 |
+
color: #ffffff !important;
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
.gr-textbox:focus, .gr-dropdown:focus {
|
| 292 |
+
border-color: #ef4444 !important;
|
| 293 |
+
box-shadow: 0 0 0 2px rgba(220, 38, 38, 0.2) !important;
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
/* Button styling */
|
| 297 |
+
.gr-button {
|
| 298 |
+
background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%) !important;
|
| 299 |
+
border: none !important;
|
| 300 |
+
color: #ffffff !important;
|
| 301 |
+
font-weight: bold !important;
|
| 302 |
+
transition: all 0.3s ease !important;
|
| 303 |
+
}
|
| 304 |
|
| 305 |
+
.gr-button:hover {
|
| 306 |
+
background: linear-gradient(135deg, #ef4444 0%, #dc2626 100%) !important;
|
| 307 |
+
transform: translateY(-2px) !important;
|
| 308 |
+
box-shadow: 0 5px 15px rgba(220, 38, 38, 0.4) !important;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
/* Code block styling */
|
| 312 |
+
.gr-code {
|
| 313 |
+
background: #1a1a1a !important;
|
| 314 |
+
border: 1px solid #dc2626 !important;
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
/* Panel styling */
|
| 318 |
+
.gr-panel {
|
| 319 |
+
background: #1a1a1a !important;
|
| 320 |
+
border: 1px solid #333333 !important;
|
| 321 |
+
}
|
| 322 |
+
|
| 323 |
+
/* Markdown styling */
|
| 324 |
+
.gr-markdown {
|
| 325 |
+
color: #ffffff !important;
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
.gr-markdown h1 {
|
| 329 |
+
color: #dc2626 !important;
|
| 330 |
+
text-shadow: 0 0 10px rgba(220, 38, 38, 0.5) !important;
|
| 331 |
+
}
|
| 332 |
+
|
| 333 |
+
.gr-markdown h2, .gr-markdown h3 {
|
| 334 |
+
color: #ef4444 !important;
|
| 335 |
+
}
|
| 336 |
+
|
| 337 |
+
/* File upload styling */
|
| 338 |
+
.gr-file-upload {
|
| 339 |
+
background: #2a2a2a !important;
|
| 340 |
+
border: 2px dashed #dc2626 !important;
|
| 341 |
+
color: #ffffff !important;
|
| 342 |
+
}
|
| 343 |
+
|
| 344 |
+
.gr-file-upload:hover {
|
| 345 |
+
border-color: #ef4444 !important;
|
| 346 |
+
background: #3a2a2a !important;
|
| 347 |
+
}
|
| 348 |
+
|
| 349 |
+
/* Chat history styling */
|
| 350 |
+
.chat-history {
|
| 351 |
+
background: #1a1a1a !important;
|
| 352 |
+
border: 1px solid #dc2626 !important;
|
| 353 |
+
border-radius: 8px !important;
|
| 354 |
+
padding: 10px !important;
|
| 355 |
+
max-height: 400px !important;
|
| 356 |
+
overflow-y: auto !important;
|
| 357 |
+
}
|
| 358 |
+
|
| 359 |
+
.chat-message {
|
| 360 |
+
margin: 10px 0 !important;
|
| 361 |
+
padding: 8px !important;
|
| 362 |
+
border-left: 3px solid #dc2626 !important;
|
| 363 |
+
background: #2a2a2a !important;
|
| 364 |
+
}
|
| 365 |
+
"""
|
| 366 |
+
|
| 367 |
+
def analyze_uploaded_code(files, analysis_type, custom_prompt):
|
| 368 |
+
"""Analyze uploaded code files."""
|
| 369 |
+
if not files:
|
| 370 |
+
return "Please upload at least one file."
|
| 371 |
|
| 372 |
+
content, status = copilot.process_multiple_files(files)
|
| 373 |
if not content or content.startswith("Error"):
|
| 374 |
return status or content
|
| 375 |
|
| 376 |
+
result = copilot.analyze_code(content, analysis_type, custom_prompt)
|
| 377 |
+
return f"**Status:** {status}\n\n**Analysis Result:**\n{result}"
|
| 378 |
|
| 379 |
+
def generate_new_code(description, language, reference_files):
|
| 380 |
+
"""Generate new code based on description with reference files."""
|
| 381 |
if not description.strip():
|
| 382 |
+
return "Please provide a description of what you want to generate.", None
|
| 383 |
+
|
| 384 |
+
reference_content = ""
|
| 385 |
+
if reference_files:
|
| 386 |
+
reference_content, _ = copilot.process_multiple_files(reference_files)
|
| 387 |
|
| 388 |
+
result = copilot.generate_code(description, language, reference_content)
|
| 389 |
+
|
| 390 |
+
# Create downloadable file
|
| 391 |
+
filename = f"generated_{language}_code.py"
|
| 392 |
+
temp_file = copilot.create_downloadable_file(result, filename)
|
| 393 |
+
|
| 394 |
+
return result, temp_file
|
| 395 |
|
| 396 |
+
def chat_about_code(files, question, chat_history):
|
| 397 |
+
"""Chat about uploaded code with history."""
|
| 398 |
+
if not files:
|
| 399 |
+
return "Please upload at least one file.", chat_history, ""
|
| 400 |
|
| 401 |
if not question.strip():
|
| 402 |
+
return "Please ask a question about the code.", chat_history, ""
|
| 403 |
|
| 404 |
+
content, status = copilot.process_multiple_files(files)
|
| 405 |
if not content or content.startswith("Error"):
|
| 406 |
+
return status or content, chat_history, ""
|
| 407 |
+
|
| 408 |
+
if chat_history is None:
|
| 409 |
+
chat_history = []
|
| 410 |
+
|
| 411 |
+
response, updated_history = copilot.chat_with_code(content, question, chat_history)
|
| 412 |
+
|
| 413 |
+
# Format chat history for display
|
| 414 |
+
chat_display = ""
|
| 415 |
+
for i, (q, a) in enumerate(updated_history):
|
| 416 |
+
chat_display += f"**Q{i+1}:** {q}\n\n**A{i+1}:** {a}\n\n---\n\n"
|
| 417 |
|
| 418 |
+
# Create downloadable response file if it contains code
|
| 419 |
+
download_file = None
|
| 420 |
+
if "```" in response:
|
| 421 |
+
download_file = copilot.create_downloadable_file(response, "chat_response.md")
|
| 422 |
+
|
| 423 |
+
return response, updated_history, chat_display, download_file
|
| 424 |
|
| 425 |
+
# Create Gradio interface with custom theme
|
| 426 |
+
with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.themes.Base()) as app:
|
| 427 |
+
gr.Markdown("""
|
| 428 |
+
# π₯ Advanced Coding Copilot
|
| 429 |
+
### Upload multiple Python files (.py) or Jupyter notebooks (.ipynb) for intelligent analysis, or generate new code with AI assistance!
|
| 430 |
+
""")
|
| 431 |
|
| 432 |
with gr.Tabs():
|
| 433 |
+
# Enhanced File Analysis Tab
|
| 434 |
with gr.TabItem("π Analyze Code"):
|
| 435 |
+
gr.Markdown("### Upload multiple files and get comprehensive analysis")
|
| 436 |
with gr.Row():
|
| 437 |
+
with gr.Column(scale=1):
|
| 438 |
file_input = gr.File(
|
| 439 |
+
label="π Upload Multiple Files (.py/.ipynb)",
|
| 440 |
+
file_count="multiple",
|
| 441 |
+
file_types=[".py", ".ipynb"],
|
| 442 |
+
height=120
|
| 443 |
)
|
| 444 |
analysis_type = gr.Dropdown(
|
| 445 |
+
choices=["review", "explain", "optimize", "debug", "document", "custom"],
|
| 446 |
value="review",
|
| 447 |
+
label="π Analysis Type",
|
| 448 |
+
info="Choose analysis type or select 'custom' to write your own prompt"
|
| 449 |
+
)
|
| 450 |
+
custom_prompt = gr.Textbox(
|
| 451 |
+
label="βοΈ Custom Prompt (when Analysis Type = 'custom')",
|
| 452 |
+
lines=3,
|
| 453 |
+
placeholder="Write your custom analysis prompt here...",
|
| 454 |
+
visible=False
|
| 455 |
)
|
| 456 |
+
analyze_btn = gr.Button("π Analyze Code", variant="primary", size="lg")
|
| 457 |
|
| 458 |
+
with gr.Column(scale=2):
|
| 459 |
analysis_output = gr.Textbox(
|
| 460 |
+
label="π Analysis Result",
|
| 461 |
+
lines=25,
|
| 462 |
+
max_lines=40,
|
| 463 |
+
show_copy_button=True
|
| 464 |
)
|
| 465 |
|
| 466 |
+
# Show/hide custom prompt based on analysis type
|
| 467 |
+
def toggle_custom_prompt(analysis_type):
|
| 468 |
+
return gr.update(visible=(analysis_type == "custom"))
|
| 469 |
+
|
| 470 |
+
analysis_type.change(toggle_custom_prompt, inputs=analysis_type, outputs=custom_prompt)
|
| 471 |
analyze_btn.click(
|
| 472 |
analyze_uploaded_code,
|
| 473 |
+
inputs=[file_input, analysis_type, custom_prompt],
|
| 474 |
outputs=analysis_output
|
| 475 |
)
|
| 476 |
|
| 477 |
+
# Enhanced Code Generation Tab
|
| 478 |
with gr.TabItem("β‘ Generate Code"):
|
| 479 |
+
gr.Markdown("### Generate new code with optional reference files")
|
| 480 |
with gr.Row():
|
| 481 |
+
with gr.Column(scale=1):
|
| 482 |
code_description = gr.Textbox(
|
| 483 |
+
label="π Describe what you want to code",
|
| 484 |
lines=5,
|
| 485 |
+
placeholder="E.g., Create a REST API using FastAPI with authentication, database models, and CRUD operations..."
|
| 486 |
)
|
| 487 |
code_language = gr.Dropdown(
|
| 488 |
+
choices=["python", "javascript", "java", "cpp", "c", "go", "rust"],
|
| 489 |
value="python",
|
| 490 |
+
label="π» Programming Language"
|
| 491 |
+
)
|
| 492 |
+
reference_files = gr.File(
|
| 493 |
+
label="π Reference Files (optional)",
|
| 494 |
+
file_count="multiple",
|
| 495 |
+
file_types=[".py", ".ipynb"],
|
| 496 |
+
info="Upload files to use as reference/context"
|
| 497 |
)
|
| 498 |
+
generate_btn = gr.Button("π― Generate Code", variant="primary", size="lg")
|
| 499 |
|
| 500 |
+
with gr.Column(scale=2):
|
| 501 |
generated_code = gr.Code(
|
| 502 |
+
label="π§ Generated Code",
|
| 503 |
language="python",
|
| 504 |
+
lines=25,
|
| 505 |
+
show_copy_button=True
|
| 506 |
)
|
| 507 |
+
download_generated = gr.File(
|
| 508 |
+
label="πΎ Download Generated Code",
|
| 509 |
+
visible=False,
|
| 510 |
+
interactive=False
|
| 511 |
+
)
|
| 512 |
+
|
| 513 |
+
def handle_generation(desc, lang, ref_files):
|
| 514 |
+
code, file_path = generate_new_code(desc, lang, ref_files)
|
| 515 |
+
return code, gr.update(value=file_path, visible=True)
|
| 516 |
|
| 517 |
generate_btn.click(
|
| 518 |
+
handle_generation,
|
| 519 |
+
inputs=[code_description, code_language, reference_files],
|
| 520 |
+
outputs=[generated_code, download_generated]
|
| 521 |
)
|
| 522 |
|
| 523 |
+
# Enhanced Code Chat Tab
|
| 524 |
with gr.TabItem("π¬ Chat About Code"):
|
| 525 |
+
gr.Markdown("### Interactive chat about your code with conversation history")
|
| 526 |
with gr.Row():
|
| 527 |
+
with gr.Column(scale=1):
|
| 528 |
chat_file_input = gr.File(
|
| 529 |
+
label="π Upload Code Files",
|
| 530 |
+
file_count="multiple",
|
| 531 |
+
file_types=[".py", ".ipynb"],
|
| 532 |
+
height=120
|
| 533 |
)
|
| 534 |
code_question = gr.Textbox(
|
| 535 |
+
label="β Ask a question about your code",
|
| 536 |
lines=3,
|
| 537 |
+
placeholder="E.g., How can I optimize this algorithm? What design patterns are used here?"
|
| 538 |
)
|
| 539 |
+
chat_btn = gr.Button("π Ask Question", variant="primary", size="lg")
|
| 540 |
+
clear_chat_btn = gr.Button("ποΈ Clear Chat History", variant="secondary")
|
| 541 |
|
| 542 |
+
with gr.Column(scale=2):
|
| 543 |
chat_output = gr.Textbox(
|
| 544 |
+
label="π€ AI Response",
|
| 545 |
lines=15,
|
| 546 |
+
max_lines=25,
|
| 547 |
+
show_copy_button=True
|
| 548 |
+
)
|
| 549 |
+
chat_history_display = gr.Textbox(
|
| 550 |
+
label="π Chat History",
|
| 551 |
+
lines=10,
|
| 552 |
+
max_lines=20,
|
| 553 |
+
show_copy_button=True,
|
| 554 |
+
info="Complete conversation history"
|
| 555 |
)
|
| 556 |
+
download_chat = gr.File(
|
| 557 |
+
label="πΎ Download Chat Response",
|
| 558 |
+
visible=False,
|
| 559 |
+
interactive=False
|
| 560 |
+
)
|
| 561 |
+
|
| 562 |
+
# Hidden state for chat history
|
| 563 |
+
chat_history_state = gr.State([])
|
| 564 |
+
|
| 565 |
+
def handle_chat(files, question, history):
|
| 566 |
+
response, updated_history, history_display, download_file = chat_about_code(files, question, history)
|
| 567 |
+
download_visible = download_file is not None
|
| 568 |
+
return (
|
| 569 |
+
response,
|
| 570 |
+
updated_history,
|
| 571 |
+
history_display,
|
| 572 |
+
gr.update(value=download_file, visible=download_visible),
|
| 573 |
+
"" # Clear the question input
|
| 574 |
+
)
|
| 575 |
+
|
| 576 |
+
def clear_chat():
|
| 577 |
+
return [], "", ""
|
| 578 |
|
| 579 |
chat_btn.click(
|
| 580 |
+
handle_chat,
|
| 581 |
+
inputs=[chat_file_input, code_question, chat_history_state],
|
| 582 |
+
outputs=[chat_output, chat_history_state, chat_history_display, download_chat, code_question]
|
| 583 |
)
|
| 584 |
+
|
| 585 |
+
clear_chat_btn.click(
|
| 586 |
+
clear_chat,
|
| 587 |
+
outputs=[chat_history_state, chat_history_display, chat_output]
|
| 588 |
+
)
|
| 589 |
+
|
| 590 |
+
# Enhanced Footer
|
| 591 |
+
gr.Markdown("""
|
| 592 |
+
---
|
| 593 |
+
### π₯ Features:
|
| 594 |
+
- **Multi-file Support**: Upload and analyze multiple files simultaneously
|
| 595 |
+
- **Custom Prompts**: Write your own analysis prompts for specific needs
|
| 596 |
+
- **Interactive Chat**: Have conversations about your code with full history
|
| 597 |
+
- **File Downloads**: Get generated code and chat responses as downloadable files
|
| 598 |
+
- **Reference Context**: Use existing files as reference when generating new code
|
| 599 |
|
| 600 |
+
**Powered by:** Claude 3 Haiku via AWS Bedrock | **Theme:** Cyberpunk Black & Red
|
| 601 |
+
""")
|
|
|
|
| 602 |
|
| 603 |
# Launch the app
|
| 604 |
if __name__ == "__main__":
|
| 605 |
+
app.launch(
|
| 606 |
+
server_name="0.0.0.0",
|
| 607 |
+
server_port=7860,
|
| 608 |
+
share=True,
|
| 609 |
+
show_error=True
|
| 610 |
+
)
|