Spaces:
Running
Running
Commit ·
9756402
1
Parent(s): 3b875ca
Added custom HTML interface
Browse files
app.py
CHANGED
|
@@ -1,31 +1,15 @@
|
|
| 1 |
"""
|
| 2 |
-
Goals: Off-Brand, Llama Champion, Field Notes
|
| 3 |
-
|
| 4 |
-
The plan
|
| 5 |
-
Create an LLM powered transpiler
|
| 6 |
-
Taking input code (Python, JS, etc) and translate it into Mermaid.js syntax
|
| 7 |
-
Gradio then interprets that visually.
|
| 8 |
-
|
| 9 |
-
The Pipeline:
|
| 10 |
-
1. Input. User pastes code into a gradio.Code() block.
|
| 11 |
-
2. Process. Send that code to Small Model with a specific system prompt
|
| 12 |
3. Graph. Capture the resulting mermaid string and visualize it
|
| 13 |
|
| 14 |
-
- include few shot examples in system prompt
|
| 15 |
-
- master prompting for the system prompt and test differnet ones
|
| 16 |
-
|
| 17 |
-
Future ideas include allowing user to edit the resulting flowchart and thereby edit code
|
| 18 |
-
|
| 19 |
-
Model:
|
| 20 |
-
https://huggingface.co/Qwen/Qwen2.5-Coder-7B-Instruct-GGUF
|
| 21 |
-
|
| 22 |
To do
|
| 23 |
-
-
|
| 24 |
|
| 25 |
"""
|
| 26 |
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 |
from textwrap import dedent
|
| 31 |
import re # remove thinking tag from response
|
|
@@ -44,7 +28,11 @@ llm = Llama(
|
|
| 44 |
n_threads=2
|
| 45 |
)
|
| 46 |
|
| 47 |
-
# -----
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def generate_flowchart(src_code: str):
|
| 49 |
# check if src_code is empty
|
| 50 |
if not src_code.strip(): return ""
|
|
@@ -120,7 +108,84 @@ def generate_flowchart(src_code: str):
|
|
| 120 |
cleaned = re.sub(r'<thinking>.*?</thinking>', '', content, flags=re.DOTALL)
|
| 121 |
return cleaned.strip() # and remove excess whitespace
|
| 122 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
-
|
|
|
|
| 1 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
3. Graph. Capture the resulting mermaid string and visualize it
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
To do
|
| 5 |
+
- create the custom gradio look
|
| 6 |
|
| 7 |
"""
|
| 8 |
from huggingface_hub import hf_hub_download
|
| 9 |
from llama_cpp import Llama
|
| 10 |
import gradio as gr
|
| 11 |
+
from gradio import Server
|
| 12 |
+
from fastapi.responses import HTMLResponse # serve the custom frontend from a route
|
| 13 |
from typing import Any, cast # to resolve PyLance freaking out over llama-cpp-python in the generate_flowchart function
|
| 14 |
from textwrap import dedent
|
| 15 |
import re # remove thinking tag from response
|
|
|
|
| 28 |
n_threads=2
|
| 29 |
)
|
| 30 |
|
| 31 |
+
# ----- Init App ----- #
|
| 32 |
+
app = gr.Server(title="Code-to-Flowchart Generator")
|
| 33 |
+
|
| 34 |
+
# ----- Functions ----- #
|
| 35 |
+
@app.api(name="generate_flowchart")
|
| 36 |
def generate_flowchart(src_code: str):
|
| 37 |
# check if src_code is empty
|
| 38 |
if not src_code.strip(): return ""
|
|
|
|
| 108 |
cleaned = re.sub(r'<thinking>.*?</thinking>', '', content, flags=re.DOTALL)
|
| 109 |
return cleaned.strip() # and remove excess whitespace
|
| 110 |
|
| 111 |
+
# ----- Custom Frontend ----- #
|
| 112 |
+
index_html = """
|
| 113 |
+
<!DOCTYPE html>
|
| 114 |
+
<html lang="en">
|
| 115 |
+
<head>
|
| 116 |
+
<meta charset="UTF-8">
|
| 117 |
+
<title>Code-to-Flowchart Generator</title>
|
| 118 |
+
<style>
|
| 119 |
+
body { font-family: sans-serif; background: #111827; color: #f3f4f6; margin: 0; padding: 20px; }
|
| 120 |
+
.container { display: flex; gap: 20px; height: 90vh; }
|
| 121 |
+
.panel { flex: 1; display: flex; flex-direction: column; background: #1f2937; padding: 15px; border-radius: 8px; }
|
| 122 |
+
textarea { flex: 1; background: #111827; color: #34d399; border: 1px solid #374151; padding: 10px; font-family: monospace; resize: none; border-radius: 4px; }
|
| 123 |
+
button { background: #059669; color: white; border: none; padding: 12px; margin-top: 10px; cursor: pointer; font-weight: bold; border-radius: 4px; }
|
| 124 |
+
button:hover { background: #10b981; }
|
| 125 |
+
#flowchart-target { flex: 1; background: #ffffff; padding: 10px; border-radius: 4px; overflow: auto; display: flex; justify-content: center; align-items: start; }
|
| 126 |
+
</style>
|
| 127 |
+
</head>
|
| 128 |
+
<body>
|
| 129 |
+
<h2>Flowchart Transpiler</h2>
|
| 130 |
+
<div class="container">
|
| 131 |
+
<div class="panel">
|
| 132 |
+
<h3>Source Code Input</h3>
|
| 133 |
+
<textarea id="code-input" placeholder="Paste your code here..."></textarea>
|
| 134 |
+
<button id="submit-btn">Generate Flowchart</button>
|
| 135 |
+
</div>
|
| 136 |
+
<div class="panel">
|
| 137 |
+
<h3>Mermaid Flowchart Visualizer</h3>
|
| 138 |
+
<div id="flowchart-target">
|
| 139 |
+
<pre class="mermaid" id="mermaid-string">
|
| 140 |
+
graph TD
|
| 141 |
+
A[Paste Code] --> B[Click Generate]
|
| 142 |
+
</pre>
|
| 143 |
+
</div>
|
| 144 |
+
</div>
|
| 145 |
+
</div>
|
| 146 |
+
|
| 147 |
+
<script type="module">
|
| 148 |
+
import { Client } from "https://cdn.jsdelivr.net/npm/@gradio/client@1/dist/index.min.js";
|
| 149 |
+
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@10/dist/mermaid.esm.min.mjs';
|
| 150 |
+
|
| 151 |
+
mermaid.initialize({ startOnLoad: true, theme: 'neutral' });
|
| 152 |
+
|
| 153 |
+
// Instantiate the local Gradio application client dynamically
|
| 154 |
+
const client = await Client.connect(window.location.origin);
|
| 155 |
+
|
| 156 |
+
document.getElementById('submit-btn').addEventListener('click', async () => {
|
| 157 |
+
const codeValue = document.getElementById('code-input').value;
|
| 158 |
+
const targetDiv = document.getElementById('flowchart-target');
|
| 159 |
+
|
| 160 |
+
if (!codeValue.trim()) {
|
| 161 |
+
targetDiv.innerHTML = "<p style='color:red;'>Please input code first.</p>";
|
| 162 |
+
return;
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
targetDiv.innerHTML = "Generating diagram...";
|
| 166 |
+
|
| 167 |
+
try {
|
| 168 |
+
// Call the @app.api function registered in python (name + param must match)
|
| 169 |
+
const result = await client.predict("/generate_flowchart", { src_code: codeValue });
|
| 170 |
+
const mermaidSyntax = result.data[0];
|
| 171 |
+
|
| 172 |
+
// Inject the raw string into a clean layout block and re-trigger parsing
|
| 173 |
+
targetDiv.innerHTML = `<pre class="mermaid">${mermaidSyntax}</pre>`;
|
| 174 |
+
await mermaid.run();
|
| 175 |
+
|
| 176 |
+
} catch (error) {
|
| 177 |
+
targetDiv.innerHTML = `<p style='color:red;'>Error during generation: ${error.message}</p>`;
|
| 178 |
+
}
|
| 179 |
+
});
|
| 180 |
+
</script>
|
| 181 |
+
</body>
|
| 182 |
+
</html>
|
| 183 |
+
"""
|
| 184 |
|
| 185 |
+
# Load the custom HTML
|
| 186 |
+
# / takes precedent over default Blocks UI
|
| 187 |
+
@app.get("/")
|
| 188 |
+
def index():
|
| 189 |
+
return HTMLResponse(index_html)
|
| 190 |
|
| 191 |
+
app.launch(share=True)
|