Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -114,6 +114,57 @@ body {
|
|
114 |
}
|
115 |
"""
|
116 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
117 |
# Function to process PDF files
|
118 |
def process_pdf(pdf_file):
|
119 |
if pdf_file is None:
|
@@ -659,54 +710,3 @@ gr.HTML("""
|
|
659 |
# Launch the app
|
660 |
if __name__ == "__main__":
|
661 |
demo.launch()
|
662 |
-
|
663 |
-
# Add new helper functions
|
664 |
-
def detect_language(extension):
|
665 |
-
"""Detect programming language from file extension"""
|
666 |
-
extension_map = {
|
667 |
-
".py": "Python",
|
668 |
-
".js": "JavaScript",
|
669 |
-
".java": "Java",
|
670 |
-
".cpp": "C++",
|
671 |
-
".c": "C",
|
672 |
-
".cs": "C#",
|
673 |
-
".php": "PHP",
|
674 |
-
".rb": "Ruby",
|
675 |
-
".go": "Go",
|
676 |
-
".ts": "TypeScript"
|
677 |
-
}
|
678 |
-
return extension_map.get(extension.lower(), "Unknown")
|
679 |
-
|
680 |
-
def calculate_complexity_metrics(content, language):
|
681 |
-
"""Calculate code complexity metrics"""
|
682 |
-
lines = content.split('\n')
|
683 |
-
total_lines = len(lines)
|
684 |
-
blank_lines = len([line for line in lines if not line.strip()])
|
685 |
-
code_lines = total_lines - blank_lines
|
686 |
-
|
687 |
-
metrics = {
|
688 |
-
"language": language,
|
689 |
-
"total_lines": total_lines,
|
690 |
-
"code_lines": code_lines,
|
691 |
-
"blank_lines": blank_lines
|
692 |
-
}
|
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 ""
|
|
|
114 |
}
|
115 |
"""
|
116 |
|
117 |
+
# Helper functions for code analysis
|
118 |
+
def detect_language(extension):
|
119 |
+
"""Detect programming language from file extension"""
|
120 |
+
extension_map = {
|
121 |
+
".py": "Python",
|
122 |
+
".js": "JavaScript",
|
123 |
+
".java": "Java",
|
124 |
+
".cpp": "C++",
|
125 |
+
".c": "C",
|
126 |
+
".cs": "C#",
|
127 |
+
".php": "PHP",
|
128 |
+
".rb": "Ruby",
|
129 |
+
".go": "Go",
|
130 |
+
".ts": "TypeScript"
|
131 |
+
}
|
132 |
+
return extension_map.get(extension.lower(), "Unknown")
|
133 |
+
|
134 |
+
def calculate_complexity_metrics(content, language):
|
135 |
+
"""Calculate code complexity metrics"""
|
136 |
+
lines = content.split('\n')
|
137 |
+
total_lines = len(lines)
|
138 |
+
blank_lines = len([line for line in lines if not line.strip()])
|
139 |
+
code_lines = total_lines - blank_lines
|
140 |
+
|
141 |
+
metrics = {
|
142 |
+
"language": language,
|
143 |
+
"total_lines": total_lines,
|
144 |
+
"code_lines": code_lines,
|
145 |
+
"blank_lines": blank_lines
|
146 |
+
}
|
147 |
+
|
148 |
+
return metrics
|
149 |
+
|
150 |
+
def generate_recommendations(metrics):
|
151 |
+
"""Generate code quality recommendations based on metrics"""
|
152 |
+
recommendations = []
|
153 |
+
|
154 |
+
if metrics.get("cyclomatic_complexity", 0) > 10:
|
155 |
+
recommendations.append("π High cyclomatic complexity detected. Consider breaking down complex functions.")
|
156 |
+
|
157 |
+
if metrics.get("code_lines", 0) > 300:
|
158 |
+
recommendations.append("π File is quite large. Consider splitting it into multiple modules.")
|
159 |
+
|
160 |
+
if metrics.get("functions", 0) > 10:
|
161 |
+
recommendations.append("π§ Large number of functions. Consider grouping related functions into classes.")
|
162 |
+
|
163 |
+
if metrics.get("comments", 0) / max(metrics.get("code_lines", 1), 1) < 0.1:
|
164 |
+
recommendations.append("π Low comment ratio. Consider adding more documentation.")
|
165 |
+
|
166 |
+
return "### Recommendations\n\n" + "\n\n".join(recommendations) if recommendations else ""
|
167 |
+
|
168 |
# Function to process PDF files
|
169 |
def process_pdf(pdf_file):
|
170 |
if pdf_file is None:
|
|
|
710 |
# Launch the app
|
711 |
if __name__ == "__main__":
|
712 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|