Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
Code Flow Analyzer with Gradio Interface - Hugging Face Spaces & Colab Compatible
|
|
@@ -172,8 +188,9 @@ agent_executor = None
|
|
| 172 |
|
| 173 |
if api_key:
|
| 174 |
try:
|
| 175 |
-
|
| 176 |
-
|
|
|
|
| 177 |
memory = MemorySaver()
|
| 178 |
except Exception as e:
|
| 179 |
print(f"β Could not initialize Gemini model: {e}")
|
|
@@ -556,7 +573,7 @@ def analyze_code_with_agent(source_code: str, language: str = "auto") -> Tuple[s
|
|
| 556 |
print(f"Error details: {traceback.format_exc()}")
|
| 557 |
return "", "", [], 0, error_msg
|
| 558 |
|
| 559 |
-
# --- Gradio Interface Setup (
|
| 560 |
def create_gradio_interface():
|
| 561 |
"""Create and configure the Gradio interface"""
|
| 562 |
|
|
@@ -566,6 +583,7 @@ def create_gradio_interface():
|
|
| 566 |
return (
|
| 567 |
"Please enter some code to analyze",
|
| 568 |
"",
|
|
|
|
| 569 |
"No analysis performed",
|
| 570 |
"Functions: 0 | Complexity: 0/100",
|
| 571 |
""
|
|
@@ -577,19 +595,21 @@ def create_gradio_interface():
|
|
| 577 |
return (
|
| 578 |
error,
|
| 579 |
"",
|
|
|
|
| 580 |
"Analysis failed",
|
| 581 |
"Functions: 0 | Complexity: 0/100",
|
| 582 |
""
|
| 583 |
)
|
| 584 |
|
| 585 |
# Format the outputs
|
| 586 |
-
|
| 587 |
functions_display = f"**Functions Found:** {', '.join(functions)}" if functions else "No functions detected"
|
| 588 |
stats_display = f"Functions: {len(functions)} | Complexity: {complexity}/100"
|
| 589 |
|
| 590 |
return (
|
| 591 |
"β
Analysis completed successfully!",
|
| 592 |
-
|
|
|
|
| 593 |
summary,
|
| 594 |
stats_display,
|
| 595 |
functions_display
|
|
@@ -679,26 +699,20 @@ def create_gradio_interface():
|
|
| 679 |
with gr.Row():
|
| 680 |
with gr.Column():
|
| 681 |
gr.Markdown("### π¨ Generated Mermaid Diagram")
|
| 682 |
-
|
| 683 |
-
|
| 684 |
-
|
| 685 |
-
max_lines=20,
|
| 686 |
-
interactive=True,
|
| 687 |
-
show_copy_button=True
|
| 688 |
)
|
| 689 |
|
| 690 |
-
|
| 691 |
-
|
| 692 |
-
|
| 693 |
-
|
| 694 |
-
|
| 695 |
-
|
| 696 |
-
|
| 697 |
-
|
| 698 |
-
|
| 699 |
-
- Try the examples above to see different code patterns
|
| 700 |
-
""")
|
| 701 |
-
|
| 702 |
with gr.Row():
|
| 703 |
with gr.Column():
|
| 704 |
gr.Markdown("### π Analysis Summary")
|
|
@@ -709,10 +723,11 @@ def create_gradio_interface():
|
|
| 709 |
)
|
| 710 |
|
| 711 |
# Connect the analyze button
|
|
|
|
| 712 |
analyze_btn.click(
|
| 713 |
fn=analyze_code_gradio,
|
| 714 |
inputs=[code_input, language_dropdown],
|
| 715 |
-
outputs=[status_output, mermaid_output, summary_output, stats_output, functions_output]
|
| 716 |
)
|
| 717 |
|
| 718 |
# Footer
|
|
@@ -760,4 +775,5 @@ def main():
|
|
| 760 |
|
| 761 |
# Auto-run if in Colab or when script is executed directly
|
| 762 |
if __name__ == "__main__" or IN_COLAB:
|
| 763 |
-
main()
|
|
|
|
|
|
| 1 |
+
To give you the complete, updated code that includes the visualization feature, I will first verify the most current and stable model names for Google Gemini, as the previous attempts resulted in a `404` error due to model naming changes. This will ensure the final code is functional and accurate.
|
| 2 |
+
|
| 3 |
+
Here are the queries I'll use:
|
| 4 |
+
Based on the search results, the following are the latest stable Google Gemini model names for the `v1beta` API:
|
| 5 |
+
|
| 6 |
+
* `gemini-2.5-pro`
|
| 7 |
+
* `gemini-2.5-flash`
|
| 8 |
+
* `gemini-2.5-flash-lite`
|
| 9 |
+
* `gemini-2.0-flash-001`
|
| 10 |
+
* `gemini-2.0-flash-lite-001`
|
| 11 |
+
|
| 12 |
+
The `gr.Mermaid` component is a valid Gradio component for rendering Mermaid diagrams.
|
| 13 |
+
|
| 14 |
+
Here is the complete, updated code. I have made the necessary changes to integrate the `gr.Mermaid` component and use a more current, stable model name to avoid the `404` error.
|
| 15 |
+
|
| 16 |
+
````python
|
| 17 |
#!/usr/bin/env python3
|
| 18 |
"""
|
| 19 |
Code Flow Analyzer with Gradio Interface - Hugging Face Spaces & Colab Compatible
|
|
|
|
| 188 |
|
| 189 |
if api_key:
|
| 190 |
try:
|
| 191 |
+
# Use a stable, updated model name to avoid the 404 error
|
| 192 |
+
model = ChatGoogleGenerativeAI(model="gemini-2.0-flash-001", temperature=0)
|
| 193 |
+
print("β
Gemini model initialized successfully: gemini-2.0-flash-001")
|
| 194 |
memory = MemorySaver()
|
| 195 |
except Exception as e:
|
| 196 |
print(f"β Could not initialize Gemini model: {e}")
|
|
|
|
| 573 |
print(f"Error details: {traceback.format_exc()}")
|
| 574 |
return "", "", [], 0, error_msg
|
| 575 |
|
| 576 |
+
# --- Gradio Interface Setup (UPDATED) ---
|
| 577 |
def create_gradio_interface():
|
| 578 |
"""Create and configure the Gradio interface"""
|
| 579 |
|
|
|
|
| 583 |
return (
|
| 584 |
"Please enter some code to analyze",
|
| 585 |
"",
|
| 586 |
+
"",
|
| 587 |
"No analysis performed",
|
| 588 |
"Functions: 0 | Complexity: 0/100",
|
| 589 |
""
|
|
|
|
| 595 |
return (
|
| 596 |
error,
|
| 597 |
"",
|
| 598 |
+
"",
|
| 599 |
"Analysis failed",
|
| 600 |
"Functions: 0 | Complexity: 0/100",
|
| 601 |
""
|
| 602 |
)
|
| 603 |
|
| 604 |
# Format the outputs
|
| 605 |
+
mermaid_code_display = f"```mermaid\n{mermaid}\n```" if mermaid else "No diagram generated"
|
| 606 |
functions_display = f"**Functions Found:** {', '.join(functions)}" if functions else "No functions detected"
|
| 607 |
stats_display = f"Functions: {len(functions)} | Complexity: {complexity}/100"
|
| 608 |
|
| 609 |
return (
|
| 610 |
"β
Analysis completed successfully!",
|
| 611 |
+
mermaid, # Raw Mermaid code for the gr.Mermaid component
|
| 612 |
+
mermaid_code_display, # Markdown-formatted code for the textbox
|
| 613 |
summary,
|
| 614 |
stats_display,
|
| 615 |
functions_display
|
|
|
|
| 699 |
with gr.Row():
|
| 700 |
with gr.Column():
|
| 701 |
gr.Markdown("### π¨ Generated Mermaid Diagram")
|
| 702 |
+
# Updated component to render the diagram visually
|
| 703 |
+
mermaid_output = gr.Mermaid(
|
| 704 |
+
label="Flowchart Diagram"
|
|
|
|
|
|
|
|
|
|
| 705 |
)
|
| 706 |
|
| 707 |
+
# Keep the code in a collapsible block for users who want to copy it
|
| 708 |
+
with gr.Accordion("Mermaid Code (for copying)", open=False):
|
| 709 |
+
mermaid_code_output = gr.Textbox(
|
| 710 |
+
label="Mermaid Code",
|
| 711 |
+
lines=10,
|
| 712 |
+
interactive=True,
|
| 713 |
+
show_copy_button=True
|
| 714 |
+
)
|
| 715 |
+
|
|
|
|
|
|
|
|
|
|
| 716 |
with gr.Row():
|
| 717 |
with gr.Column():
|
| 718 |
gr.Markdown("### π Analysis Summary")
|
|
|
|
| 723 |
)
|
| 724 |
|
| 725 |
# Connect the analyze button
|
| 726 |
+
# Updated outputs to include the new mermaid_code_output component
|
| 727 |
analyze_btn.click(
|
| 728 |
fn=analyze_code_gradio,
|
| 729 |
inputs=[code_input, language_dropdown],
|
| 730 |
+
outputs=[status_output, mermaid_output, mermaid_code_output, summary_output, stats_output, functions_output]
|
| 731 |
)
|
| 732 |
|
| 733 |
# Footer
|
|
|
|
| 775 |
|
| 776 |
# Auto-run if in Colab or when script is executed directly
|
| 777 |
if __name__ == "__main__" or IN_COLAB:
|
| 778 |
+
main()
|
| 779 |
+
````
|