Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -29,8 +29,8 @@ class CodingCopilot:
|
|
| 29 |
print(f"Error setting up AWS client: {e}")
|
| 30 |
self.bedrock_client = None
|
| 31 |
|
| 32 |
-
def call_claude(self, prompt: str, max_tokens: int =
|
| 33 |
-
"""Call Claude 3 Haiku via AWS Bedrock."""
|
| 34 |
if not self.bedrock_client:
|
| 35 |
return "Error: AWS Bedrock client not initialized. Please check your credentials."
|
| 36 |
|
|
@@ -121,7 +121,7 @@ class CodingCopilot:
|
|
| 121 |
return combined_content, status
|
| 122 |
|
| 123 |
def analyze_code_with_chat(self, code_content: str, analysis_type: str, custom_prompt: str = "", chat_history: List = None) -> Tuple[str, List]:
|
| 124 |
-
"""Analyze code with chat functionality."""
|
| 125 |
if chat_history is None:
|
| 126 |
chat_history = []
|
| 127 |
|
|
@@ -135,7 +135,7 @@ class CodingCopilot:
|
|
| 135 |
context = ""
|
| 136 |
if chat_history:
|
| 137 |
context = "\n\nPrevious analysis conversation:\n"
|
| 138 |
-
for i, (q, a) in enumerate(chat_history[-
|
| 139 |
context += f"Q{i+1}: {q}\nA{i+1}: {a}\n"
|
| 140 |
|
| 141 |
prompt = f"""Given this code:
|
|
@@ -147,11 +147,13 @@ class CodingCopilot:
|
|
| 147 |
|
| 148 |
Follow-up question about the analysis: {custom_prompt}
|
| 149 |
|
| 150 |
-
Please provide a detailed response based on the code and previous analysis."""
|
| 151 |
|
| 152 |
-
response = self.call_claude(prompt)
|
| 153 |
chat_history.append((custom_prompt, response))
|
| 154 |
return response, chat_history
|
|
|
|
|
|
|
| 155 |
"""Analyze code using Claude with optional custom prompt."""
|
| 156 |
base_prompts = {
|
| 157 |
"review": f"""Please review this code and provide:
|
|
@@ -159,35 +161,45 @@ Please provide a detailed response based on the code and previous analysis."""
|
|
| 159 |
2. Potential bugs or issues
|
| 160 |
3. Performance improvements
|
| 161 |
4. Best practices suggestions
|
| 162 |
-
5. Security considerations
|
|
|
|
|
|
|
| 163 |
|
| 164 |
"explain": f"""Please explain this code in detail:
|
| 165 |
1. What does this code do?
|
| 166 |
2. How does it work?
|
| 167 |
3. Key functions and their purposes
|
| 168 |
4. Dependencies and requirements
|
| 169 |
-
5. Usage examples
|
|
|
|
|
|
|
| 170 |
|
| 171 |
"optimize": f"""Please optimize this code:
|
| 172 |
1. Suggest performance improvements
|
| 173 |
2. Refactor for better readability
|
| 174 |
3. Reduce complexity where possible
|
| 175 |
4. Improve memory usage
|
| 176 |
-
5. Provide the optimized version
|
|
|
|
|
|
|
| 177 |
|
| 178 |
"debug": f"""Please help debug this code:
|
| 179 |
1. Identify potential bugs
|
| 180 |
2. Suggest fixes
|
| 181 |
3. Add error handling
|
| 182 |
4. Improve robustness
|
| 183 |
-
5. Provide corrected version
|
|
|
|
|
|
|
| 184 |
|
| 185 |
"document": f"""Please add comprehensive documentation to this code:
|
| 186 |
1. Add docstrings to functions/classes
|
| 187 |
2. Add inline comments
|
| 188 |
3. Create usage examples
|
| 189 |
4. Document parameters and return values
|
| 190 |
-
5. Add type hints where appropriate
|
|
|
|
|
|
|
| 191 |
|
| 192 |
"custom": custom_prompt
|
| 193 |
}
|
|
@@ -204,9 +216,39 @@ Code:
|
|
| 204 |
{code_content}
|
| 205 |
```
|
| 206 |
|
| 207 |
-
Please provide a detailed response and if fixes are needed, provide the corrected code."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 208 |
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
|
| 211 |
def generate_code(self, description: str, language: str = "python", reference_files: str = "") -> str:
|
| 212 |
"""Generate code based on description with optional reference files."""
|
|
@@ -231,14 +273,16 @@ Requirements:
|
|
| 231 |
3. Add type hints where appropriate
|
| 232 |
4. Provide usage examples
|
| 233 |
5. Follow best practices
|
|
|
|
|
|
|
| 234 |
{reference_context}
|
| 235 |
|
| 236 |
-
Please provide the complete, ready-to-use code with detailed comments."""
|
| 237 |
|
| 238 |
-
return self.call_claude(prompt)
|
| 239 |
|
| 240 |
def chat_with_code(self, code_content: str, question: str, chat_history: List = None) -> Tuple[str, List]:
|
| 241 |
-
"""Chat about specific code with history."""
|
| 242 |
if chat_history is None:
|
| 243 |
chat_history = []
|
| 244 |
|
|
@@ -258,9 +302,9 @@ Please provide the complete, ready-to-use code with detailed comments."""
|
|
| 258 |
|
| 259 |
Current question: {question}
|
| 260 |
|
| 261 |
-
Please provide a detailed answer about the code. If you're suggesting changes,
|
| 262 |
|
| 263 |
-
response = self.call_claude(prompt)
|
| 264 |
|
| 265 |
# Update chat history
|
| 266 |
chat_history.append((question, response))
|
|
@@ -396,7 +440,7 @@ custom_css = """
|
|
| 396 |
"""
|
| 397 |
|
| 398 |
def analyze_uploaded_code(files, analysis_type, custom_prompt, chat_history):
|
| 399 |
-
"""Analyze uploaded code files with chat functionality."""
|
| 400 |
if not files:
|
| 401 |
return "Please upload at least one file.", chat_history, ""
|
| 402 |
|
|
@@ -428,34 +472,50 @@ def analyze_uploaded_code(files, analysis_type, custom_prompt, chat_history):
|
|
| 428 |
|
| 429 |
return status_with_result, chat_history, chat_display
|
| 430 |
|
| 431 |
-
def
|
| 432 |
-
"""Generate new code
|
| 433 |
if not description.strip():
|
| 434 |
-
return "Please provide a description of what you want to generate.", None
|
| 435 |
|
| 436 |
reference_content = ""
|
| 437 |
if reference_files:
|
| 438 |
reference_content, _ = copilot.process_multiple_files(reference_files)
|
| 439 |
|
| 440 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 441 |
|
| 442 |
# Create downloadable file
|
| 443 |
filename = f"generated_{language}_code.py"
|
| 444 |
temp_file = copilot.create_downloadable_file(result, filename)
|
| 445 |
|
| 446 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 447 |
|
| 448 |
def chat_about_code(files, question, chat_history):
|
| 449 |
-
"""Chat about uploaded code with history."""
|
| 450 |
if not files:
|
| 451 |
-
return "Please upload at least one file.", chat_history, ""
|
| 452 |
|
| 453 |
if not question.strip():
|
| 454 |
-
return "Please ask a question about the code.", chat_history, ""
|
| 455 |
|
| 456 |
content, status = copilot.process_multiple_files(files)
|
| 457 |
if not content or content.startswith("Error"):
|
| 458 |
-
return status or content, chat_history, ""
|
| 459 |
|
| 460 |
if chat_history is None:
|
| 461 |
chat_history = []
|
|
@@ -475,10 +535,11 @@ def chat_about_code(files, question, chat_history):
|
|
| 475 |
return response, updated_history, chat_display, download_file
|
| 476 |
|
| 477 |
# Create Gradio interface with custom theme
|
| 478 |
-
with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.themes.Base()) as app:
|
| 479 |
gr.Markdown("""
|
| 480 |
-
# π₯ Advanced Coding Copilot
|
| 481 |
### Upload multiple Python files (.py) or Jupyter notebooks (.ipynb) for intelligent analysis, or generate new code with AI assistance!
|
|
|
|
| 482 |
""")
|
| 483 |
|
| 484 |
with gr.Tabs():
|
|
@@ -501,7 +562,7 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 501 |
custom_prompt = gr.Textbox(
|
| 502 |
label="βοΈ Custom Prompt / Chat Message",
|
| 503 |
lines=3,
|
| 504 |
-
placeholder="Write your custom analysis prompt
|
| 505 |
visible=False
|
| 506 |
)
|
| 507 |
analyze_btn = gr.Button("π Analyze Code", variant="primary", size="lg")
|
|
@@ -511,12 +572,12 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 511 |
analysis_output = gr.Textbox(
|
| 512 |
label="π Analysis Result / Current Response",
|
| 513 |
lines=15,
|
| 514 |
-
max_lines=
|
| 515 |
)
|
| 516 |
analysis_chat_history = gr.Textbox(
|
| 517 |
label="π¬ Analysis Chat History",
|
| 518 |
lines=15,
|
| 519 |
-
max_lines=
|
| 520 |
placeholder="Chat history will appear here after analysis..."
|
| 521 |
)
|
| 522 |
|
|
@@ -559,15 +620,15 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 559 |
outputs=[analysis_chat_state, analysis_chat_history, analysis_output]
|
| 560 |
)
|
| 561 |
|
| 562 |
-
# Enhanced Code Generation Tab
|
| 563 |
with gr.TabItem("β‘ Generate Code"):
|
| 564 |
-
gr.Markdown("### Generate new code with
|
| 565 |
with gr.Row():
|
| 566 |
with gr.Column(scale=1):
|
| 567 |
code_description = gr.Textbox(
|
| 568 |
-
label="π Describe what you want to code",
|
| 569 |
lines=5,
|
| 570 |
-
placeholder="E.g., Create a REST API using FastAPI
|
| 571 |
)
|
| 572 |
code_language = gr.Dropdown(
|
| 573 |
choices=["python", "javascript", "java", "cpp", "c", "go", "rust"],
|
|
@@ -579,13 +640,21 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 579 |
file_count="multiple",
|
| 580 |
file_types=[".py", ".ipynb"]
|
| 581 |
)
|
| 582 |
-
generate_btn = gr.Button("π― Generate Code", variant="primary", size="lg")
|
|
|
|
| 583 |
|
| 584 |
with gr.Column(scale=2):
|
| 585 |
generated_code = gr.Code(
|
| 586 |
label="π§ Generated Code",
|
| 587 |
language="python",
|
| 588 |
-
lines=25
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
)
|
| 590 |
download_generated = gr.File(
|
| 591 |
label="πΎ Download Generated Code",
|
|
@@ -593,19 +662,26 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 593 |
interactive=False
|
| 594 |
)
|
| 595 |
|
| 596 |
-
|
| 597 |
-
|
| 598 |
-
|
|
|
|
|
|
|
| 599 |
|
| 600 |
generate_btn.click(
|
| 601 |
-
|
| 602 |
-
inputs=[code_description, code_language, reference_files],
|
| 603 |
-
outputs=[generated_code, download_generated]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
)
|
| 605 |
|
| 606 |
# Enhanced Code Chat Tab
|
| 607 |
with gr.TabItem("π¬ Chat About Code"):
|
| 608 |
-
gr.Markdown("### Interactive chat about your code with conversation history")
|
| 609 |
with gr.Row():
|
| 610 |
with gr.Column(scale=1):
|
| 611 |
chat_file_input = gr.File(
|
|
@@ -617,7 +693,7 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 617 |
code_question = gr.Textbox(
|
| 618 |
label="β Ask a question about your code",
|
| 619 |
lines=3,
|
| 620 |
-
placeholder="E.g., How can I optimize this algorithm? What design patterns are used here?"
|
| 621 |
)
|
| 622 |
chat_btn = gr.Button("π Ask Question", variant="primary", size="lg")
|
| 623 |
clear_chat_btn = gr.Button("ποΈ Clear Chat History", variant="secondary")
|
|
@@ -626,12 +702,12 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 626 |
chat_output = gr.Textbox(
|
| 627 |
label="π€ AI Response",
|
| 628 |
lines=15,
|
| 629 |
-
max_lines=
|
| 630 |
)
|
| 631 |
chat_history_display = gr.Textbox(
|
| 632 |
label="π Chat History",
|
| 633 |
lines=10,
|
| 634 |
-
max_lines=
|
| 635 |
)
|
| 636 |
download_chat = gr.File(
|
| 637 |
label="πΎ Download Chat Response",
|
|
@@ -671,13 +747,21 @@ with gr.Blocks(title="π₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
|
|
| 671 |
gr.Markdown("""
|
| 672 |
---
|
| 673 |
### π₯ Features:
|
|
|
|
|
|
|
| 674 |
- **Multi-file Support**: Upload and analyze multiple files simultaneously
|
| 675 |
- **Custom Prompts**: Write your own analysis prompts for specific needs
|
| 676 |
- **Interactive Chat**: Have conversations about your code with full history
|
| 677 |
- **File Downloads**: Get generated code and chat responses as downloadable files
|
| 678 |
- **Reference Context**: Use existing files as reference when generating new code
|
| 679 |
|
| 680 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
""")
|
| 682 |
|
| 683 |
# Launch the app
|
|
|
|
| 29 |
print(f"Error setting up AWS client: {e}")
|
| 30 |
self.bedrock_client = None
|
| 31 |
|
| 32 |
+
def call_claude(self, prompt: str, max_tokens: int = 8000) -> str:
|
| 33 |
+
"""Call Claude 3 Haiku via AWS Bedrock with increased token limit."""
|
| 34 |
if not self.bedrock_client:
|
| 35 |
return "Error: AWS Bedrock client not initialized. Please check your credentials."
|
| 36 |
|
|
|
|
| 121 |
return combined_content, status
|
| 122 |
|
| 123 |
def analyze_code_with_chat(self, code_content: str, analysis_type: str, custom_prompt: str = "", chat_history: List = None) -> Tuple[str, List]:
|
| 124 |
+
"""Analyze code with chat functionality and support for long responses."""
|
| 125 |
if chat_history is None:
|
| 126 |
chat_history = []
|
| 127 |
|
|
|
|
| 135 |
context = ""
|
| 136 |
if chat_history:
|
| 137 |
context = "\n\nPrevious analysis conversation:\n"
|
| 138 |
+
for i, (q, a) in enumerate(chat_history[-3:]): # Last 3 exchanges for context
|
| 139 |
context += f"Q{i+1}: {q}\nA{i+1}: {a}\n"
|
| 140 |
|
| 141 |
prompt = f"""Given this code:
|
|
|
|
| 147 |
|
| 148 |
Follow-up question about the analysis: {custom_prompt}
|
| 149 |
|
| 150 |
+
Please provide a detailed response based on the code and previous analysis. If you're providing code, make sure to include the complete implementation without truncation."""
|
| 151 |
|
| 152 |
+
response = self.call_claude(prompt, max_tokens=8000)
|
| 153 |
chat_history.append((custom_prompt, response))
|
| 154 |
return response, chat_history
|
| 155 |
+
|
| 156 |
+
def analyze_code(self, code_content: str, analysis_type: str, custom_prompt: str = "") -> str:
|
| 157 |
"""Analyze code using Claude with optional custom prompt."""
|
| 158 |
base_prompts = {
|
| 159 |
"review": f"""Please review this code and provide:
|
|
|
|
| 161 |
2. Potential bugs or issues
|
| 162 |
3. Performance improvements
|
| 163 |
4. Best practices suggestions
|
| 164 |
+
5. Security considerations
|
| 165 |
+
|
| 166 |
+
Make sure to provide complete analysis with detailed explanations.""",
|
| 167 |
|
| 168 |
"explain": f"""Please explain this code in detail:
|
| 169 |
1. What does this code do?
|
| 170 |
2. How does it work?
|
| 171 |
3. Key functions and their purposes
|
| 172 |
4. Dependencies and requirements
|
| 173 |
+
5. Usage examples
|
| 174 |
+
|
| 175 |
+
Provide comprehensive explanations for each part.""",
|
| 176 |
|
| 177 |
"optimize": f"""Please optimize this code:
|
| 178 |
1. Suggest performance improvements
|
| 179 |
2. Refactor for better readability
|
| 180 |
3. Reduce complexity where possible
|
| 181 |
4. Improve memory usage
|
| 182 |
+
5. Provide the complete optimized version
|
| 183 |
+
|
| 184 |
+
Make sure to include the full optimized code without truncation.""",
|
| 185 |
|
| 186 |
"debug": f"""Please help debug this code:
|
| 187 |
1. Identify potential bugs
|
| 188 |
2. Suggest fixes
|
| 189 |
3. Add error handling
|
| 190 |
4. Improve robustness
|
| 191 |
+
5. Provide the complete corrected version
|
| 192 |
+
|
| 193 |
+
Include the full corrected code implementation.""",
|
| 194 |
|
| 195 |
"document": f"""Please add comprehensive documentation to this code:
|
| 196 |
1. Add docstrings to functions/classes
|
| 197 |
2. Add inline comments
|
| 198 |
3. Create usage examples
|
| 199 |
4. Document parameters and return values
|
| 200 |
+
5. Add type hints where appropriate
|
| 201 |
+
|
| 202 |
+
Provide the complete documented version of the code.""",
|
| 203 |
|
| 204 |
"custom": custom_prompt
|
| 205 |
}
|
|
|
|
| 216 |
{code_content}
|
| 217 |
```
|
| 218 |
|
| 219 |
+
Please provide a detailed response and if fixes are needed, provide the complete corrected code without any truncation."""
|
| 220 |
+
|
| 221 |
+
return self.call_claude(full_prompt, max_tokens=8000)
|
| 222 |
+
|
| 223 |
+
def generate_code_with_chat(self, description: str, language: str = "python", reference_files: str = "", chat_history: List = None) -> Tuple[str, List]:
|
| 224 |
+
"""Generate code with chat functionality for continuation and modifications."""
|
| 225 |
+
if chat_history is None:
|
| 226 |
+
chat_history = []
|
| 227 |
|
| 228 |
+
# If this is the first generation, do the standard generation
|
| 229 |
+
if not chat_history:
|
| 230 |
+
result = self.generate_code(description, language, reference_files)
|
| 231 |
+
chat_history.append(("Initial Generation Request", description))
|
| 232 |
+
chat_history.append(("Generated Code", result))
|
| 233 |
+
return result, chat_history
|
| 234 |
+
else:
|
| 235 |
+
# This is a follow-up request (continue, modify, etc.)
|
| 236 |
+
context = ""
|
| 237 |
+
if chat_history:
|
| 238 |
+
context = "\n\nPrevious generation conversation:\n"
|
| 239 |
+
for i, (q, a) in enumerate(chat_history[-4:]): # Last 4 exchanges for context
|
| 240 |
+
context += f"Step {i+1}: {q}\nResponse {i+1}: {a}\n"
|
| 241 |
+
|
| 242 |
+
prompt = f"""Based on the previous code generation context:
|
| 243 |
+
{context}
|
| 244 |
+
|
| 245 |
+
New request: {description}
|
| 246 |
+
|
| 247 |
+
Please provide the requested modification/continuation. If you're continuing code, make sure to provide the complete implementation. If you're modifying existing code, provide the full modified version."""
|
| 248 |
+
|
| 249 |
+
response = self.call_claude(prompt, max_tokens=8000)
|
| 250 |
+
chat_history.append((description, response))
|
| 251 |
+
return response, chat_history
|
| 252 |
|
| 253 |
def generate_code(self, description: str, language: str = "python", reference_files: str = "") -> str:
|
| 254 |
"""Generate code based on description with optional reference files."""
|
|
|
|
| 273 |
3. Add type hints where appropriate
|
| 274 |
4. Provide usage examples
|
| 275 |
5. Follow best practices
|
| 276 |
+
6. Generate complete, comprehensive code - don't truncate or abbreviate
|
| 277 |
+
7. If the code is large, provide the full implementation
|
| 278 |
{reference_context}
|
| 279 |
|
| 280 |
+
Please provide the complete, ready-to-use code with detailed comments. Make sure to generate comprehensive code that fully implements the requested functionality."""
|
| 281 |
|
| 282 |
+
return self.call_claude(prompt, max_tokens=8000)
|
| 283 |
|
| 284 |
def chat_with_code(self, code_content: str, question: str, chat_history: List = None) -> Tuple[str, List]:
|
| 285 |
+
"""Chat about specific code with history and support for long responses."""
|
| 286 |
if chat_history is None:
|
| 287 |
chat_history = []
|
| 288 |
|
|
|
|
| 302 |
|
| 303 |
Current question: {question}
|
| 304 |
|
| 305 |
+
Please provide a detailed answer about the code. If you're suggesting changes or providing code, make sure to include the complete implementation without truncation."""
|
| 306 |
|
| 307 |
+
response = self.call_claude(prompt, max_tokens=8000)
|
| 308 |
|
| 309 |
# Update chat history
|
| 310 |
chat_history.append((question, response))
|
|
|
|
| 440 |
"""
|
| 441 |
|
| 442 |
def analyze_uploaded_code(files, analysis_type, custom_prompt, chat_history):
|
| 443 |
+
"""Analyze uploaded code files with chat functionality and long response support."""
|
| 444 |
if not files:
|
| 445 |
return "Please upload at least one file.", chat_history, ""
|
| 446 |
|
|
|
|
| 472 |
|
| 473 |
return status_with_result, chat_history, chat_display
|
| 474 |
|
| 475 |
+
def generate_new_code_with_chat(description, language, reference_files, generation_chat_history):
|
| 476 |
+
"""Generate new code with chat support for continuation and modifications."""
|
| 477 |
if not description.strip():
|
| 478 |
+
return "Please provide a description of what you want to generate.", None, generation_chat_history, ""
|
| 479 |
|
| 480 |
reference_content = ""
|
| 481 |
if reference_files:
|
| 482 |
reference_content, _ = copilot.process_multiple_files(reference_files)
|
| 483 |
|
| 484 |
+
if generation_chat_history is None:
|
| 485 |
+
generation_chat_history = []
|
| 486 |
+
|
| 487 |
+
# Check if this is initial generation or continuation
|
| 488 |
+
if not generation_chat_history:
|
| 489 |
+
# Initial generation
|
| 490 |
+
result = copilot.generate_code(description, language, reference_content)
|
| 491 |
+
generation_chat_history.append(("Initial Generation", description))
|
| 492 |
+
generation_chat_history.append(("Generated Code", result))
|
| 493 |
+
else:
|
| 494 |
+
# Continuation or modification
|
| 495 |
+
result, generation_chat_history = copilot.generate_code_with_chat(description, language, reference_content, generation_chat_history)
|
| 496 |
|
| 497 |
# Create downloadable file
|
| 498 |
filename = f"generated_{language}_code.py"
|
| 499 |
temp_file = copilot.create_downloadable_file(result, filename)
|
| 500 |
|
| 501 |
+
# Format chat history for display
|
| 502 |
+
chat_display = ""
|
| 503 |
+
for i, (q, a) in enumerate(generation_chat_history):
|
| 504 |
+
chat_display += f"**Step {i+1}:** {q}\n\n**Response {i+1}:** {a}\n\n---\n\n"
|
| 505 |
+
|
| 506 |
+
return result, temp_file, generation_chat_history, chat_display
|
| 507 |
|
| 508 |
def chat_about_code(files, question, chat_history):
|
| 509 |
+
"""Chat about uploaded code with history and long response support."""
|
| 510 |
if not files:
|
| 511 |
+
return "Please upload at least one file.", chat_history, "", None
|
| 512 |
|
| 513 |
if not question.strip():
|
| 514 |
+
return "Please ask a question about the code.", chat_history, "", None
|
| 515 |
|
| 516 |
content, status = copilot.process_multiple_files(files)
|
| 517 |
if not content or content.startswith("Error"):
|
| 518 |
+
return status or content, chat_history, "", None
|
| 519 |
|
| 520 |
if chat_history is None:
|
| 521 |
chat_history = []
|
|
|
|
| 535 |
return response, updated_history, chat_display, download_file
|
| 536 |
|
| 537 |
# Create Gradio interface with custom theme
|
| 538 |
+
with gr.Blocks(title="π₯ Advanced Coding Copilot - Long Code Support", css=custom_css, theme=gr.themes.Base()) as app:
|
| 539 |
gr.Markdown("""
|
| 540 |
+
# π₯ Advanced Coding Copilot - Long Code Support
|
| 541 |
### Upload multiple Python files (.py) or Jupyter notebooks (.ipynb) for intelligent analysis, or generate new code with AI assistance!
|
| 542 |
+
### β¨ **NEW**: Support for extremely long code generation (up to 5000+ lines) with chat continuation!
|
| 543 |
""")
|
| 544 |
|
| 545 |
with gr.Tabs():
|
|
|
|
| 562 |
custom_prompt = gr.Textbox(
|
| 563 |
label="βοΈ Custom Prompt / Chat Message",
|
| 564 |
lines=3,
|
| 565 |
+
placeholder="Write your custom analysis prompt, ask questions, or request 'continue' for more details...",
|
| 566 |
visible=False
|
| 567 |
)
|
| 568 |
analyze_btn = gr.Button("π Analyze Code", variant="primary", size="lg")
|
|
|
|
| 572 |
analysis_output = gr.Textbox(
|
| 573 |
label="π Analysis Result / Current Response",
|
| 574 |
lines=15,
|
| 575 |
+
max_lines=30
|
| 576 |
)
|
| 577 |
analysis_chat_history = gr.Textbox(
|
| 578 |
label="π¬ Analysis Chat History",
|
| 579 |
lines=15,
|
| 580 |
+
max_lines=30,
|
| 581 |
placeholder="Chat history will appear here after analysis..."
|
| 582 |
)
|
| 583 |
|
|
|
|
| 620 |
outputs=[analysis_chat_state, analysis_chat_history, analysis_output]
|
| 621 |
)
|
| 622 |
|
| 623 |
+
# Enhanced Code Generation Tab with Chat Support
|
| 624 |
with gr.TabItem("β‘ Generate Code"):
|
| 625 |
+
gr.Markdown("### Generate new code with chat support for continuation and modifications")
|
| 626 |
with gr.Row():
|
| 627 |
with gr.Column(scale=1):
|
| 628 |
code_description = gr.Textbox(
|
| 629 |
+
label="π Describe what you want to code / Chat Message",
|
| 630 |
lines=5,
|
| 631 |
+
placeholder="E.g., Create a REST API using FastAPI... OR 'continue the previous code' OR 'add more features' OR 'optimize the generated code'"
|
| 632 |
)
|
| 633 |
code_language = gr.Dropdown(
|
| 634 |
choices=["python", "javascript", "java", "cpp", "c", "go", "rust"],
|
|
|
|
| 640 |
file_count="multiple",
|
| 641 |
file_types=[".py", ".ipynb"]
|
| 642 |
)
|
| 643 |
+
generate_btn = gr.Button("π― Generate/Continue Code", variant="primary", size="lg")
|
| 644 |
+
clear_generation_btn = gr.Button("ποΈ Clear Generation Chat", variant="secondary")
|
| 645 |
|
| 646 |
with gr.Column(scale=2):
|
| 647 |
generated_code = gr.Code(
|
| 648 |
label="π§ Generated Code",
|
| 649 |
language="python",
|
| 650 |
+
lines=25,
|
| 651 |
+
max_lines=50
|
| 652 |
+
)
|
| 653 |
+
generation_chat_history = gr.Textbox(
|
| 654 |
+
label="π¬ Generation Chat History",
|
| 655 |
+
lines=10,
|
| 656 |
+
max_lines=20,
|
| 657 |
+
placeholder="Generation history will appear here..."
|
| 658 |
)
|
| 659 |
download_generated = gr.File(
|
| 660 |
label="πΎ Download Generated Code",
|
|
|
|
| 662 |
interactive=False
|
| 663 |
)
|
| 664 |
|
| 665 |
+
# Hidden state for generation chat history
|
| 666 |
+
generation_chat_state = gr.State([])
|
| 667 |
+
|
| 668 |
+
def clear_generation_chat():
|
| 669 |
+
return [], "", ""
|
| 670 |
|
| 671 |
generate_btn.click(
|
| 672 |
+
generate_new_code_with_chat,
|
| 673 |
+
inputs=[code_description, code_language, reference_files, generation_chat_state],
|
| 674 |
+
outputs=[generated_code, download_generated, generation_chat_state, generation_chat_history]
|
| 675 |
+
)
|
| 676 |
+
|
| 677 |
+
clear_generation_btn.click(
|
| 678 |
+
clear_generation_chat,
|
| 679 |
+
outputs=[generation_chat_state, generation_chat_history, generated_code]
|
| 680 |
)
|
| 681 |
|
| 682 |
# Enhanced Code Chat Tab
|
| 683 |
with gr.TabItem("π¬ Chat About Code"):
|
| 684 |
+
gr.Markdown("### Interactive chat about your code with conversation history and long response support")
|
| 685 |
with gr.Row():
|
| 686 |
with gr.Column(scale=1):
|
| 687 |
chat_file_input = gr.File(
|
|
|
|
| 693 |
code_question = gr.Textbox(
|
| 694 |
label="β Ask a question about your code",
|
| 695 |
lines=3,
|
| 696 |
+
placeholder="E.g., How can I optimize this algorithm? What design patterns are used here? Continue explaining... Add more features..."
|
| 697 |
)
|
| 698 |
chat_btn = gr.Button("π Ask Question", variant="primary", size="lg")
|
| 699 |
clear_chat_btn = gr.Button("ποΈ Clear Chat History", variant="secondary")
|
|
|
|
| 702 |
chat_output = gr.Textbox(
|
| 703 |
label="π€ AI Response",
|
| 704 |
lines=15,
|
| 705 |
+
max_lines=30
|
| 706 |
)
|
| 707 |
chat_history_display = gr.Textbox(
|
| 708 |
label="π Chat History",
|
| 709 |
lines=10,
|
| 710 |
+
max_lines=25
|
| 711 |
)
|
| 712 |
download_chat = gr.File(
|
| 713 |
label="πΎ Download Chat Response",
|
|
|
|
| 747 |
gr.Markdown("""
|
| 748 |
---
|
| 749 |
### π₯ Features:
|
| 750 |
+
- **π Long Code Support**: Generate up to 5000+ lines of code with continuation support
|
| 751 |
+
- **π¬ Chat Continuation**: Ask "continue", "add more features", or "extend the code" in any tab
|
| 752 |
- **Multi-file Support**: Upload and analyze multiple files simultaneously
|
| 753 |
- **Custom Prompts**: Write your own analysis prompts for specific needs
|
| 754 |
- **Interactive Chat**: Have conversations about your code with full history
|
| 755 |
- **File Downloads**: Get generated code and chat responses as downloadable files
|
| 756 |
- **Reference Context**: Use existing files as reference when generating new code
|
| 757 |
|
| 758 |
+
### π‘ Pro Tips:
|
| 759 |
+
- Use "continue" or "continue the code" to extend generated code
|
| 760 |
+
- Ask "add more features" to enhance existing implementations
|
| 761 |
+
- Use "optimize" or "refactor" to improve code quality
|
| 762 |
+
- Upload reference files to generate code based on existing patterns
|
| 763 |
+
|
| 764 |
+
**Powered by:** Claude 3 Haiku via AWS Bedrock | **Theme:** Cyberpunk Black & Red | **Max Tokens:** 8000 per response
|
| 765 |
""")
|
| 766 |
|
| 767 |
# Launch the app
|