prernajeet14 commited on
Commit
c001f4d
Β·
verified Β·
1 Parent(s): 91043f2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +106 -19
app.py CHANGED
@@ -120,7 +120,38 @@ class CodingCopilot:
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:
@@ -364,17 +395,38 @@ custom_css = """
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."""
@@ -432,7 +484,7 @@ with gr.Blocks(title="πŸ”₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
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(
@@ -442,34 +494,69 @@ with gr.Blocks(title="πŸ”₯ Advanced Coding Copilot", css=custom_css, theme=gr.th
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
  )
449
  custom_prompt = gr.Textbox(
450
- label="✏️ Custom Prompt (when Analysis Type = 'custom')",
451
  lines=3,
452
- placeholder="Write your custom analysis prompt here...",
453
  visible=False
454
  )
455
  analyze_btn = gr.Button("πŸš€ Analyze Code", variant="primary", size="lg")
 
456
 
457
  with gr.Column(scale=2):
458
  analysis_output = gr.Textbox(
459
- label="πŸ“Š Analysis Result",
460
- lines=25,
461
- max_lines=40
 
 
 
 
 
 
462
  )
463
 
 
 
 
464
  # Show/hide custom prompt based on analysis type
465
- def toggle_custom_prompt(analysis_type):
466
- return gr.update(visible=(analysis_type == "custom"))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
467
 
468
- analysis_type.change(toggle_custom_prompt, inputs=analysis_type, outputs=custom_prompt)
469
  analyze_btn.click(
470
  analyze_uploaded_code,
471
- inputs=[file_input, analysis_type, custom_prompt],
472
- outputs=analysis_output
 
 
 
 
 
473
  )
474
 
475
  # Enhanced Code Generation Tab
 
120
 
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
+
128
+ # If this is the first analysis, do the standard analysis
129
+ if not chat_history:
130
+ result = self.analyze_code(code_content, analysis_type, custom_prompt)
131
+ chat_history.append(("Initial Analysis", result))
132
+ return result, chat_history
133
+ else:
134
+ # This is a follow-up question
135
+ context = ""
136
+ if chat_history:
137
+ context = "\n\nPrevious analysis conversation:\n"
138
+ for i, (q, a) in enumerate(chat_history[-2:]): # Last 2 exchanges for context
139
+ context += f"Q{i+1}: {q}\nA{i+1}: {a}\n"
140
+
141
+ prompt = f"""Given this code:
142
+
143
+ ```python
144
+ {code_content}
145
+ ```
146
+ {context}
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:
 
395
  }
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
 
403
  content, status = copilot.process_multiple_files(files)
404
  if not content or content.startswith("Error"):
405
+ return status or content, chat_history, ""
406
+
407
+ if chat_history is None:
408
+ chat_history = []
409
+
410
+ # Check if this is initial analysis or follow-up chat
411
+ if not chat_history and analysis_type != "chat":
412
+ # Initial analysis
413
+ result = copilot.analyze_code(content, analysis_type, custom_prompt)
414
+ chat_history.append((f"Initial {analysis_type} analysis", result))
415
+ status_with_result = f"**Status:** {status}\n\n**Analysis Result:**\n{result}"
416
+ else:
417
+ # Follow-up chat or direct chat
418
+ if not custom_prompt.strip():
419
+ return "Please ask a question about the analyzed code.", chat_history, ""
420
+
421
+ result, chat_history = copilot.analyze_code_with_chat(content, analysis_type, custom_prompt, chat_history)
422
+ status_with_result = result
423
+
424
+ # Format chat history for display
425
+ chat_display = ""
426
+ for i, (q, a) in enumerate(chat_history):
427
+ chat_display += f"**Q{i+1}:** {q}\n\n**A{i+1}:** {a}\n\n---\n\n"
428
 
429
+ return status_with_result, chat_history, chat_display
 
430
 
431
  def generate_new_code(description, language, reference_files):
432
  """Generate new code based on description with reference files."""
 
484
  with gr.Tabs():
485
  # Enhanced File Analysis Tab
486
  with gr.TabItem("πŸ“ Analyze Code"):
487
+ gr.Markdown("### Upload multiple files and get comprehensive analysis with chat support")
488
  with gr.Row():
489
  with gr.Column(scale=1):
490
  file_input = gr.File(
 
494
  height=120
495
  )
496
  analysis_type = gr.Dropdown(
497
+ choices=["review", "explain", "optimize", "debug", "document", "custom", "chat"],
498
  value="review",
499
  label="πŸ” Analysis Type"
500
  )
501
  custom_prompt = gr.Textbox(
502
+ label="✏️ Custom Prompt / Chat Message",
503
  lines=3,
504
+ placeholder="Write your custom analysis prompt or ask questions about the code...",
505
  visible=False
506
  )
507
  analyze_btn = gr.Button("πŸš€ Analyze Code", variant="primary", size="lg")
508
+ clear_analysis_btn = gr.Button("πŸ—‘οΈ Clear Analysis Chat", variant="secondary")
509
 
510
  with gr.Column(scale=2):
511
  analysis_output = gr.Textbox(
512
+ label="πŸ“Š Analysis Result / Current Response",
513
+ lines=15,
514
+ max_lines=25
515
+ )
516
+ analysis_chat_history = gr.Textbox(
517
+ label="πŸ’¬ Analysis Chat History",
518
+ lines=15,
519
+ max_lines=25,
520
+ placeholder="Chat history will appear here after analysis..."
521
  )
522
 
523
+ # Hidden state for analysis chat history
524
+ analysis_chat_state = gr.State([])
525
+
526
  # Show/hide custom prompt based on analysis type
527
+ def toggle_custom_prompt_analysis(analysis_type):
528
+ if analysis_type in ["custom", "chat"]:
529
+ return gr.update(visible=True, label="✏️ Custom Prompt" if analysis_type == "custom" else "πŸ’¬ Chat Message")
530
+ return gr.update(visible=False)
531
+
532
+ def clear_analysis_chat():
533
+ return [], "", ""
534
+
535
+ def update_button_text(analysis_type):
536
+ if analysis_type == "chat":
537
+ return gr.update(value="πŸ’­ Send Message")
538
+ return gr.update(value="πŸš€ Analyze Code")
539
+
540
+ analysis_type.change(
541
+ toggle_custom_prompt_analysis,
542
+ inputs=analysis_type,
543
+ outputs=custom_prompt
544
+ )
545
+ analysis_type.change(
546
+ update_button_text,
547
+ inputs=analysis_type,
548
+ outputs=analyze_btn
549
+ )
550
 
 
551
  analyze_btn.click(
552
  analyze_uploaded_code,
553
+ inputs=[file_input, analysis_type, custom_prompt, analysis_chat_state],
554
+ outputs=[analysis_output, analysis_chat_state, analysis_chat_history]
555
+ )
556
+
557
+ clear_analysis_btn.click(
558
+ clear_analysis_chat,
559
+ outputs=[analysis_chat_state, analysis_chat_history, analysis_output]
560
  )
561
 
562
  # Enhanced Code Generation Tab