CosmickVisions commited on
Commit
4addefb
Β·
verified Β·
1 Parent(s): 4c3afa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -94
app.py CHANGED
@@ -461,7 +461,7 @@ def process_code_file(file_obj):
461
  language = detect_language(file_extension)
462
 
463
  # Calculate metrics
464
- metrics = calculate_complexity_metrics(content, language)
465
 
466
  # Create vectorstore if embeddings are available
467
  session_id = None
@@ -493,86 +493,18 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
493
  </div>
494
  """)
495
 
496
- with gr.Row(elem_classes="container"):
497
- with gr.Column(scale=1, min_width=300):
498
- file_input = gr.File(
499
- label="Upload Code File",
500
- file_types=[".py", ".js", ".java", ".cpp", ".c", ".cs", ".php", ".rb", ".go", ".ts"],
501
- type="binary"
502
- )
503
- upload_button = gr.Button("Analyze Code", variant="primary")
504
- file_status = gr.Markdown("No file uploaded yet")
505
- model_dropdown = gr.Dropdown(
506
- choices=["llama3-70b-8192", "mixtral-8x7b-32768", "gemma-7b-it"],
507
- value="llama3-70b-8192",
508
- label="Select Model"
509
- )
510
 
511
- # Developer Tools Section
512
- gr.Markdown("### Developer Tools", elem_classes="tool-title")
513
- with gr.Group(elem_classes="tool-container"): # Replace Box with Group
514
- with gr.Tabs():
515
- with gr.TabItem("GitHub Search"):
516
- repo_query = gr.Textbox(label="Search Query", placeholder="Enter keywords to search for repositories")
517
- with gr.Row():
518
- language = gr.Dropdown(
519
- choices=["any", "JavaScript", "Python", "Java", "C++", "TypeScript", "Go", "Rust", "PHP", "C#"],
520
- value="any",
521
- label="Language"
522
- )
523
- min_stars = gr.Dropdown(
524
- choices=["0", "10", "50", "100", "1000", "10000"],
525
- value="0",
526
- label="Min Stars"
527
- )
528
- sort_by = gr.Dropdown(
529
- choices=["stars", "forks", "updated"],
530
- value="stars",
531
- label="Sort By"
532
- )
533
- repo_search_btn = gr.Button("Search Repositories")
534
-
535
- with gr.TabItem("Stack Overflow"):
536
- stack_query = gr.Textbox(label="Search Query", placeholder="Enter your technical question")
537
- with gr.Row():
538
- tag = gr.Dropdown(
539
- choices=["any", "python", "javascript", "java", "c++", "react", "node.js", "android", "ios", "sql"],
540
- value="any",
541
- label="Tag"
542
- )
543
- so_sort_by = gr.Dropdown(
544
- choices=["votes", "newest", "activity"],
545
- value="votes",
546
- label="Sort By"
547
- )
548
- so_search_btn = gr.Button("Search Stack Overflow")
549
-
550
- with gr.TabItem("Code Explainer"):
551
- code_input = gr.Textbox(
552
- label="Code to Explain",
553
- placeholder="Paste your code here...",
554
- lines=10
555
- )
556
- explain_btn = gr.Button("Explain Code")
557
-
558
- with gr.Column(scale=2, min_width=600):
559
- with gr.Tabs():
560
- with gr.TabItem("Code Analysis"):
561
- with gr.Column(elem_classes="code-viewer-container"):
562
- code_metrics = gr.Markdown("No code analyzed yet", elem_classes="stats-box")
563
- code_recommendations = gr.Markdown("", elem_classes="recommendations-box")
564
-
565
- with gr.TabItem("GitHub Results"):
566
- repo_results = gr.Markdown("Search for repositories to see results here")
567
-
568
- with gr.TabItem("Stack Overflow Results"):
569
- stack_results = gr.Markdown("Search for questions to see results here")
570
-
571
- with gr.TabItem("Code Explanation"):
572
- code_explanation = gr.Markdown("Paste your code and click 'Explain Code' to see an explanation here")
573
-
574
- with gr.Row(elem_classes="container"):
575
- with gr.Column(scale=2, min_width=600):
576
  chatbot = gr.Chatbot(
577
  height=500,
578
  show_copy_button=True,
@@ -587,8 +519,73 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
587
  )
588
  send_btn = gr.Button("Send", scale=1)
589
  clear_btn = gr.Button("Clear Conversation")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
590
 
591
- # Update event handlers
592
  upload_button.click(
593
  process_code_file,
594
  inputs=[file_input],
@@ -607,7 +604,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
607
  inputs=[code_state],
608
  outputs=[code_metrics]
609
  ).then(
610
- lambda state: generate_recommendations(state),
611
  inputs=[code_state],
612
  outputs=[code_recommendations]
613
  )
@@ -693,20 +690,64 @@ def calculate_complexity_metrics(content, language):
693
 
694
  return metrics
695
 
696
- def generate_recommendations(metrics):
697
- """Generate code quality recommendations based on metrics"""
698
- recommendations = []
699
 
700
- if metrics.get("cyclomatic_complexity", 0) > 10:
701
- recommendations.append("πŸ”„ High cyclomatic complexity detected. Consider breaking down complex functions.")
 
 
 
702
 
703
- if metrics.get("code_lines", 0) > 300:
704
- recommendations.append("πŸ“ File is quite large. Consider splitting it into multiple modules.")
705
 
706
- if metrics.get("functions", 0) > 10:
707
- recommendations.append("πŸ”§ Large number of functions. Consider grouping related functions into classes.")
 
 
 
 
 
 
 
 
708
 
709
- if metrics.get("comments", 0) / max(metrics.get("code_lines", 1), 1) < 0.1:
710
- recommendations.append("πŸ“ Low comment ratio. Consider adding more documentation.")
 
 
 
 
 
 
 
711
 
712
- return "### Recommendations\n\n" + "\n\n".join(recommendations) if recommendations else ""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
461
  language = detect_language(file_extension)
462
 
463
  # Calculate metrics
464
+ metrics = enhanced_code_analysis(content, language)
465
 
466
  # Create vectorstore if embeddings are available
467
  session_id = None
 
493
  </div>
494
  """)
495
 
496
+ # Main container with all functionality in tabs
497
+ with gr.Tabs() as main_tabs:
498
+ # Chat Assistant Tab
499
+ with gr.TabItem("πŸ’¬ Chat Assistant", id=0):
500
+ with gr.Row():
501
+ with gr.Column(scale=1):
502
+ model_dropdown = gr.Dropdown(
503
+ choices=["llama3-70b-8192", "mixtral-8x7b-32768", "gemma-7b-it"],
504
+ value="llama3-70b-8192",
505
+ label="Model Selection"
506
+ )
 
 
 
507
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
508
  chatbot = gr.Chatbot(
509
  height=500,
510
  show_copy_button=True,
 
519
  )
520
  send_btn = gr.Button("Send", scale=1)
521
  clear_btn = gr.Button("Clear Conversation")
522
+
523
+ # Code Analysis Tab
524
+ with gr.TabItem("πŸ“„ Code Analysis", id=1):
525
+ with gr.Row():
526
+ with gr.Column(scale=1):
527
+ file_input = gr.File(
528
+ label="Upload Code File",
529
+ file_types=[".py", ".js", ".java", ".cpp", ".c", ".cs", ".php", ".rb", ".go", ".ts"],
530
+ type="binary"
531
+ )
532
+ upload_button = gr.Button("Analyze Code", variant="primary")
533
+ file_status = gr.Markdown("No file uploaded yet")
534
+
535
+ with gr.Column(scale=2):
536
+ code_metrics = gr.Markdown("No code analyzed yet", elem_classes="stats-box")
537
+ code_recommendations = gr.Markdown("", elem_classes="recommendations-box")
538
+
539
+ # Developer Tools Tab
540
+ with gr.TabItem("πŸ” Developer Tools", id=2):
541
+ with gr.Tabs():
542
+ with gr.TabItem("GitHub Search"):
543
+ repo_query = gr.Textbox(label="Search Query", placeholder="Enter keywords to search for repositories")
544
+ with gr.Row():
545
+ language = gr.Dropdown(
546
+ choices=["any", "JavaScript", "Python", "Java", "C++", "TypeScript", "Go", "Rust", "PHP", "C#"],
547
+ value="any",
548
+ label="Language"
549
+ )
550
+ min_stars = gr.Dropdown(
551
+ choices=["0", "10", "50", "100", "1000", "10000"],
552
+ value="0",
553
+ label="Min Stars"
554
+ )
555
+ sort_by = gr.Dropdown(
556
+ choices=["stars", "forks", "updated"],
557
+ value="stars",
558
+ label="Sort By"
559
+ )
560
+ repo_search_btn = gr.Button("Search Repositories")
561
+ repo_results = gr.Markdown("Search for repositories to see results here")
562
+
563
+ with gr.TabItem("Stack Overflow"):
564
+ stack_query = gr.Textbox(label="Search Query", placeholder="Enter your technical question")
565
+ with gr.Row():
566
+ tag = gr.Dropdown(
567
+ choices=["any", "python", "javascript", "java", "c++", "react", "node.js", "android", "ios", "sql"],
568
+ value="any",
569
+ label="Tag"
570
+ )
571
+ so_sort_by = gr.Dropdown(
572
+ choices=["votes", "newest", "activity"],
573
+ value="votes",
574
+ label="Sort By"
575
+ )
576
+ so_search_btn = gr.Button("Search Stack Overflow")
577
+ stack_results = gr.Markdown("Search for questions to see results here")
578
+
579
+ with gr.TabItem("Code Explainer"):
580
+ code_input = gr.Textbox(
581
+ label="Code to Explain",
582
+ placeholder="Paste your code here...",
583
+ lines=10
584
+ )
585
+ explain_btn = gr.Button("Explain Code")
586
+ code_explanation = gr.Markdown("Paste your code and click 'Explain Code' to see an explanation here")
587
 
588
+ # Event Handlers
589
  upload_button.click(
590
  process_code_file,
591
  inputs=[file_input],
 
604
  inputs=[code_state],
605
  outputs=[code_metrics]
606
  ).then(
607
+ lambda state: generate_enhanced_recommendations(state),
608
  inputs=[code_state],
609
  outputs=[code_recommendations]
610
  )
 
690
 
691
  return metrics
692
 
693
+ def enhanced_code_analysis(content, language):
694
+ """Improved code analysis with more metrics"""
695
+ metrics = calculate_complexity_metrics(content, language)
696
 
697
+ # Add security scanning
698
+ security_issues = scan_security_issues(content, language)
699
+
700
+ # Add best practices analysis
701
+ best_practices = analyze_best_practices(content, language)
702
 
703
+ # Add performance suggestions
704
+ performance_tips = analyze_performance(content, language)
705
 
706
+ return {
707
+ **metrics,
708
+ "security_issues": security_issues,
709
+ "best_practices": best_practices,
710
+ "performance_tips": performance_tips
711
+ }
712
+
713
+ def generate_enhanced_recommendations(metrics):
714
+ """Enhanced recommendations with more detail"""
715
+ recommendations = []
716
 
717
+ # Existing complexity checks
718
+ if metrics.get("cyclomatic_complexity", 0) > 10:
719
+ recommendations.append({
720
+ "type": "complexity",
721
+ "severity": "warning",
722
+ "message": "πŸ”„ High cyclomatic complexity detected",
723
+ "details": "Consider breaking down complex functions into smaller, more manageable pieces.",
724
+ "examples": ["Extract repeated logic into helper functions", "Use early returns to reduce nesting"]
725
+ })
726
 
727
+ # Add security recommendations
728
+ for issue in metrics.get("security_issues", []):
729
+ recommendations.append({
730
+ "type": "security",
731
+ "severity": issue["severity"],
732
+ "message": f"πŸ”’ {issue['title']}",
733
+ "details": issue["description"],
734
+ "fix": issue["recommendation"]
735
+ })
736
+
737
+ return format_recommendations(recommendations)
738
+
739
+ def scan_security_issues(content, language):
740
+ # This function is called but not defined
741
+ pass
742
+
743
+ def analyze_best_practices(content, language):
744
+ # This function is called but not defined
745
+ pass
746
+
747
+ def analyze_performance(content, language):
748
+ # This function is called but not defined
749
+ pass
750
+
751
+ def format_recommendations(recommendations):
752
+ # This function is called but not defined
753
+ pass