Khurram123 commited on
Commit
95c0719
·
verified ·
1 Parent(s): c6a7884

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. .gitattributes +1 -35
  2. README.md +13 -0
  3. app.py +182 -0
  4. requirements.txt +7 -0
.gitattributes CHANGED
@@ -1,35 +1 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
1
+ *.gguf filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ΣMath — Visual Computation Engine v2.0
2
+
3
+ An interactive mathematical visualization engine powered by **Qwen-Math** and accelerated by **NVIDIA RTX 4060 Ti**.
4
+
5
+ ## Features
6
+ - **GPU Accelerated**: Fully offloaded to CUDA.
7
+ - **Dual Rendering**: Plotly for interactive 3D/Animations and Matplotlib/mpld3 for 2D.
8
+ - **Resilient Engine**: Automatic correction of colorscale mismatches and backend management.
9
+
10
+ ## Setup
11
+ 1. Place `qwen_math_q4_k_m.gguf` in the root directory.
12
+ 2. Install dependencies: `pip install -r requirements.txt`
13
+ 3. Run: `python app.py`
app.py ADDED
@@ -0,0 +1,182 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uvicorn
3
+ import re
4
+ import numpy as np
5
+ import matplotlib
6
+ matplotlib.use('Agg') # Prevents extra GUI windows from opening on your Ubuntu desktop
7
+ import matplotlib.pyplot as plt
8
+ import mpld3
9
+ import plotly.graph_objects as go
10
+ from fastapi import FastAPI, Request
11
+ from fastapi.responses import HTMLResponse
12
+ from llama_cpp import Llama
13
+
14
+ app = FastAPI()
15
+
16
+ # 1. GPU INITIALIZATION (RTX 4060 Ti)
17
+ llm = Llama(
18
+ model_path="/mnt/ai_data/math_APP/qwen_math_q4_k_m.gguf",
19
+ n_gpu_layers=-1,
20
+ n_ctx=2048,
21
+ verbose=False
22
+ )
23
+
24
+ def fix_plotly_colorscales(code):
25
+ """Intercepts Matplotlib colormap names and swaps them for Plotly equivalents."""
26
+ # Maps common Matplotlib names to their closest Plotly counterparts
27
+ color_map = {
28
+ "spring": "Viridis",
29
+ "summer": "Plasma",
30
+ "autumn": "Inferno",
31
+ "winter": "Cividis",
32
+ "magma": "Magma"
33
+ }
34
+ for mpl_name, plotly_name in color_map.items():
35
+ # Handles both single and double quotes, and case sensitivity
36
+ code = re.sub(f"['\"]{mpl_name}['\"]", f"'{plotly_name}'", code, flags=re.IGNORECASE)
37
+ return code
38
+
39
+ @app.get("/", response_class=HTMLResponse)
40
+ async def index():
41
+ return """
42
+ <!DOCTYPE html>
43
+ <html lang="en">
44
+ <head>
45
+ <meta charset="UTF-8">
46
+ <title>Interactive Math Core</title>
47
+ <script src="https://cdn.tailwindcss.com"></script>
48
+ <style>
49
+ @import url('https://fonts.googleapis.com/css2?family=Fira+Code&display=swap');
50
+ .code-box { font-family: 'Fira Code', monospace; }
51
+ iframe { border: none; width: 100%; height: 100%; border-radius: 1rem; background: white; }
52
+ </style>
53
+ </head>
54
+ <body class="bg-gray-950 text-gray-100 min-h-screen p-6">
55
+ <div class="max-w-7xl mx-auto">
56
+ <header class="mb-8 flex justify-between items-center">
57
+ <h1 class="text-3xl font-bold text-transparent bg-clip-text bg-gradient-to-r from-blue-400 to-cyan-400">Math Visualizer Core</h1>
58
+ <div class="text-xs font-mono text-gray-500 bg-gray-900 px-3 py-1 rounded-full border border-gray-800">Resilient Engine v2.0</div>
59
+ </header>
60
+
61
+ <div class="grid grid-cols-1 lg:grid-cols-12 gap-6">
62
+ <div class="lg:col-span-4 space-y-4">
63
+ <div class="bg-gray-900 p-5 rounded-2xl border border-gray-800 shadow-xl">
64
+ <label class="block text-xs font-bold text-gray-500 uppercase mb-2">Research Prompt</label>
65
+ <textarea id="promptInput" rows="5"
66
+ class="w-full bg-gray-800 border border-gray-700 rounded-xl p-4 text-sm focus:ring-2 focus:ring-blue-500 outline-none transition-all"
67
+ placeholder="e.g., Create a rotating 3D torus..."></textarea>
68
+ <button onclick="generateVisual()" id="genBtn"
69
+ class="w-full mt-4 bg-blue-600 hover:bg-blue-500 py-3 rounded-xl font-bold transition-all shadow-lg shadow-blue-900/40">
70
+ Execute on GPU
71
+ </button>
72
+ </div>
73
+ <div class="bg-gray-900 p-5 rounded-2xl border border-gray-800">
74
+ <pre id="codeDisplay" class="code-box bg-black p-4 rounded-lg overflow-x-auto text-[11px] text-green-400 border border-gray-800 min-h-[200px]"></pre>
75
+ </div>
76
+ </div>
77
+
78
+ <div class="lg:col-span-8">
79
+ <div class="bg-gray-900 rounded-2xl border border-gray-800 h-[650px] relative overflow-hidden flex items-center justify-center">
80
+ <div id="loader" class="hidden z-20 flex flex-col items-center">
81
+ <div class="animate-spin rounded-full h-12 w-12 border-t-2 border-b-2 border-blue-500 mb-4"></div>
82
+ <p class="text-blue-400">Solving Geometry...</p>
83
+ </div>
84
+ <div id="vizContainer" class="w-full h-full p-2">
85
+ <div id="placeholder" class="h-full flex items-center justify-center text-gray-600 italic">
86
+ Ready for computation.
87
+ </div>
88
+ </div>
89
+ </div>
90
+ </div>
91
+ </div>
92
+ </div>
93
+
94
+ <script>
95
+ async function generateVisual() {
96
+ const prompt = document.getElementById('promptInput').value;
97
+ const btn = document.getElementById('genBtn');
98
+ const loader = document.getElementById('loader');
99
+ const vizContainer = document.getElementById('vizContainer');
100
+ const placeholder = document.getElementById('placeholder');
101
+ const codeBox = document.getElementById('codeDisplay');
102
+
103
+ if (!prompt) return;
104
+
105
+ btn.disabled = true;
106
+ loader.classList.remove('hidden');
107
+ placeholder.classList.add('hidden');
108
+ codeBox.innerText = "# Synthesizing logic...";
109
+
110
+ try {
111
+ const response = await fetch('/process', {
112
+ method: 'POST',
113
+ headers: {'Content-Type': 'application/json'},
114
+ body: JSON.stringify({ prompt: prompt })
115
+ });
116
+ const data = await response.json();
117
+ codeBox.innerText = data.code;
118
+
119
+ if (data.html) {
120
+ vizContainer.innerHTML = `<iframe srcdoc='${data.html.replace(/'/g, "&apos;")}'></iframe>`;
121
+ } else if (data.error) {
122
+ vizContainer.innerHTML = `<div class='p-10 text-red-500 font-mono text-xs overflow-auto h-full whitespace-pre-wrap'>${data.error}</div>`;
123
+ }
124
+ } catch (err) {
125
+ codeBox.innerText = "# Execution failed.";
126
+ } finally {
127
+ loader.classList.add('hidden');
128
+ btn.disabled = false;
129
+ }
130
+ }
131
+ </script>
132
+ </body>
133
+ </html>
134
+ """
135
+
136
+ @app.post("/process")
137
+ async def process_request(request: Request):
138
+ data = await request.json()
139
+ user_prompt = data.get("prompt")
140
+
141
+ instruction = (
142
+ "Output ONLY raw Python code. Use plotly.graph_objects (go) for 3D/animations "
143
+ "and name the figure 'fig'. Use matplotlib.pyplot (plt) for 2D. "
144
+ "CRITICAL: Never use plt.show() or fig.show(). Use Plotly-safe colorscales."
145
+ )
146
+
147
+ formatted = f"<|im_start|>system\n{instruction}<|im_end|>\n<|im_start|>user\n{user_prompt}<|im_end|>\n<|im_start|>assistant\n"
148
+ output = llm(formatted, max_tokens=1536, stop=["<|im_end|>"])
149
+ code = re.sub(r"```python|```", "", output['choices'][0]['text']).strip()
150
+
151
+ # Apply the colorscale fix
152
+ code = fix_plotly_colorscales(code)
153
+
154
+ try:
155
+ plt.clf()
156
+ plt.close('all')
157
+
158
+ # Scope with "Dummy Show" to prevent Ubuntu window popups
159
+ def dummy_show(*args, **kwargs): pass
160
+
161
+ exec_scope = {
162
+ "plt": plt, "np": np, "go": go, "mpld3": mpld3,
163
+ "__builtins__": __builtins__
164
+ }
165
+ plt.show = dummy_show
166
+
167
+ exec(code, exec_scope)
168
+
169
+ if "fig" in exec_scope:
170
+ # Captures Plotly (3D/Animated)
171
+ html_output = exec_scope["fig"].to_html(full_html=False, include_plotlyjs='cdn')
172
+ else:
173
+ # Captures Matplotlib (2D)
174
+ html_output = mpld3.fig_to_html(plt.gcf())
175
+
176
+ return {"code": code, "html": html_output}
177
+
178
+ except Exception as e:
179
+ return {"code": code, "error": str(e)}
180
+
181
+ if __name__ == "__main__":
182
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ llama-cpp-python
4
+ numpy
5
+ matplotlib
6
+ mpld3
7
+ plotly