Spaces:
Running
Running
Commit ·
3b875ca
1
Parent(s): 2d635f4
Added system prompt, finished generate flowchart
Browse files
app.py
CHANGED
|
@@ -27,6 +27,8 @@ from huggingface_hub import hf_hub_download
|
|
| 27 |
from llama_cpp import Llama
|
| 28 |
import gradio as gr
|
| 29 |
from typing import Any, cast # to resolve PyLance freaking out over llama-cpp-python in the generate_flowchart function
|
|
|
|
|
|
|
| 30 |
|
| 31 |
# ----- Get Model ----- #
|
| 32 |
# Download Q4_K_M GGUF file from the repo
|
|
@@ -47,10 +49,59 @@ def generate_flowchart(src_code: str):
|
|
| 47 |
# check if src_code is empty
|
| 48 |
if not src_code.strip(): return ""
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
# Casting else PyLance gets mad
|
| 56 |
response = cast(Any, llm.create_chat_completion(
|
|
@@ -65,10 +116,10 @@ def generate_flowchart(src_code: str):
|
|
| 65 |
|
| 66 |
content = response["choices"][0]["message"]["content"]
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
|
|
|
| 70 |
|
| 71 |
-
return mermaid_raw
|
| 72 |
|
| 73 |
# ----- Gradio Interface (Basic, Not Custom, Archive Later) ----- #
|
| 74 |
|
|
|
|
| 27 |
from llama_cpp import Llama
|
| 28 |
import gradio as gr
|
| 29 |
from typing import Any, cast # to resolve PyLance freaking out over llama-cpp-python in the generate_flowchart function
|
| 30 |
+
from textwrap import dedent
|
| 31 |
+
import re # remove thinking tag from response
|
| 32 |
|
| 33 |
# ----- Get Model ----- #
|
| 34 |
# Download Q4_K_M GGUF file from the repo
|
|
|
|
| 49 |
# check if src_code is empty
|
| 50 |
if not src_code.strip(): return ""
|
| 51 |
|
| 52 |
+
# Set system prompt.
|
| 53 |
+
system_prompt = dedent("""
|
| 54 |
+
## Role/Persona
|
| 55 |
+
You are a senior staff software architect and compiler engineer specializing in visual control-flow mapping. Your philosophy is pure utility: you translate raw execution logic into highly accurate, scannable, structural diagrams without any conversational filler, meta-commentary, or stylistic fluff.
|
| 56 |
+
|
| 57 |
+
## Context/Objective
|
| 58 |
+
The user will provide source code files or logic snippets. Your sole objective is to parse the syntax and output a corresponding, valid Mermaid.js flowchart graph. This graph will be rendered natively in a production UI to help developers audit execution paths at a glance.
|
| 59 |
+
|
| 60 |
+
## Strict Constraints
|
| 61 |
+
<constraints>
|
| 62 |
+
1. OUTPUT FORMAT: Output ONLY valid, raw Mermaid.js syntax.
|
| 63 |
+
2. NO MARKDOWN FENCING: Do not wrap the output in ```mermaid or ``` blocks. Start directly with the Mermaid graph definition, for example: graph TD.
|
| 64 |
+
3. NO PROSE: Do not include introductory text, explanations, or concluding remarks. If the code cannot be parsed, output an isolated error node.
|
| 65 |
+
4. NODE NAMING: Keep text inside the flowchart nodes descriptive but concise, under 5 words.
|
| 66 |
+
</constraints>
|
| 67 |
+
|
| 68 |
+
<banned_vocabulary>
|
| 69 |
+
- Here is the flowchart
|
| 70 |
+
- ```mermaid
|
| 71 |
+
- ```
|
| 72 |
+
- Note:
|
| 73 |
+
- Explanation:
|
| 74 |
+
- In this diagram
|
| 75 |
+
- As requested
|
| 76 |
+
</banned_vocabulary>
|
| 77 |
+
|
| 78 |
+
## Response Workflow
|
| 79 |
+
Before outputting the final diagram syntax, perform structural parsing inside a hidden <thinking> tag according to these steps:
|
| 80 |
+
1. Identify all conditional branches, including if/else, loops, including for/while, and termination points, including return/throw.
|
| 81 |
+
2. Map out the execution flow nodes chronologically.
|
| 82 |
+
3. Verify that every opening bracket and node label matching syntax, including [ ], ( ), and { }, is perfectly balanced and closed according to Mermaid specifications.
|
| 83 |
+
4. Ensure no markdown formatting tags leak past the closing </thinking> tag.
|
| 84 |
+
|
| 85 |
+
## Few-Shot Examples
|
| 86 |
+
|
| 87 |
+
Input:
|
| 88 |
+
def check_status(val):
|
| 89 |
+
if val > 10:
|
| 90 |
+
return "Active"
|
| 91 |
+
else:
|
| 92 |
+
return "Inactive"
|
| 93 |
+
|
| 94 |
+
Output:
|
| 95 |
+
<thinking>
|
| 96 |
+
1. Control structures: One conditional check, two return branches.
|
| 97 |
+
2. Nodes: A Start, B Conditional, C Active return, D Inactive return.
|
| 98 |
+
3. Syntax verification: B uses curly braces for decisions. Edges use standard arrows.
|
| 99 |
+
</thinking>
|
| 100 |
+
graph TD
|
| 101 |
+
A[Start: check_status] --> B{val > 10}
|
| 102 |
+
B -- True --> C[Return "Active"]
|
| 103 |
+
B -- False --> D[Return "Inactive"]
|
| 104 |
+
""").strip()
|
| 105 |
|
| 106 |
# Casting else PyLance gets mad
|
| 107 |
response = cast(Any, llm.create_chat_completion(
|
|
|
|
| 116 |
|
| 117 |
content = response["choices"][0]["message"]["content"]
|
| 118 |
|
| 119 |
+
# remove the thinking tags from the response
|
| 120 |
+
cleaned = re.sub(r'<thinking>.*?</thinking>', '', content, flags=re.DOTALL)
|
| 121 |
+
return cleaned.strip() # and remove excess whitespace
|
| 122 |
|
|
|
|
| 123 |
|
| 124 |
# ----- Gradio Interface (Basic, Not Custom, Archive Later) ----- #
|
| 125 |
|