Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -5,7 +5,7 @@ import tempfile
|
|
5 |
import uuid
|
6 |
from dotenv import load_dotenv
|
7 |
from langchain_community.vectorstores import FAISS
|
8 |
-
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
|
9 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
import fitz # PyMuPDF
|
11 |
import base64
|
@@ -38,8 +38,7 @@ except Exception as e:
|
|
38 |
)
|
39 |
except Exception as e:
|
40 |
print(f"Warning: Failed to load fallback embeddings model: {e}")
|
41 |
-
|
42 |
-
embeddings = HuggingFaceEmbeddings()
|
43 |
|
44 |
# Directory to store FAISS indexes
|
45 |
FAISS_INDEX_DIR = "faiss_indexes_tech"
|
@@ -49,54 +48,69 @@ if not os.path.exists(FAISS_INDEX_DIR):
|
|
49 |
# Dictionary to store user-specific vectorstores
|
50 |
user_vectorstores = {}
|
51 |
|
52 |
-
# Custom CSS for Tech theme
|
53 |
custom_css = """
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
background:
|
59 |
-
|
60 |
-
|
61 |
-
color:
|
62 |
-
|
63 |
-
margin: var(--spacing-md) 0;
|
64 |
-
border-left: 3px solid var(--accent-color);
|
65 |
-
box-shadow: var(--shadow-sm);
|
66 |
}
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
color: var(--text-color);
|
71 |
-
padding: var(--spacing-md);
|
72 |
-
border-radius: var(--radius-md);
|
73 |
-
margin: var(--spacing-md) 0;
|
74 |
-
border: 1px solid var(--border-color);
|
75 |
-
box-shadow: var(--shadow-sm);
|
76 |
}
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
.string { color: var(--text-color-secondary); }
|
82 |
-
|
83 |
-
/* Custom Scrollbar */
|
84 |
-
::-webkit-scrollbar {
|
85 |
-
width: 8px;
|
86 |
-
height: 8px;
|
87 |
}
|
88 |
-
|
89 |
-
|
90 |
-
|
|
|
|
|
|
|
|
|
91 |
}
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
|
|
96 |
}
|
97 |
-
|
98 |
-
|
99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
}
|
101 |
"""
|
102 |
|
@@ -439,7 +453,7 @@ def process_code_file(file_obj):
|
|
439 |
language = detect_language(file_extension)
|
440 |
|
441 |
# Calculate metrics
|
442 |
-
metrics =
|
443 |
|
444 |
# Create vectorstore if embeddings are available
|
445 |
session_id = None
|
@@ -471,18 +485,86 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
471 |
</div>
|
472 |
""")
|
473 |
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
-
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
-
|
483 |
-
|
484 |
-
|
|
|
|
|
|
|
485 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
486 |
chatbot = gr.Chatbot(
|
487 |
height=500,
|
488 |
show_copy_button=True,
|
@@ -497,73 +579,8 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
497 |
)
|
498 |
send_btn = gr.Button("Send", scale=1)
|
499 |
clear_btn = gr.Button("Clear Conversation")
|
500 |
-
|
501 |
-
# Code Analysis Tab
|
502 |
-
with gr.TabItem("π Code Analysis", id=1):
|
503 |
-
with gr.Row():
|
504 |
-
with gr.Column(scale=1):
|
505 |
-
file_input = gr.File(
|
506 |
-
label="Upload Code File",
|
507 |
-
file_types=[".py", ".js", ".java", ".cpp", ".c", ".cs", ".php", ".rb", ".go", ".ts"],
|
508 |
-
type="binary"
|
509 |
-
)
|
510 |
-
upload_button = gr.Button("Analyze Code", variant="primary")
|
511 |
-
file_status = gr.Markdown("No file uploaded yet")
|
512 |
-
|
513 |
-
with gr.Column(scale=2):
|
514 |
-
code_metrics = gr.Markdown("No code analyzed yet", elem_classes="stats-box")
|
515 |
-
code_recommendations = gr.Markdown("", elem_classes="recommendations-box")
|
516 |
-
|
517 |
-
# Developer Tools Tab
|
518 |
-
with gr.TabItem("π Developer Tools", id=2):
|
519 |
-
with gr.Tabs():
|
520 |
-
with gr.TabItem("GitHub Search"):
|
521 |
-
repo_query = gr.Textbox(label="Search Query", placeholder="Enter keywords to search for repositories")
|
522 |
-
with gr.Row():
|
523 |
-
language = gr.Dropdown(
|
524 |
-
choices=["any", "JavaScript", "Python", "Java", "C++", "TypeScript", "Go", "Rust", "PHP", "C#"],
|
525 |
-
value="any",
|
526 |
-
label="Language"
|
527 |
-
)
|
528 |
-
min_stars = gr.Dropdown(
|
529 |
-
choices=["0", "10", "50", "100", "1000", "10000"],
|
530 |
-
value="0",
|
531 |
-
label="Min Stars"
|
532 |
-
)
|
533 |
-
sort_by = gr.Dropdown(
|
534 |
-
choices=["stars", "forks", "updated"],
|
535 |
-
value="stars",
|
536 |
-
label="Sort By"
|
537 |
-
)
|
538 |
-
repo_search_btn = gr.Button("Search Repositories")
|
539 |
-
repo_results = gr.Markdown("Search for repositories to see results here")
|
540 |
-
|
541 |
-
with gr.TabItem("Stack Overflow"):
|
542 |
-
stack_query = gr.Textbox(label="Search Query", placeholder="Enter your technical question")
|
543 |
-
with gr.Row():
|
544 |
-
tag = gr.Dropdown(
|
545 |
-
choices=["any", "python", "javascript", "java", "c++", "react", "node.js", "android", "ios", "sql"],
|
546 |
-
value="any",
|
547 |
-
label="Tag"
|
548 |
-
)
|
549 |
-
so_sort_by = gr.Dropdown(
|
550 |
-
choices=["votes", "newest", "activity"],
|
551 |
-
value="votes",
|
552 |
-
label="Sort By"
|
553 |
-
)
|
554 |
-
so_search_btn = gr.Button("Search Stack Overflow")
|
555 |
-
stack_results = gr.Markdown("Search for questions to see results here")
|
556 |
-
|
557 |
-
with gr.TabItem("Code Explainer"):
|
558 |
-
code_input = gr.Textbox(
|
559 |
-
label="Code to Explain",
|
560 |
-
placeholder="Paste your code here...",
|
561 |
-
lines=10
|
562 |
-
)
|
563 |
-
explain_btn = gr.Button("Explain Code")
|
564 |
-
code_explanation = gr.Markdown("Paste your code and click 'Explain Code' to see an explanation here")
|
565 |
|
566 |
-
#
|
567 |
upload_button.click(
|
568 |
process_code_file,
|
569 |
inputs=[file_input],
|
@@ -582,7 +599,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
582 |
inputs=[code_state],
|
583 |
outputs=[code_metrics]
|
584 |
).then(
|
585 |
-
lambda state:
|
586 |
inputs=[code_state],
|
587 |
outputs=[code_recommendations]
|
588 |
)
|
@@ -626,15 +643,14 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
|
626 |
|
627 |
# Add footer with attribution
|
628 |
gr.HTML("""
|
629 |
-
<div style="text-align: center; margin-top: 20px; padding:
|
630 |
-
Created by Calvin Allen
|
631 |
-
<span style="font-style: italic; font-size: 0.8rem;">Founder of Cosmick Visions</span>
|
632 |
</div>
|
633 |
""")
|
634 |
|
635 |
-
# Launch the app
|
636 |
if __name__ == "__main__":
|
637 |
-
demo.launch()
|
638 |
|
639 |
# Add new helper functions
|
640 |
def detect_language(extension):
|
@@ -669,64 +685,20 @@ def calculate_complexity_metrics(content, language):
|
|
669 |
|
670 |
return metrics
|
671 |
|
672 |
-
def
|
673 |
-
"""
|
674 |
-
metrics = calculate_complexity_metrics(content, language)
|
675 |
-
|
676 |
-
# Add security scanning
|
677 |
-
security_issues = scan_security_issues(content, language)
|
678 |
-
|
679 |
-
# Add best practices analysis
|
680 |
-
best_practices = analyze_best_practices(content, language)
|
681 |
-
|
682 |
-
# Add performance suggestions
|
683 |
-
performance_tips = analyze_performance(content, language)
|
684 |
-
|
685 |
-
return {
|
686 |
-
**metrics,
|
687 |
-
"security_issues": security_issues,
|
688 |
-
"best_practices": best_practices,
|
689 |
-
"performance_tips": performance_tips
|
690 |
-
}
|
691 |
-
|
692 |
-
def generate_enhanced_recommendations(metrics):
|
693 |
-
"""Enhanced recommendations with more detail"""
|
694 |
recommendations = []
|
695 |
|
696 |
-
# Existing complexity checks
|
697 |
if metrics.get("cyclomatic_complexity", 0) > 10:
|
698 |
-
recommendations.append(
|
699 |
-
"type": "complexity",
|
700 |
-
"severity": "warning",
|
701 |
-
"message": "οΏ½οΏ½οΏ½ High cyclomatic complexity detected",
|
702 |
-
"details": "Consider breaking down complex functions into smaller, more manageable pieces.",
|
703 |
-
"examples": ["Extract repeated logic into helper functions", "Use early returns to reduce nesting"]
|
704 |
-
})
|
705 |
|
706 |
-
|
707 |
-
|
708 |
-
recommendations.append({
|
709 |
-
"type": "security",
|
710 |
-
"severity": issue["severity"],
|
711 |
-
"message": f"π {issue['title']}",
|
712 |
-
"details": issue["description"],
|
713 |
-
"fix": issue["recommendation"]
|
714 |
-
})
|
715 |
|
716 |
-
|
717 |
-
|
718 |
-
|
719 |
-
|
720 |
-
|
721 |
-
|
722 |
-
|
723 |
-
# This function is called but not defined
|
724 |
-
pass
|
725 |
-
|
726 |
-
def analyze_performance(content, language):
|
727 |
-
# This function is called but not defined
|
728 |
-
pass
|
729 |
-
|
730 |
-
def format_recommendations(recommendations):
|
731 |
-
# This function is called but not defined
|
732 |
-
pass
|
|
|
5 |
import uuid
|
6 |
from dotenv import load_dotenv
|
7 |
from langchain_community.vectorstores import FAISS
|
8 |
+
from langchain_community.embeddings import HuggingFaceInstructEmbeddings
|
9 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
10 |
import fitz # PyMuPDF
|
11 |
import base64
|
|
|
38 |
)
|
39 |
except Exception as e:
|
40 |
print(f"Warning: Failed to load fallback embeddings model: {e}")
|
41 |
+
embeddings = None
|
|
|
42 |
|
43 |
# Directory to store FAISS indexes
|
44 |
FAISS_INDEX_DIR = "faiss_indexes_tech"
|
|
|
48 |
# Dictionary to store user-specific vectorstores
|
49 |
user_vectorstores = {}
|
50 |
|
51 |
+
# Custom CSS for Tech theme
|
52 |
custom_css = """
|
53 |
+
:root {
|
54 |
+
--primary-color: #4285F4;
|
55 |
+
--secondary-color: #34A853;
|
56 |
+
--accent-color: #EA4335;
|
57 |
+
--light-background: #F8F9FA;
|
58 |
+
--dark-text: #202124;
|
59 |
+
--white: #FFFFFF;
|
60 |
+
--border-color: #DADCE0;
|
61 |
+
--code-bg: #F1F3F4;
|
|
|
|
|
|
|
62 |
}
|
63 |
+
body {
|
64 |
+
background-color: var(--light-background);
|
65 |
+
font-family: 'Google Sans', 'Roboto', sans-serif;
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
}
|
67 |
+
.container {
|
68 |
+
max-width: 1200px !important;
|
69 |
+
margin: 0 auto !important;
|
70 |
+
padding: 10px;
|
|
|
|
|
|
|
|
|
|
|
|
|
71 |
}
|
72 |
+
.header {
|
73 |
+
background-color: var(--white);
|
74 |
+
border-bottom: 1px solid var(--border-color);
|
75 |
+
padding: 15px 0;
|
76 |
+
margin-bottom: 20px;
|
77 |
+
border-radius: 12px 12px 0 0;
|
78 |
+
box-shadow: 0 2px 4px rgba(0,0,0,0.05);
|
79 |
}
|
80 |
+
.header-title {
|
81 |
+
color: var(--primary-color);
|
82 |
+
font-size: 1.8rem;
|
83 |
+
font-weight: 700;
|
84 |
+
text-align: center;
|
85 |
}
|
86 |
+
.header-subtitle {
|
87 |
+
color: var(--dark-text);
|
88 |
+
font-size: 1rem;
|
89 |
+
text-align: center;
|
90 |
+
margin-top: 5px;
|
91 |
+
}
|
92 |
+
.chat-container {
|
93 |
+
border-radius: 12px !important;
|
94 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
|
95 |
+
background-color: var(--white) !important;
|
96 |
+
border: 1px solid var(--border-color) !important;
|
97 |
+
min-height: 500px;
|
98 |
+
}
|
99 |
+
.tool-container {
|
100 |
+
background-color: var(--white);
|
101 |
+
border-radius: 12px;
|
102 |
+
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
|
103 |
+
padding: 15px;
|
104 |
+
margin-bottom: 20px;
|
105 |
+
}
|
106 |
+
.code-block {
|
107 |
+
background-color: var(--code-bg);
|
108 |
+
padding: 12px;
|
109 |
+
border-radius: 8px;
|
110 |
+
font-family: 'Roboto Mono', monospace;
|
111 |
+
overflow-x: auto;
|
112 |
+
margin: 10px 0;
|
113 |
+
border-left: 3px solid var(--primary-color);
|
114 |
}
|
115 |
"""
|
116 |
|
|
|
453 |
language = detect_language(file_extension)
|
454 |
|
455 |
# Calculate metrics
|
456 |
+
metrics = calculate_complexity_metrics(content, language)
|
457 |
|
458 |
# Create vectorstore if embeddings are available
|
459 |
session_id = None
|
|
|
485 |
</div>
|
486 |
""")
|
487 |
|
488 |
+
with gr.Row(elem_classes="container"):
|
489 |
+
with gr.Column(scale=1, min_width=300):
|
490 |
+
file_input = gr.File(
|
491 |
+
label="Upload Code File",
|
492 |
+
file_types=[".py", ".js", ".java", ".cpp", ".c", ".cs", ".php", ".rb", ".go", ".ts"],
|
493 |
+
type="binary"
|
494 |
+
)
|
495 |
+
upload_button = gr.Button("Analyze Code", variant="primary")
|
496 |
+
file_status = gr.Markdown("No file uploaded yet")
|
497 |
+
model_dropdown = gr.Dropdown(
|
498 |
+
choices=["llama3-70b-8192", "mixtral-8x7b-32768", "gemma-7b-it"],
|
499 |
+
value="llama3-70b-8192",
|
500 |
+
label="Select Model"
|
501 |
+
)
|
502 |
|
503 |
+
# Developer Tools Section
|
504 |
+
gr.Markdown("### Developer Tools", elem_classes="tool-title")
|
505 |
+
with gr.Group(elem_classes="tool-container"): # Replace Box with Group
|
506 |
+
with gr.Tabs():
|
507 |
+
with gr.TabItem("GitHub Search"):
|
508 |
+
repo_query = gr.Textbox(label="Search Query", placeholder="Enter keywords to search for repositories")
|
509 |
+
with gr.Row():
|
510 |
+
language = gr.Dropdown(
|
511 |
+
choices=["any", "JavaScript", "Python", "Java", "C++", "TypeScript", "Go", "Rust", "PHP", "C#"],
|
512 |
+
value="any",
|
513 |
+
label="Language"
|
514 |
+
)
|
515 |
+
min_stars = gr.Dropdown(
|
516 |
+
choices=["0", "10", "50", "100", "1000", "10000"],
|
517 |
+
value="0",
|
518 |
+
label="Min Stars"
|
519 |
+
)
|
520 |
+
sort_by = gr.Dropdown(
|
521 |
+
choices=["stars", "forks", "updated"],
|
522 |
+
value="stars",
|
523 |
+
label="Sort By"
|
524 |
+
)
|
525 |
+
repo_search_btn = gr.Button("Search Repositories")
|
526 |
+
|
527 |
+
with gr.TabItem("Stack Overflow"):
|
528 |
+
stack_query = gr.Textbox(label="Search Query", placeholder="Enter your technical question")
|
529 |
+
with gr.Row():
|
530 |
+
tag = gr.Dropdown(
|
531 |
+
choices=["any", "python", "javascript", "java", "c++", "react", "node.js", "android", "ios", "sql"],
|
532 |
+
value="any",
|
533 |
+
label="Tag"
|
534 |
+
)
|
535 |
+
so_sort_by = gr.Dropdown(
|
536 |
+
choices=["votes", "newest", "activity"],
|
537 |
+
value="votes",
|
538 |
+
label="Sort By"
|
539 |
+
)
|
540 |
+
so_search_btn = gr.Button("Search Stack Overflow")
|
541 |
+
|
542 |
+
with gr.TabItem("Code Explainer"):
|
543 |
+
code_input = gr.Textbox(
|
544 |
+
label="Code to Explain",
|
545 |
+
placeholder="Paste your code here...",
|
546 |
+
lines=10
|
547 |
+
)
|
548 |
+
explain_btn = gr.Button("Explain Code")
|
549 |
+
|
550 |
+
with gr.Column(scale=2, min_width=600):
|
551 |
+
with gr.Tabs():
|
552 |
+
with gr.TabItem("Code Analysis"):
|
553 |
+
with gr.Column(elem_classes="code-viewer-container"):
|
554 |
+
code_metrics = gr.Markdown("No code analyzed yet", elem_classes="stats-box")
|
555 |
+
code_recommendations = gr.Markdown("", elem_classes="recommendations-box")
|
556 |
+
|
557 |
+
with gr.TabItem("GitHub Results"):
|
558 |
+
repo_results = gr.Markdown("Search for repositories to see results here")
|
559 |
+
|
560 |
+
with gr.TabItem("Stack Overflow Results"):
|
561 |
+
stack_results = gr.Markdown("Search for questions to see results here")
|
562 |
+
|
563 |
+
with gr.TabItem("Code Explanation"):
|
564 |
+
code_explanation = gr.Markdown("Paste your code and click 'Explain Code' to see an explanation here")
|
565 |
+
|
566 |
+
with gr.Row(elem_classes="container"):
|
567 |
+
with gr.Column(scale=2, min_width=600):
|
568 |
chatbot = gr.Chatbot(
|
569 |
height=500,
|
570 |
show_copy_button=True,
|
|
|
579 |
)
|
580 |
send_btn = gr.Button("Send", scale=1)
|
581 |
clear_btn = gr.Button("Clear Conversation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
582 |
|
583 |
+
# Update event handlers
|
584 |
upload_button.click(
|
585 |
process_code_file,
|
586 |
inputs=[file_input],
|
|
|
599 |
inputs=[code_state],
|
600 |
outputs=[code_metrics]
|
601 |
).then(
|
602 |
+
lambda state: generate_recommendations(state),
|
603 |
inputs=[code_state],
|
604 |
outputs=[code_recommendations]
|
605 |
)
|
|
|
643 |
|
644 |
# Add footer with attribution
|
645 |
gr.HTML("""
|
646 |
+
<div style="text-align: center; margin-top: 20px; padding: 10px; color: #666; font-size: 0.8rem; border-top: 1px solid #eee;">
|
647 |
+
Created by Calvin Allen Crawford
|
|
|
648 |
</div>
|
649 |
""")
|
650 |
|
651 |
+
# Launch the app
|
652 |
if __name__ == "__main__":
|
653 |
+
demo.launch()
|
654 |
|
655 |
# Add new helper functions
|
656 |
def detect_language(extension):
|
|
|
685 |
|
686 |
return metrics
|
687 |
|
688 |
+
def generate_recommendations(metrics):
|
689 |
+
"""Generate code quality recommendations based on metrics"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
690 |
recommendations = []
|
691 |
|
|
|
692 |
if metrics.get("cyclomatic_complexity", 0) > 10:
|
693 |
+
recommendations.append("π High cyclomatic complexity detected. Consider breaking down complex functions.")
|
|
|
|
|
|
|
|
|
|
|
|
|
694 |
|
695 |
+
if metrics.get("code_lines", 0) > 300:
|
696 |
+
recommendations.append("π File is quite large. Consider splitting it into multiple modules.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
697 |
|
698 |
+
if metrics.get("functions", 0) > 10:
|
699 |
+
recommendations.append("π§ Large number of functions. Consider grouping related functions into classes.")
|
700 |
+
|
701 |
+
if metrics.get("comments", 0) / max(metrics.get("code_lines", 1), 1) < 0.1:
|
702 |
+
recommendations.append("π Low comment ratio. Consider adding more documentation.")
|
703 |
+
|
704 |
+
return "### Recommendations\n\n" + "\n\n".join(recommendations) if recommendations else ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|