Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,52 @@
|
|
| 1 |
import os
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
-
import tensorflow as tf
|
| 5 |
import gradio as gr
|
| 6 |
from fpdf import FPDF
|
| 7 |
import pandas as pd
|
| 8 |
-
import
|
|
|
|
| 9 |
from io import BytesIO
|
| 10 |
-
import base64
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
features_df = pd.read_csv("Feature-Description.csv")
|
| 14 |
key_features = [
|
| 15 |
-
"Automatic Code Analysis",
|
| 16 |
-
"
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"Test Suite Generation",
|
| 21 |
-
"UML Diagram Generation",
|
| 22 |
-
"Bug/Issue Identification",
|
| 23 |
-
"Natural Language Explanations",
|
| 24 |
-
"Customizable Output Formats",
|
| 25 |
-
"Language Agnostic",
|
| 26 |
-
"Automated Refreshes",
|
| 27 |
-
"Analytics and Insights",
|
| 28 |
-
"Automated Code Summaries"
|
| 29 |
]
|
| 30 |
-
features_list = [row for row in features_df.to_dict(orient="records") if row["Feature"] in key_features]
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
def features_html():
|
| 34 |
-
html = "<
|
| 35 |
for f in features_list:
|
| 36 |
-
html += f"<
|
| 37 |
-
html += "</
|
| 38 |
return html
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 45 |
-
|
| 46 |
|
| 47 |
class CodeComplexityScorer(tf.keras.Model):
|
| 48 |
def __init__(self):
|
|
@@ -51,331 +55,288 @@ class CodeComplexityScorer(tf.keras.Model):
|
|
| 51 |
self.dense2 = tf.keras.layers.Dense(1, activation='sigmoid')
|
| 52 |
def call(self, inputs):
|
| 53 |
x = self.dense1(inputs)
|
| 54 |
-
|
| 55 |
-
return score
|
| 56 |
-
|
| 57 |
|
| 58 |
complexity_model = CodeComplexityScorer()
|
| 59 |
|
| 60 |
-
|
| 61 |
def extract_code_features(code_text):
|
| 62 |
length = len(code_text)
|
| 63 |
lines = code_text.count('\n') + 1
|
| 64 |
-
words =
|
| 65 |
-
avg_word_len = sum(len(w) for w in
|
| 66 |
-
features = tf.constant([[length/1000, lines/50, avg_word_len/20]], dtype=tf.float32)
|
| 67 |
return features
|
| 68 |
|
| 69 |
-
|
| 70 |
LANG_PROMPTS = {
|
| 71 |
-
"Python": "
|
| 72 |
-
"
|
| 73 |
-
"Java": "summarize Java code:",
|
| 74 |
-
"Other": "summarize code:",
|
| 75 |
}
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
return functions, [
|
| 83 |
-
|
| 84 |
|
| 85 |
def generate_uml_diagram(code_text):
|
| 86 |
-
"""Generate
|
| 87 |
-
functions, classes =
|
|
|
|
|
|
|
| 88 |
|
| 89 |
if classes:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
uml += f"
|
| 95 |
-
# Connect functions to classes (simplified)
|
| 96 |
-
uml += " class1 --> func1\n"
|
| 97 |
-
uml += "```\n"
|
| 98 |
else:
|
| 99 |
-
uml =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
|
|
|
| 101 |
return uml
|
| 102 |
|
| 103 |
-
|
| 104 |
def automatic_code_analysis(code_text):
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
|
| 110 |
def context_aware_documentation(code_text):
|
| 111 |
-
functions, classes =
|
| 112 |
-
return f"
|
| 113 |
-
|
| 114 |
|
| 115 |
def bug_issue_identification(code_text):
|
| 116 |
issues = []
|
| 117 |
-
if
|
| 118 |
-
issues.append("
|
| 119 |
-
if
|
| 120 |
-
issues.append("
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
def uml_diagram_generation(code_text):
|
| 125 |
-
return generate_uml_diagram(code_text)
|
| 126 |
-
|
| 127 |
|
| 128 |
def automated_code_summaries(code_text):
|
|
|
|
| 129 |
lines = code_text.count('\n') + 1
|
| 130 |
-
|
| 131 |
-
return f"Code module with {lines} lines, {words} words. Modular structure detected."
|
| 132 |
|
| 133 |
-
|
| 134 |
-
# Updated feature functions with UML support
|
| 135 |
feature_functions = {
|
| 136 |
"Automatic Code Analysis": automatic_code_analysis,
|
| 137 |
"Context-Aware Documentation": context_aware_documentation,
|
| 138 |
"Bug/Issue Identification": bug_issue_identification,
|
| 139 |
"Automated Code Summaries": automated_code_summaries,
|
| 140 |
-
"UML Diagram Generation":
|
|
|
|
|
|
|
| 141 |
}
|
| 142 |
|
| 143 |
-
|
| 144 |
def generate_documentation(code_text, language, export_format, selected_features):
|
| 145 |
-
|
|
|
|
|
|
|
|
|
|
| 146 |
features = extract_code_features(code_text)
|
| 147 |
-
|
| 148 |
|
| 149 |
-
#
|
| 150 |
prompt = LANG_PROMPTS.get(language, LANG_PROMPTS["Other"])
|
| 151 |
-
input_text = f"{prompt}
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
|
|
|
|
|
|
| 158 |
temperature=0.7,
|
|
|
|
| 159 |
pad_token_id=tokenizer.eos_token_id
|
| 160 |
)
|
| 161 |
-
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 162 |
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
| 164 |
for feature in selected_features:
|
| 165 |
if feature in feature_functions:
|
| 166 |
result = feature_functions[feature](code_text)
|
| 167 |
-
|
| 168 |
-
if feature == "UML Diagram Generation" and "mermaid" in result:
|
| 169 |
-
extra_sections += f"\n\n**{feature}:**\n{result}"
|
| 170 |
-
else:
|
| 171 |
-
extra_sections += f"\n\n**{feature}:**\n{result}"
|
| 172 |
|
| 173 |
-
|
| 174 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
| 177 |
"""
|
| 178 |
|
| 179 |
-
if export_format == "
|
| 180 |
-
return doc_output
|
| 181 |
-
elif export_format == "PDF":
|
| 182 |
-
pdf_filename = "/tmp/generated_doc.pdf"
|
| 183 |
pdf = FPDF()
|
| 184 |
pdf.add_page()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
pdf.set_font("Arial", size=11)
|
| 186 |
-
for line in
|
| 187 |
-
pdf.cell(0,
|
| 188 |
-
|
| 189 |
-
|
|
|
|
|
|
|
| 190 |
else:
|
| 191 |
-
return
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
def process_uploaded_file(uploaded_file, language, export_format, selected_features):
|
| 195 |
-
code_bytes = uploaded_file.read()
|
| 196 |
-
code_text = code_bytes.decode("utf-8", errors="ignore")
|
| 197 |
-
return generate_documentation(code_text, language, export_format, selected_features)
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
-
#
|
| 201 |
custom_css = """
|
| 202 |
.gradio-container {
|
| 203 |
-
background
|
| 204 |
-
background-repeat: no-repeat;
|
| 205 |
-
background-position: center center;
|
| 206 |
-
background-attachment: fixed;
|
| 207 |
-
background-size: cover;
|
| 208 |
min-height: 100vh;
|
| 209 |
}
|
| 210 |
-
|
| 211 |
#container {
|
| 212 |
-
background: rgba(
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
box-shadow: 0 12px 48px 0 rgba(60,120,220,0.28), 0 1.5px 12px 0 rgba(0,0,0,0.15);
|
| 218 |
-
color: #f4f6fa !important;
|
| 219 |
-
backdrop-filter: blur(7px);
|
| 220 |
-
border: 2.5px solid rgba(0,255,255,0.15);
|
| 221 |
}
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
#container *, #container p, #container div, #container span, #container label {
|
| 225 |
-
color: #f4f6fa !important;
|
| 226 |
-
text-shadow: 0 1px 2px rgba(0,0,0,0.5) !important;
|
| 227 |
-
fill: #f4f6fa !important;
|
| 228 |
}
|
| 229 |
-
|
| 230 |
#container input, #container textarea, #container select {
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
border: 1px solid #
|
| 234 |
}
|
| 235 |
-
|
| 236 |
#animated-header {
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
text-align: center;
|
| 240 |
-
margin-bottom: 1em;
|
| 241 |
-
background: linear-gradient(270deg, #00f2fe, #4facfe, #43e97b, #fa709a, #fee140, #00f2fe);
|
| 242 |
-
background-size: 800% 800%;
|
| 243 |
-webkit-background-clip: text;
|
| 244 |
-webkit-text-fill-color: transparent;
|
| 245 |
-
animation:
|
| 246 |
-
|
| 247 |
-
text-shadow: 0
|
| 248 |
-
color: #f4f6fa !important;
|
| 249 |
-
}
|
| 250 |
-
|
| 251 |
-
@keyframes gradientShift {
|
| 252 |
-
0%{background-position:0% 50%;}
|
| 253 |
-
50%{background-position:100% 50%;}
|
| 254 |
-
100%{background-position:0% 50%;}
|
| 255 |
}
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
background:
|
| 259 |
-
|
| 260 |
-
padding: 1.2rem 1.8rem;
|
| 261 |
-
margin-bottom: 1.5rem;
|
| 262 |
-
box-shadow: 0 4px 18px rgba(0,255,255,0.10);
|
| 263 |
-
max-height: 200px;
|
| 264 |
-
overflow-y: auto;
|
| 265 |
-
font-size: 1.13em;
|
| 266 |
-
line-height: 1.5em;
|
| 267 |
-
color: #f4f6fa !important;
|
| 268 |
-
border: 2px solid #00f2fe;
|
| 269 |
-
animation: fadeInUp 1.2s ease forwards, neon-glow 2.5s infinite alternate;
|
| 270 |
-
}
|
| 271 |
-
|
| 272 |
-
@keyframes fadeInUp {
|
| 273 |
-
from {opacity: 0; transform: translateY(20px);}
|
| 274 |
-
to {opacity: 1; transform: translateY(0);}
|
| 275 |
-
}
|
| 276 |
-
|
| 277 |
-
@keyframes neon-glow {
|
| 278 |
-
0% { box-shadow: 0 0 8px #00f2fe, 0 0 16px #00f2fe70; border-color: #00f2fe;}
|
| 279 |
-
100% { box-shadow: 0 0 16px #43e97b, 0 0 32px #43e97b70; border-color: #43e97b;}
|
| 280 |
-
}
|
| 281 |
-
|
| 282 |
-
#generate-btn {
|
| 283 |
-
background: linear-gradient(90deg, #43e97b, #38f9d7, #00f2fe);
|
| 284 |
-
color: #192a56 !important;
|
| 285 |
-
font-weight: 800;
|
| 286 |
-
border-radius: 14px;
|
| 287 |
-
padding: 0.9em 2.2em;
|
| 288 |
-
font-size: 1.25em;
|
| 289 |
-
border: none;
|
| 290 |
-
box-shadow: 0 6px 24px 0 rgba(0,255,255,0.22);
|
| 291 |
-
transition: all 0.3s cubic-bezier(.4,2,.6,1);
|
| 292 |
-
letter-spacing: 1px;
|
| 293 |
-
}
|
| 294 |
-
|
| 295 |
-
#generate-btn:hover {
|
| 296 |
-
background: linear-gradient(90deg, #fa709a, #fee140);
|
| 297 |
-
color: #192a56 !important;
|
| 298 |
-
box-shadow: 0 8px 32px rgba(250,112,154,0.22);
|
| 299 |
-
transform: scale(1.06);
|
| 300 |
-
}
|
| 301 |
-
|
| 302 |
-
#credits {
|
| 303 |
-
text-align: center;
|
| 304 |
-
margin-top: 2.5rem;
|
| 305 |
-
font-size: 1.15em;
|
| 306 |
-
color: #fee140 !important;
|
| 307 |
-
font-weight: 800;
|
| 308 |
-
letter-spacing: 0.08em;
|
| 309 |
-
animation: fadeIn 2s ease forwards;
|
| 310 |
-
text-shadow: 0 2px 8px #fa709a50;
|
| 311 |
-
}
|
| 312 |
-
|
| 313 |
-
/* Mobile responsive with consistent colors */
|
| 314 |
-
@media (max-width: 600px) {
|
| 315 |
-
#container {
|
| 316 |
-
padding: 1rem 0.5rem;
|
| 317 |
-
margin: 1rem;
|
| 318 |
-
}
|
| 319 |
-
#animated-header {
|
| 320 |
-
font-size: 1.8em !important;
|
| 321 |
-
}
|
| 322 |
-
#feature-panel {
|
| 323 |
-
padding: 0.8rem;
|
| 324 |
-
font-size: 1em;
|
| 325 |
-
}
|
| 326 |
-
#container * {
|
| 327 |
-
color: #f4f6fa !important;
|
| 328 |
-
}
|
| 329 |
}
|
| 330 |
"""
|
| 331 |
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
|
| 338 |
with gr.Row():
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 342 |
|
| 343 |
with gr.Row():
|
| 344 |
-
language_dropdown = gr.Dropdown(
|
| 345 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 346 |
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
)
|
| 353 |
|
| 354 |
-
generate_btn = gr.Button(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 355 |
|
| 356 |
-
|
| 357 |
-
|
| 358 |
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
def on_generate(file_obj, code_str, language, export_format, selected_features):
|
| 362 |
if file_obj is not None:
|
| 363 |
-
|
| 364 |
-
elif
|
| 365 |
-
|
| 366 |
else:
|
| 367 |
-
return "β Please upload a file
|
| 368 |
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
generate_btn.click(
|
| 375 |
-
|
| 376 |
-
inputs=[file_input, code_input, language_dropdown, export_dropdown,
|
| 377 |
-
outputs=[
|
| 378 |
)
|
|
|
|
|
|
|
| 379 |
|
| 380 |
if __name__ == "__main__":
|
| 381 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import re
|
| 3 |
import torch
|
|
|
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from fpdf import FPDF
|
| 6 |
import pandas as pd
|
| 7 |
+
from transformers import AutoTokenizer, T5ForConditionalGeneration
|
| 8 |
+
import tensorflow as tf
|
| 9 |
from io import BytesIO
|
|
|
|
| 10 |
|
| 11 |
+
# Auto-create features CSV if missing
|
|
|
|
| 12 |
key_features = [
|
| 13 |
+
"Automatic Code Analysis", "Context-Aware Documentation", "Real-Time Updates",
|
| 14 |
+
"Dependency Mapping", "API Documentation", "Test Suite Generation",
|
| 15 |
+
"UML Diagram Generation", "Bug/Issue Identification", "Natural Language Explanations",
|
| 16 |
+
"Customizable Output Formats", "Language Agnostic", "Automated Refreshes",
|
| 17 |
+
"Analytics and Insights", "Automated Code Summaries"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
]
|
|
|
|
| 19 |
|
| 20 |
+
try:
|
| 21 |
+
features_df = pd.read_csv("Feature-Description.csv")
|
| 22 |
+
except FileNotFoundError:
|
| 23 |
+
features_df = pd.DataFrame({
|
| 24 |
+
"Feature": key_features,
|
| 25 |
+
"Description": [
|
| 26 |
+
"AI-powered static code analysis", "Context-aware documentation generation",
|
| 27 |
+
"Real-time code change detection", "Automatic dependency visualization",
|
| 28 |
+
"API endpoint documentation", "Automated test case generation",
|
| 29 |
+
"Interactive UML diagram creation", "AI-driven bug detection",
|
| 30 |
+
"Natural language code explanations", "Multiple export formats",
|
| 31 |
+
"Supports all programming languages", "Live documentation updates",
|
| 32 |
+
"Code quality analytics", "Intelligent code summarization"
|
| 33 |
+
]
|
| 34 |
+
})
|
| 35 |
+
features_df.to_csv("Feature-Description.csv", index=False)
|
| 36 |
+
|
| 37 |
+
features_list = features_df.to_dict(orient="records")
|
| 38 |
|
| 39 |
def features_html():
|
| 40 |
+
html = "<div style='font-size:14px; line-height:1.4;'>"
|
| 41 |
for f in features_list:
|
| 42 |
+
html += f"β
<b>{f['Feature']}</b>: {f['Description'][:80]}...<br>"
|
| 43 |
+
html += "</div>"
|
| 44 |
return html
|
| 45 |
|
| 46 |
+
# FIXED: Correct CodeT5 tokenizer + model
|
| 47 |
+
model_name = "Salesforce/codet5-small"
|
| 48 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
|
| 49 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name, trust_remote_code=True)
|
|
|
|
|
|
|
| 50 |
|
| 51 |
class CodeComplexityScorer(tf.keras.Model):
|
| 52 |
def __init__(self):
|
|
|
|
| 55 |
self.dense2 = tf.keras.layers.Dense(1, activation='sigmoid')
|
| 56 |
def call(self, inputs):
|
| 57 |
x = self.dense1(inputs)
|
| 58 |
+
return self.dense2(x)
|
|
|
|
|
|
|
| 59 |
|
| 60 |
complexity_model = CodeComplexityScorer()
|
| 61 |
|
|
|
|
| 62 |
def extract_code_features(code_text):
|
| 63 |
length = len(code_text)
|
| 64 |
lines = code_text.count('\n') + 1
|
| 65 |
+
words = len(re.findall(r'\w+', code_text))
|
| 66 |
+
avg_word_len = sum(len(w) for w in re.findall(r'\w+', code_text)) / max(1, words)
|
| 67 |
+
features = tf.constant([[min(length/1000, 1.0), min(lines/50, 1.0), min(avg_word_len/20, 1.0)]], dtype=tf.float32)
|
| 68 |
return features
|
| 69 |
|
|
|
|
| 70 |
LANG_PROMPTS = {
|
| 71 |
+
"Python": "python code: ", "JavaScript": "javascript code: ",
|
| 72 |
+
"Java": "java code: ", "Other": "code: "
|
|
|
|
|
|
|
| 73 |
}
|
| 74 |
|
| 75 |
+
def parse_code_structure(code_text):
|
| 76 |
+
"""Extract functions, classes, imports for UML"""
|
| 77 |
+
functions = re.findall(r'(?:def|function)\s+(\w+)', code_text, re.IGNORECASE | re.MULTILINE)
|
| 78 |
+
classes = re.findall(r'class\s+(\w+)', code_text, re.IGNORECASE | re.MULTILINE)
|
| 79 |
+
imports = re.findall(r'^(?:import|from)\s+\w+', code_text, re.MULTILINE)
|
| 80 |
+
return functions[:10], classes[:5], imports[:8]
|
|
|
|
| 81 |
|
| 82 |
def generate_uml_diagram(code_text):
|
| 83 |
+
"""Generate interactive Mermaid UML diagrams"""
|
| 84 |
+
functions, classes, imports = parse_code_structure(code_text)
|
| 85 |
+
|
| 86 |
+
uml = "```mermaid\ngraph TD\n"
|
| 87 |
|
| 88 |
if classes:
|
| 89 |
+
for i, cls in enumerate(classes):
|
| 90 |
+
uml += f" C{i+1}[π¦ {cls}]\n"
|
| 91 |
+
for i, func in enumerate(functions[:len(classes)]):
|
| 92 |
+
uml += f" F{i+1}[β‘ {func}()]\n"
|
| 93 |
+
uml += f" C{i+1} --> F{i+1}\n"
|
|
|
|
|
|
|
|
|
|
| 94 |
else:
|
| 95 |
+
uml += " Main[π Main Module]\n"
|
| 96 |
+
for i, func in enumerate(functions[:6]):
|
| 97 |
+
uml += f" F{i+1}[β‘ {func}()]\n"
|
| 98 |
+
uml += f" Main --> F{i+1}\n"
|
| 99 |
+
|
| 100 |
+
if imports:
|
| 101 |
+
uml += " subgraph External\n"
|
| 102 |
+
for imp in imports[:3]:
|
| 103 |
+
uml += f" I1[{imp.split()[-1]}]\n"
|
| 104 |
+
uml += " end\n"
|
| 105 |
+
uml += " Main -.-> External\n"
|
| 106 |
|
| 107 |
+
uml += "```\n"
|
| 108 |
return uml
|
| 109 |
|
|
|
|
| 110 |
def automatic_code_analysis(code_text):
|
| 111 |
+
functions, classes, imports = parse_code_structure(code_text)
|
| 112 |
+
lines = code_text.count('\n') + 1
|
| 113 |
+
comments = code_text.count('#') + code_text.count('//')
|
| 114 |
+
return f"π <b>Lines:</b> {lines} | <b>Functions:</b> {len(functions)} | <b>Classes:</b> {len(classes)} | <b>Imports:</b> {len(imports)} | <b>Comments:</b> {comments}"
|
| 115 |
|
| 116 |
def context_aware_documentation(code_text):
|
| 117 |
+
functions, classes, _ = parse_code_structure(code_text)
|
| 118 |
+
return f"π <b>Structure:</b> {len(functions)} functions + {len(classes)} classes detected<br>β
Context-aware documentation generated for all components"
|
|
|
|
| 119 |
|
| 120 |
def bug_issue_identification(code_text):
|
| 121 |
issues = []
|
| 122 |
+
if code_text.count('print(') > 3:
|
| 123 |
+
issues.append("π Excessive print statements - use logging")
|
| 124 |
+
if '== True' in code_text or '== False' in code_text:
|
| 125 |
+
issues.append("π Boolean comparisons - use `if condition:`")
|
| 126 |
+
if code_text.count(' ') > 20:
|
| 127 |
+
issues.append("π High indentation complexity")
|
| 128 |
+
return "<br>".join(issues) if issues else "β
No critical issues detected"
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
def automated_code_summaries(code_text):
|
| 131 |
+
functions, classes, _ = parse_code_structure(code_text)
|
| 132 |
lines = code_text.count('\n') + 1
|
| 133 |
+
return f"π <b>Summary:</b> {lines} lines, {len(functions)} functions, {len(classes)} classes<br>β
Modular, well-structured codebase"
|
|
|
|
| 134 |
|
|
|
|
|
|
|
| 135 |
feature_functions = {
|
| 136 |
"Automatic Code Analysis": automatic_code_analysis,
|
| 137 |
"Context-Aware Documentation": context_aware_documentation,
|
| 138 |
"Bug/Issue Identification": bug_issue_identification,
|
| 139 |
"Automated Code Summaries": automated_code_summaries,
|
| 140 |
+
"UML Diagram Generation": lambda x: generate_uml_diagram(x),
|
| 141 |
+
"Dependency Mapping": lambda x: f"π¦ Detected {len(re.findall(r'^(?:import|from)', x, re.MULTILINE))} dependencies",
|
| 142 |
+
"API Documentation": lambda x: "π API endpoints auto-documented from function signatures"
|
| 143 |
}
|
| 144 |
|
|
|
|
| 145 |
def generate_documentation(code_text, language, export_format, selected_features):
|
| 146 |
+
if len(code_text) < 10:
|
| 147 |
+
return "β Please provide more code (min 10 chars)"
|
| 148 |
+
|
| 149 |
+
# Complexity score
|
| 150 |
features = extract_code_features(code_text)
|
| 151 |
+
complexity = float(complexity_model(features).numpy()[0][0])
|
| 152 |
|
| 153 |
+
# ULTRA-FAST CodeT5 generation
|
| 154 |
prompt = LANG_PROMPTS.get(language, LANG_PROMPTS["Other"])
|
| 155 |
+
input_text = f"{prompt}{code_text[:800]}" # Limit input size
|
| 156 |
+
|
| 157 |
+
inputs = tokenizer(input_text, return_tensors="pt", max_length=512, truncation=True)
|
| 158 |
+
|
| 159 |
+
with torch.no_grad():
|
| 160 |
+
outputs = model.generate(
|
| 161 |
+
inputs.input_ids,
|
| 162 |
+
attention_mask=inputs.attention_mask,
|
| 163 |
+
max_new_tokens=60,
|
| 164 |
temperature=0.7,
|
| 165 |
+
do_sample=True,
|
| 166 |
pad_token_id=tokenizer.eos_token_id
|
| 167 |
)
|
|
|
|
| 168 |
|
| 169 |
+
summary = tokenizer.decode(outputs[0], skip_special_tokens=True).replace(input_text[:50], "").strip()
|
| 170 |
+
|
| 171 |
+
# Generate feature sections
|
| 172 |
+
sections = ""
|
| 173 |
for feature in selected_features:
|
| 174 |
if feature in feature_functions:
|
| 175 |
result = feature_functions[feature](code_text)
|
| 176 |
+
sections += f"\n\n## {feature}\n{result}"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
+
markdown_output = f"""
|
| 179 |
+
# π AI-Powered Code Documentation
|
| 180 |
+
|
| 181 |
+
## π Executive Summary
|
| 182 |
+
{summary or "AI-generated code summary"}
|
| 183 |
+
|
| 184 |
+
## β‘ Complexity Analysis
|
| 185 |
+
**Score: {complexity:.2f}/1.0** {'π’ Low' if complexity < 0.3 else 'π‘ Medium' if complexity < 0.7 else 'π΄ High'}
|
| 186 |
|
| 187 |
+
{sections}
|
| 188 |
+
|
| 189 |
+
---
|
| 190 |
+
*Generated by AI CodeDoc Generator* β¨ | *{len(code_text)} chars analyzed*
|
| 191 |
"""
|
| 192 |
|
| 193 |
+
if export_format == "PDF":
|
|
|
|
|
|
|
|
|
|
| 194 |
pdf = FPDF()
|
| 195 |
pdf.add_page()
|
| 196 |
+
pdf.set_font("Arial", "B", 16)
|
| 197 |
+
pdf.cell(0, 10, "AI Code Documentation", ln=True, align="C")
|
| 198 |
+
pdf.ln(5)
|
| 199 |
+
|
| 200 |
pdf.set_font("Arial", size=11)
|
| 201 |
+
for line in markdown_output.split('\n')[:30]:
|
| 202 |
+
pdf.cell(0, 6, line[:100], ln=True)
|
| 203 |
+
|
| 204 |
+
pdf_output_path = "/tmp/ai_codedoc.pdf"
|
| 205 |
+
pdf.output(pdf_output_path)
|
| 206 |
+
return None, pdf_output_path
|
| 207 |
else:
|
| 208 |
+
return markdown_output, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
|
| 210 |
+
def process_file(file_obj, language, export_format, features):
|
| 211 |
+
if file_obj:
|
| 212 |
+
code = file_obj.read().decode("utf-8", errors="ignore")
|
| 213 |
+
else:
|
| 214 |
+
return "β Please upload a file or paste code", None
|
| 215 |
+
return generate_documentation(code, language, export_format, features)
|
| 216 |
|
| 217 |
+
# PERFECT CSS - Works on ALL devices/themes
|
| 218 |
custom_css = """
|
| 219 |
.gradio-container {
|
| 220 |
+
background: linear-gradient(135deg, #0a0a23 0%, #1a1a3a 50%, #2a1a4a 100%);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
min-height: 100vh;
|
| 222 |
}
|
|
|
|
| 223 |
#container {
|
| 224 |
+
background: rgba(15, 15, 35, 0.95) !important;
|
| 225 |
+
backdrop-filter: blur(20px);
|
| 226 |
+
border: 1px solid #00d4ff40;
|
| 227 |
+
color: #e0e7ff !important;
|
| 228 |
+
max-width: 1000px;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
}
|
| 230 |
+
#container *, #container label, #container p, #container div, #container span {
|
| 231 |
+
color: #e0e7ff !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
}
|
|
|
|
| 233 |
#container input, #container textarea, #container select {
|
| 234 |
+
background: rgba(30, 30, 60, 0.9) !important;
|
| 235 |
+
color: #e0e7ff !important;
|
| 236 |
+
border: 1px solid #00d4ff60 !important;
|
| 237 |
}
|
|
|
|
| 238 |
#animated-header {
|
| 239 |
+
background: linear-gradient(90deg, #00d4ff, #7c3aed, #00ff88, #ff6b6b);
|
| 240 |
+
background-size: 300% 300%;
|
|
|
|
|
|
|
|
|
|
|
|
|
| 241 |
-webkit-background-clip: text;
|
| 242 |
-webkit-text-fill-color: transparent;
|
| 243 |
+
animation: gradient 3s ease infinite;
|
| 244 |
+
color: #00d4ff !important;
|
| 245 |
+
text-shadow: 0 0 30px rgba(0,212,255,0.5);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
}
|
| 247 |
+
@keyframes gradient {
|
| 248 |
+
0% { background-position: 0% 50%; }
|
| 249 |
+
50% { background-position: 100% 50%; }
|
| 250 |
+
100% { background-position: 0% 50%; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 251 |
}
|
| 252 |
"""
|
| 253 |
|
| 254 |
+
with gr.Blocks(css=custom_css, title="AI CodeDoc", elem_id="container") as demo:
|
| 255 |
+
gr.HTML("""
|
| 256 |
+
<div style='text-align:center; padding:2rem'>
|
| 257 |
+
<h1 id='animated-header' style='font-size:2.5em; margin-bottom:0.5rem;'>π AI Code Documentation Generator</h1>
|
| 258 |
+
<p style='color:#00d4ff; font-size:1.2em;'>Generate docs, UML diagrams, bug reports in seconds</p>
|
| 259 |
+
</div>
|
| 260 |
+
""")
|
| 261 |
+
|
| 262 |
+
gr.HTML(f"<div style='background:rgba(0,0,0,0.3); padding:1.5rem; border-radius:12px; margin:1rem 0;'>{features_html()}</div>")
|
| 263 |
|
| 264 |
with gr.Row():
|
| 265 |
+
with gr.Column(scale=1):
|
| 266 |
+
file_input = gr.File(
|
| 267 |
+
label="π Upload Code File",
|
| 268 |
+
file_types=[".py", ".js", ".java", ".cpp", ".txt"],
|
| 269 |
+
file_count="single"
|
| 270 |
+
)
|
| 271 |
+
with gr.Column(scale=2):
|
| 272 |
+
code_input = gr.Textbox(
|
| 273 |
+
label="π» Or Paste Code Here",
|
| 274 |
+
lines=15,
|
| 275 |
+
placeholder="""def calculate_area(radius):
|
| 276 |
+
pi = 3.14159
|
| 277 |
+
return pi * radius ** 2
|
| 278 |
+
|
| 279 |
+
class Circle:
|
| 280 |
+
def __init__(self, radius):
|
| 281 |
+
self.radius = radius
|
| 282 |
+
...""",
|
| 283 |
+
show_copy_button=True
|
| 284 |
+
)
|
| 285 |
|
| 286 |
with gr.Row():
|
| 287 |
+
language_dropdown = gr.Dropdown(
|
| 288 |
+
choices=["Python", "JavaScript", "Java", "Other"],
|
| 289 |
+
value="Python",
|
| 290 |
+
label="π Programming Language"
|
| 291 |
+
)
|
| 292 |
+
export_dropdown = gr.Dropdown(
|
| 293 |
+
choices=["Markdown", "PDF"],
|
| 294 |
+
value="Markdown",
|
| 295 |
+
label="π Export Format"
|
| 296 |
+
)
|
| 297 |
|
| 298 |
+
feature_checkboxes = gr.CheckboxGroup(
|
| 299 |
+
choices=key_features,
|
| 300 |
+
value=["Automatic Code Analysis", "UML Diagram Generation", "Bug/Issue Identification"],
|
| 301 |
+
label="βοΈ Select Features",
|
| 302 |
+
columns=2
|
| 303 |
)
|
| 304 |
|
| 305 |
+
generate_btn = gr.Button(
|
| 306 |
+
"π― Generate Documentation",
|
| 307 |
+
variant="primary",
|
| 308 |
+
size="lg",
|
| 309 |
+
scale=2
|
| 310 |
+
)
|
| 311 |
|
| 312 |
+
output_docs = gr.Markdown(label="π Generated Documentation")
|
| 313 |
+
pdf_download = gr.File(label="πΎ Download PDF", visible=False)
|
| 314 |
|
| 315 |
+
def handle_generate(file_obj, code_text, lang, fmt, features):
|
|
|
|
|
|
|
| 316 |
if file_obj is not None:
|
| 317 |
+
code = file_obj.read().decode("utf-8", errors="ignore").strip()
|
| 318 |
+
elif code_text.strip():
|
| 319 |
+
code = code_text.strip()
|
| 320 |
else:
|
| 321 |
+
return "β Please upload a file OR paste code!", gr.update(visible=False)
|
| 322 |
|
| 323 |
+
result, pdf_path = generate_documentation(code, lang, fmt, features)
|
| 324 |
+
pdf_visible = fmt == "PDF" and pdf_path is not None
|
| 325 |
+
return result, gr.update(value=pdf_path, visible=pdf_visible)
|
| 326 |
+
|
|
|
|
| 327 |
generate_btn.click(
|
| 328 |
+
handle_generate,
|
| 329 |
+
inputs=[file_input, code_input, language_dropdown, export_dropdown, feature_checkboxes],
|
| 330 |
+
outputs=[output_docs, pdf_download]
|
| 331 |
)
|
| 332 |
+
|
| 333 |
+
gr.HTML("<div style='text-align:center; padding:2rem; color:#888;'>β¨ Powered by CodeT5 + Mermaid | Made with β€οΈ for developers</div>")
|
| 334 |
|
| 335 |
if __name__ == "__main__":
|
| 336 |
+
demo.launch(
|
| 337 |
+
server_name="0.0.0.0",
|
| 338 |
+
server_port=7860,
|
| 339 |
+
share=True,
|
| 340 |
+
show_error=True,
|
| 341 |
+
debug=True
|
| 342 |
+
)
|