Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
|
| 2 |
#!/usr/bin/env python3
|
| 3 |
"""
|
| 4 |
Code Flow Analyzer with Gradio Interface - Hugging Face Spaces & Colab Compatible
|
|
@@ -558,7 +557,41 @@ def analyze_code_with_agent(source_code: str, language: str = "auto") -> Tuple[s
|
|
| 558 |
print(f"Error details: {traceback.format_exc()}")
|
| 559 |
return "", "", [], 0, error_msg
|
| 560 |
|
| 561 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 562 |
def create_gradio_interface():
|
| 563 |
"""Create and configure the Gradio interface"""
|
| 564 |
|
|
@@ -567,7 +600,7 @@ def create_gradio_interface():
|
|
| 567 |
if not code.strip():
|
| 568 |
return (
|
| 569 |
"Please enter some code to analyze",
|
| 570 |
-
"",
|
| 571 |
"",
|
| 572 |
"No analysis performed",
|
| 573 |
"Functions: 0 | Complexity: 0/100",
|
|
@@ -579,7 +612,7 @@ def create_gradio_interface():
|
|
| 579 |
if error:
|
| 580 |
return (
|
| 581 |
error,
|
| 582 |
-
"",
|
| 583 |
"",
|
| 584 |
"Analysis failed",
|
| 585 |
"Functions: 0 | Complexity: 0/100",
|
|
@@ -587,13 +620,14 @@ def create_gradio_interface():
|
|
| 587 |
)
|
| 588 |
|
| 589 |
# Format the outputs
|
|
|
|
| 590 |
mermaid_code_display = f"```mermaid\n{mermaid}\n```" if mermaid else "No diagram generated"
|
| 591 |
functions_display = f"**Functions Found:** {', '.join(functions)}" if functions else "No functions detected"
|
| 592 |
stats_display = f"Functions: {len(functions)} | Complexity: {complexity}/100"
|
| 593 |
|
| 594 |
return (
|
| 595 |
"✅ Analysis completed successfully!",
|
| 596 |
-
|
| 597 |
mermaid_code_display, # Markdown-formatted code for the textbox
|
| 598 |
summary,
|
| 599 |
stats_display,
|
|
@@ -684,9 +718,10 @@ def create_gradio_interface():
|
|
| 684 |
with gr.Row():
|
| 685 |
with gr.Column():
|
| 686 |
gr.Markdown("### 🎨 Generated Mermaid Diagram")
|
| 687 |
-
#
|
| 688 |
-
mermaid_output = gr.
|
| 689 |
-
label="Flowchart Diagram"
|
|
|
|
| 690 |
)
|
| 691 |
|
| 692 |
# Keep the code in a collapsible block for users who want to copy it
|
|
@@ -708,7 +743,6 @@ def create_gradio_interface():
|
|
| 708 |
)
|
| 709 |
|
| 710 |
# Connect the analyze button
|
| 711 |
-
# Updated outputs to include the new mermaid_code_output component
|
| 712 |
analyze_btn.click(
|
| 713 |
fn=analyze_code_gradio,
|
| 714 |
inputs=[code_input, language_dropdown],
|
|
@@ -759,5 +793,4 @@ def main():
|
|
| 759 |
)
|
| 760 |
|
| 761 |
# Auto-run if in Colab or when script is executed directly
|
| 762 |
-
if __name__ == "__main__
|
| 763 |
-
main()
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Code Flow Analyzer with Gradio Interface - Hugging Face Spaces & Colab Compatible
|
|
|
|
| 557 |
print(f"Error details: {traceback.format_exc()}")
|
| 558 |
return "", "", [], 0, error_msg
|
| 559 |
|
| 560 |
+
def create_mermaid_html(mermaid_code: str) -> str:
|
| 561 |
+
"""Create HTML to render Mermaid diagram"""
|
| 562 |
+
if not mermaid_code.strip():
|
| 563 |
+
return "<div style='text-align: center; padding: 20px; color: #666;'>No diagram to display</div>"
|
| 564 |
+
|
| 565 |
+
# Escape the mermaid code for HTML
|
| 566 |
+
escaped_code = mermaid_code.replace('`', '\\`').replace('${', '\\${')
|
| 567 |
+
|
| 568 |
+
html = f"""
|
| 569 |
+
<div id="mermaid-container" style="text-align: center; padding: 20px; min-height: 400px;">
|
| 570 |
+
<div class="mermaid">
|
| 571 |
+
{mermaid_code}
|
| 572 |
+
</div>
|
| 573 |
+
</div>
|
| 574 |
+
|
| 575 |
+
<script src="https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.6.1/mermaid.min.js"></script>
|
| 576 |
+
<script>
|
| 577 |
+
mermaid.initialize({{
|
| 578 |
+
startOnLoad: true,
|
| 579 |
+
theme: 'default',
|
| 580 |
+
flowchart: {{
|
| 581 |
+
useMaxWidth: true,
|
| 582 |
+
htmlLabels: true
|
| 583 |
+
}}
|
| 584 |
+
}});
|
| 585 |
+
|
| 586 |
+
// Re-render mermaid diagrams
|
| 587 |
+
setTimeout(() => {{
|
| 588 |
+
mermaid.init(undefined, document.querySelectorAll('.mermaid'));
|
| 589 |
+
}}, 100);
|
| 590 |
+
</script>
|
| 591 |
+
"""
|
| 592 |
+
return html
|
| 593 |
+
|
| 594 |
+
# --- Gradio Interface Setup (FIXED) ---
|
| 595 |
def create_gradio_interface():
|
| 596 |
"""Create and configure the Gradio interface"""
|
| 597 |
|
|
|
|
| 600 |
if not code.strip():
|
| 601 |
return (
|
| 602 |
"Please enter some code to analyze",
|
| 603 |
+
"<div style='text-align: center; padding: 20px; color: #666;'>No code to analyze</div>",
|
| 604 |
"",
|
| 605 |
"No analysis performed",
|
| 606 |
"Functions: 0 | Complexity: 0/100",
|
|
|
|
| 612 |
if error:
|
| 613 |
return (
|
| 614 |
error,
|
| 615 |
+
"<div style='text-align: center; padding: 20px; color: #c62828;'>Analysis failed</div>",
|
| 616 |
"",
|
| 617 |
"Analysis failed",
|
| 618 |
"Functions: 0 | Complexity: 0/100",
|
|
|
|
| 620 |
)
|
| 621 |
|
| 622 |
# Format the outputs
|
| 623 |
+
mermaid_html = create_mermaid_html(mermaid)
|
| 624 |
mermaid_code_display = f"```mermaid\n{mermaid}\n```" if mermaid else "No diagram generated"
|
| 625 |
functions_display = f"**Functions Found:** {', '.join(functions)}" if functions else "No functions detected"
|
| 626 |
stats_display = f"Functions: {len(functions)} | Complexity: {complexity}/100"
|
| 627 |
|
| 628 |
return (
|
| 629 |
"✅ Analysis completed successfully!",
|
| 630 |
+
mermaid_html, # HTML for rendering the Mermaid diagram
|
| 631 |
mermaid_code_display, # Markdown-formatted code for the textbox
|
| 632 |
summary,
|
| 633 |
stats_display,
|
|
|
|
| 718 |
with gr.Row():
|
| 719 |
with gr.Column():
|
| 720 |
gr.Markdown("### 🎨 Generated Mermaid Diagram")
|
| 721 |
+
# Use HTML component to render the diagram
|
| 722 |
+
mermaid_output = gr.HTML(
|
| 723 |
+
label="Flowchart Diagram",
|
| 724 |
+
value="<div style='text-align: center; padding: 20px; color: #666;'>Diagram will appear here after analysis</div>"
|
| 725 |
)
|
| 726 |
|
| 727 |
# Keep the code in a collapsible block for users who want to copy it
|
|
|
|
| 743 |
)
|
| 744 |
|
| 745 |
# Connect the analyze button
|
|
|
|
| 746 |
analyze_btn.click(
|
| 747 |
fn=analyze_code_gradio,
|
| 748 |
inputs=[code_input, language_dropdown],
|
|
|
|
| 793 |
)
|
| 794 |
|
| 795 |
# Auto-run if in Colab or when script is executed directly
|
| 796 |
+
if __name__ == "__main__
|
|
|