Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,7 +7,9 @@ from fpdf import FPDF
|
|
| 7 |
import pandas as pd
|
| 8 |
import re
|
| 9 |
from io import BytesIO
|
| 10 |
-
import
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Load Features from CSV (curate a subset for demo clarity)
|
| 13 |
features_df = pd.read_csv("Feature-Description.csv")
|
|
@@ -184,38 +186,113 @@ def generate_documentation(code_text, language, export_format, selected_features
|
|
| 184 |
summary_ids = mdl.generate(inputs, max_length=64, num_beams=2, early_stopping=True)
|
| 185 |
summary = tok.decode(summary_ids[0], skip_special_tokens=True)
|
| 186 |
|
| 187 |
-
extra_sections =
|
| 188 |
for feature in selected_features:
|
| 189 |
if feature in feature_functions:
|
| 190 |
if feature == "UML Diagram Generation":
|
| 191 |
-
extra_sections
|
| 192 |
else:
|
| 193 |
-
extra_sections
|
| 194 |
|
|
|
|
| 195 |
doc_output = f"""### AI-Generated Documentation
|
| 196 |
|
| 197 |
{summary}
|
| 198 |
|
| 199 |
**Code Complexity Score:** {complexity_score:.2f} (0=low, 1=high)
|
| 200 |
|
| 201 |
-
{extra_sections}
|
| 202 |
"""
|
|
|
|
|
|
|
| 203 |
|
| 204 |
if export_format == "Markdown":
|
| 205 |
-
return doc_output
|
| 206 |
elif export_format == "PDF":
|
| 207 |
pdf_filename = "/tmp/generated_doc.pdf"
|
| 208 |
pdf = FPDF()
|
| 209 |
pdf.add_page()
|
| 210 |
pdf.set_font("Arial", size=10)
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 215 |
pdf.output(pdf_filename)
|
| 216 |
-
return pdf_filename
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
else:
|
| 218 |
-
return doc_output
|
| 219 |
|
| 220 |
|
| 221 |
def process_uploaded_file(uploaded_file, language, export_format, selected_features):
|
|
@@ -356,7 +433,7 @@ with gr.Blocks(css=custom_css, elem_id="container") as demo:
|
|
| 356 |
file_input = gr.File(label="Upload Code File (.py, .js, .java)", file_types=[".py", ".js", ".java"])
|
| 357 |
code_input = gr.Textbox(label="Or Paste Code Here", lines=8, max_lines=15, placeholder="Paste your code snippet here...")
|
| 358 |
language_dropdown = gr.Dropdown(label="Select Language", choices=["Python", "JavaScript", "Java", "Other"], value="Python")
|
| 359 |
-
export_dropdown = gr.Dropdown(label="Export Format", choices=["Markdown", "PDF"], value="Markdown")
|
| 360 |
feature_options = gr.CheckboxGroup(
|
| 361 |
label="Select Features to Include",
|
| 362 |
choices=[f["Feature"] for f in features_list],
|
|
@@ -367,27 +444,34 @@ with gr.Blocks(css=custom_css, elem_id="container") as demo:
|
|
| 367 |
)
|
| 368 |
generate_btn = gr.Button("Generate Documentation", elem_id="generate-btn")
|
| 369 |
output_box = gr.Textbox(label="Generated Documentation", lines=10, max_lines=20, interactive=False, show_copy_button=True)
|
| 370 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 371 |
gr.HTML("<div id='credits'>Credits: Sreelekha Putta</div>")
|
| 372 |
|
| 373 |
|
| 374 |
def on_generate(file_obj, code_str, language, export_format, selected_features):
|
| 375 |
if file_obj is not None:
|
| 376 |
-
|
| 377 |
elif code_str.strip() != "":
|
| 378 |
-
|
| 379 |
else:
|
| 380 |
-
return "Please upload a file or paste code to generate documentation.",
|
|
|
|
| 381 |
if export_format == "PDF":
|
| 382 |
-
return None, gr.update(value=
|
|
|
|
|
|
|
| 383 |
else:
|
| 384 |
-
return
|
| 385 |
|
| 386 |
|
| 387 |
generate_btn.click(
|
| 388 |
on_generate,
|
| 389 |
inputs=[file_input, code_input, language_dropdown, export_dropdown, feature_options],
|
| 390 |
-
outputs=[output_box, pdf_output]
|
| 391 |
)
|
| 392 |
|
| 393 |
|
|
|
|
| 7 |
import pandas as pd
|
| 8 |
import re
|
| 9 |
from io import BytesIO
|
| 10 |
+
from pptx import Presentation
|
| 11 |
+
from pptx.util import Inches, Pt
|
| 12 |
+
from pptx.enum.text import PP_ALIGN
|
| 13 |
|
| 14 |
# Load Features from CSV (curate a subset for demo clarity)
|
| 15 |
features_df = pd.read_csv("Feature-Description.csv")
|
|
|
|
| 186 |
summary_ids = mdl.generate(inputs, max_length=64, num_beams=2, early_stopping=True)
|
| 187 |
summary = tok.decode(summary_ids[0], skip_special_tokens=True)
|
| 188 |
|
| 189 |
+
extra_sections = {}
|
| 190 |
for feature in selected_features:
|
| 191 |
if feature in feature_functions:
|
| 192 |
if feature == "UML Diagram Generation":
|
| 193 |
+
extra_sections[feature] = feature_functions[feature](code_text, language)
|
| 194 |
else:
|
| 195 |
+
extra_sections[feature] = feature_functions[feature](code_text)
|
| 196 |
|
| 197 |
+
# Markdown output
|
| 198 |
doc_output = f"""### AI-Generated Documentation
|
| 199 |
|
| 200 |
{summary}
|
| 201 |
|
| 202 |
**Code Complexity Score:** {complexity_score:.2f} (0=low, 1=high)
|
| 203 |
|
|
|
|
| 204 |
"""
|
| 205 |
+
for feature, content in extra_sections.items():
|
| 206 |
+
doc_output += f"\n**{feature}:**\n{content}\n"
|
| 207 |
|
| 208 |
if export_format == "Markdown":
|
| 209 |
+
return doc_output, None, None
|
| 210 |
elif export_format == "PDF":
|
| 211 |
pdf_filename = "/tmp/generated_doc.pdf"
|
| 212 |
pdf = FPDF()
|
| 213 |
pdf.add_page()
|
| 214 |
pdf.set_font("Arial", size=10)
|
| 215 |
+
|
| 216 |
+
# Title
|
| 217 |
+
pdf.set_font("Arial", 'B', 16)
|
| 218 |
+
pdf.cell(0, 10, txt="AI-Generated Documentation", ln=True, align='C')
|
| 219 |
+
pdf.ln(5)
|
| 220 |
+
|
| 221 |
+
# Summary
|
| 222 |
+
pdf.set_font("Arial", 'B', 12)
|
| 223 |
+
pdf.cell(0, 8, txt="Summary:", ln=True)
|
| 224 |
+
pdf.set_font("Arial", size=10)
|
| 225 |
+
pdf.multi_cell(0, 6, txt=summary)
|
| 226 |
+
pdf.ln(3)
|
| 227 |
+
|
| 228 |
+
# Complexity Score
|
| 229 |
+
pdf.set_font("Arial", 'B', 11)
|
| 230 |
+
pdf.cell(0, 8, txt=f"Code Complexity Score: {complexity_score:.2f}", ln=True)
|
| 231 |
+
pdf.ln(3)
|
| 232 |
+
|
| 233 |
+
# Extra sections
|
| 234 |
+
for feature, content in extra_sections.items():
|
| 235 |
+
pdf.set_font("Arial", 'B', 11)
|
| 236 |
+
pdf.cell(0, 8, txt=feature + ":", ln=True)
|
| 237 |
+
pdf.set_font("Arial", size=9)
|
| 238 |
+
safe_content = content.replace('```mermaid', '').replace('```', '').encode('latin-1', 'replace').decode('latin-1')
|
| 239 |
+
pdf.multi_cell(0, 5, txt=safe_content)
|
| 240 |
+
pdf.ln(2)
|
| 241 |
+
|
| 242 |
pdf.output(pdf_filename)
|
| 243 |
+
return None, pdf_filename, None
|
| 244 |
+
|
| 245 |
+
elif export_format == "PPT":
|
| 246 |
+
ppt_filename = "/tmp/generated_doc.pptx"
|
| 247 |
+
prs = Presentation()
|
| 248 |
+
prs.slide_width = Inches(10)
|
| 249 |
+
prs.slide_height = Inches(7.5)
|
| 250 |
+
|
| 251 |
+
# Slide 1: Title Slide
|
| 252 |
+
title_slide_layout = prs.slide_layouts[0]
|
| 253 |
+
slide = prs.slides.add_slide(title_slide_layout)
|
| 254 |
+
title = slide.shapes.title
|
| 255 |
+
subtitle = slide.placeholders[1]
|
| 256 |
+
title.text = "AI-Generated Code Documentation"
|
| 257 |
+
subtitle.text = f"Language: {language}\nComplexity Score: {complexity_score:.2f}"
|
| 258 |
+
|
| 259 |
+
# Slide 2: Summary
|
| 260 |
+
bullet_slide_layout = prs.slide_layouts[1]
|
| 261 |
+
slide = prs.slides.add_slide(bullet_slide_layout)
|
| 262 |
+
shapes = slide.shapes
|
| 263 |
+
title_shape = shapes.title
|
| 264 |
+
body_shape = shapes.placeholders[1]
|
| 265 |
+
title_shape.text = "Code Summary"
|
| 266 |
+
tf = body_shape.text_frame
|
| 267 |
+
tf.text = summary
|
| 268 |
+
|
| 269 |
+
# Slide 3+: Feature sections
|
| 270 |
+
for feature, content in extra_sections.items():
|
| 271 |
+
slide = prs.slides.add_slide(bullet_slide_layout)
|
| 272 |
+
shapes = slide.shapes
|
| 273 |
+
title_shape = shapes.title
|
| 274 |
+
body_shape = shapes.placeholders[1]
|
| 275 |
+
title_shape.text = feature
|
| 276 |
+
|
| 277 |
+
tf = body_shape.text_frame
|
| 278 |
+
clean_content = content.replace('```mermaid', '').replace('```', '')
|
| 279 |
+
|
| 280 |
+
# Split content into lines and add as bullets
|
| 281 |
+
lines = clean_content.split('\n')
|
| 282 |
+
for i, line in enumerate(lines[:10]): # Limit to 10 lines per slide
|
| 283 |
+
if i == 0:
|
| 284 |
+
tf.text = line.strip()
|
| 285 |
+
else:
|
| 286 |
+
if line.strip():
|
| 287 |
+
p = tf.add_paragraph()
|
| 288 |
+
p.text = line.strip()
|
| 289 |
+
p.level = 0
|
| 290 |
+
|
| 291 |
+
prs.save(ppt_filename)
|
| 292 |
+
return None, None, ppt_filename
|
| 293 |
+
|
| 294 |
else:
|
| 295 |
+
return doc_output, None, None
|
| 296 |
|
| 297 |
|
| 298 |
def process_uploaded_file(uploaded_file, language, export_format, selected_features):
|
|
|
|
| 433 |
file_input = gr.File(label="Upload Code File (.py, .js, .java)", file_types=[".py", ".js", ".java"])
|
| 434 |
code_input = gr.Textbox(label="Or Paste Code Here", lines=8, max_lines=15, placeholder="Paste your code snippet here...")
|
| 435 |
language_dropdown = gr.Dropdown(label="Select Language", choices=["Python", "JavaScript", "Java", "Other"], value="Python")
|
| 436 |
+
export_dropdown = gr.Dropdown(label="Export Format", choices=["Markdown", "PDF", "PPT"], value="Markdown")
|
| 437 |
feature_options = gr.CheckboxGroup(
|
| 438 |
label="Select Features to Include",
|
| 439 |
choices=[f["Feature"] for f in features_list],
|
|
|
|
| 444 |
)
|
| 445 |
generate_btn = gr.Button("Generate Documentation", elem_id="generate-btn")
|
| 446 |
output_box = gr.Textbox(label="Generated Documentation", lines=10, max_lines=20, interactive=False, show_copy_button=True)
|
| 447 |
+
|
| 448 |
+
with gr.Row():
|
| 449 |
+
pdf_output = gr.File(label="Download PDF", visible=False)
|
| 450 |
+
ppt_output = gr.File(label="Download PPT", visible=False)
|
| 451 |
+
|
| 452 |
gr.HTML("<div id='credits'>Credits: Sreelekha Putta</div>")
|
| 453 |
|
| 454 |
|
| 455 |
def on_generate(file_obj, code_str, language, export_format, selected_features):
|
| 456 |
if file_obj is not None:
|
| 457 |
+
markdown_result, pdf_result, ppt_result = process_uploaded_file(file_obj, language, export_format, selected_features)
|
| 458 |
elif code_str.strip() != "":
|
| 459 |
+
markdown_result, pdf_result, ppt_result = generate_documentation(code_str, language, export_format, selected_features)
|
| 460 |
else:
|
| 461 |
+
return "Please upload a file or paste code to generate documentation.", gr.update(visible=False), gr.update(visible=False)
|
| 462 |
+
|
| 463 |
if export_format == "PDF":
|
| 464 |
+
return None, gr.update(value=pdf_result, visible=True), gr.update(visible=False)
|
| 465 |
+
elif export_format == "PPT":
|
| 466 |
+
return None, gr.update(visible=False), gr.update(value=ppt_result, visible=True)
|
| 467 |
else:
|
| 468 |
+
return markdown_result, gr.update(visible=False), gr.update(visible=False)
|
| 469 |
|
| 470 |
|
| 471 |
generate_btn.click(
|
| 472 |
on_generate,
|
| 473 |
inputs=[file_input, code_input, language_dropdown, export_dropdown, feature_options],
|
| 474 |
+
outputs=[output_box, pdf_output, ppt_output]
|
| 475 |
)
|
| 476 |
|
| 477 |
|