Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
import sys
|
|
|
|
|
|
|
| 2 |
import kagglehub
|
| 3 |
import torch
|
| 4 |
from fastapi import FastAPI
|
|
@@ -7,76 +9,59 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# 1. ุชุญู
ูู ุงููู
ูุฐุฌ
|
| 11 |
-
model_path = kagglehub.model_download(
|
| 12 |
-
"ruicompany/onyx-code-figma/keras/default"
|
| 13 |
-
)
|
| 14 |
|
| 15 |
quantization_config = BitsAndBytesConfig(
|
| 16 |
load_in_8bit=True,
|
| 17 |
llm_int8_enable_fp32_cpu_offload=True,
|
| 18 |
)
|
| 19 |
|
| 20 |
-
max_memory = {
|
| 21 |
-
0: "0GB",
|
| 22 |
-
"cpu": "6GB",
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
model_path,
|
| 27 |
quantization_config=quantization_config,
|
| 28 |
device_map="auto",
|
| 29 |
-
max_memory=
|
| 30 |
offload_buffers=True,
|
| 31 |
offload_folder="offload",
|
| 32 |
offload_state_dict=True,
|
| 33 |
torch_dtype=torch.float32,
|
| 34 |
trust_remote_code=True,
|
| 35 |
)
|
| 36 |
-
|
| 37 |
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 38 |
-
print("โ
ุงููู
ูุฐุฌ
|
| 39 |
-
|
| 40 |
-
# 2. ุงุณุชุฏุนุงุก onyx_code ูุงูุชูุงุท ุงูู HTML
|
| 41 |
-
sys.path.insert(0, model_path)
|
| 42 |
-
import onyx_code
|
| 43 |
|
|
|
|
| 44 |
captured_html = []
|
| 45 |
|
| 46 |
-
|
| 47 |
-
# ุงุนุชุฑุงุถ ุฏูุงู ุงูุนุฑุถ ุงูุฎุงุตุฉ ุจู IPython ุฏุงุฎู onyx_code
|
| 48 |
def mock_display(obj, *args, **kwargs):
|
| 49 |
if hasattr(obj, "data"):
|
| 50 |
captured_html.append(obj.data)
|
| 51 |
-
elif isinstance(obj, str):
|
| 52 |
-
captured_html.append(obj)
|
| 53 |
-
|
| 54 |
|
| 55 |
class MockHTML:
|
| 56 |
-
|
| 57 |
def __init__(self, data):
|
| 58 |
self.data = data
|
| 59 |
-
captured_html.append(data)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
| 64 |
|
| 65 |
-
# ุช
|
| 66 |
-
|
|
|
|
| 67 |
|
| 68 |
-
# ุง
|
| 69 |
-
if captured_html:
|
| 70 |
-
html_content = captured_html[-1]
|
| 71 |
-
elif hasattr(result, "data"):
|
| 72 |
-
html_content = result.data
|
| 73 |
-
elif result is not None:
|
| 74 |
-
html_content = str(result)
|
| 75 |
-
else:
|
| 76 |
-
html_content = "<h2>ูู
ูุชู
ุงูุนุซูุฑ ุนูู ููุฏ HTML ู
ู ุงูุฏุงูุฉ</h2>"
|
| 77 |
|
|
|
|
|
|
|
| 78 |
|
| 79 |
-
#
|
| 80 |
@app.get("/", response_class=HTMLResponse)
|
| 81 |
def read_root():
|
| 82 |
return html_content
|
|
|
|
| 1 |
import sys
|
| 2 |
+
import os
|
| 3 |
+
import types
|
| 4 |
import kagglehub
|
| 5 |
import torch
|
| 6 |
from fastapi import FastAPI
|
|
|
|
| 9 |
|
| 10 |
app = FastAPI()
|
| 11 |
|
| 12 |
+
# โโ 1. ุชุญู
ูู ุงููู
ูุฐุฌ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 13 |
+
model_path = kagglehub.model_download("ruicompany/onyx-code-figma/keras/default")
|
|
|
|
|
|
|
| 14 |
|
| 15 |
quantization_config = BitsAndBytesConfig(
|
| 16 |
load_in_8bit=True,
|
| 17 |
llm_int8_enable_fp32_cpu_offload=True,
|
| 18 |
)
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
model = AutoModelForCausalLM.from_pretrained(
|
| 21 |
model_path,
|
| 22 |
quantization_config=quantization_config,
|
| 23 |
device_map="auto",
|
| 24 |
+
max_memory={0: "0GB", "cpu": "6GB"},
|
| 25 |
offload_buffers=True,
|
| 26 |
offload_folder="offload",
|
| 27 |
offload_state_dict=True,
|
| 28 |
torch_dtype=torch.float32,
|
| 29 |
trust_remote_code=True,
|
| 30 |
)
|
|
|
|
| 31 |
tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True)
|
| 32 |
+
print("โ
ุงููู
ูุฐุฌ ุฌุงูุฒ")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
# โโ 2. ูุฎ IPython (Monkey-patch) โโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 35 |
captured_html = []
|
| 36 |
|
|
|
|
|
|
|
| 37 |
def mock_display(obj, *args, **kwargs):
|
| 38 |
if hasattr(obj, "data"):
|
| 39 |
captured_html.append(obj.data)
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
class MockHTML:
|
|
|
|
| 42 |
def __init__(self, data):
|
| 43 |
self.data = data
|
|
|
|
| 44 |
|
| 45 |
+
# ููุดุฆ ุงูู modules ุงูููู
ูุฉ ูุจู ุฃู ุงุณุชูุฑุงุฏ
|
| 46 |
+
ipython_pkg = types.ModuleType("IPython")
|
| 47 |
+
ipython_display = types.ModuleType("IPython.display")
|
| 48 |
+
ipython_display.HTML = MockHTML
|
| 49 |
+
ipython_display.display = mock_display
|
| 50 |
+
ipython_pkg.display = ipython_display
|
| 51 |
|
| 52 |
+
sys.modules["IPython"] = ipython_pkg
|
| 53 |
+
sys.modules["IPython.display"] = ipython_display
|
| 54 |
|
| 55 |
+
# โโ 3. ุงุณุชูุฑุงุฏ onyx_code ูุงูุชูุงุท HTML โโโโโโโโโโโโโโโโโโโโโ
|
| 56 |
+
sys.path.insert(0, model_path)
|
| 57 |
+
import onyx_code
|
| 58 |
|
| 59 |
+
onyx_code.launch_designer() # ุงูู mock ููุชูุท HTML ุชููุงุฆูุงู
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
+
html_content = captured_html[-1] if captured_html else "<h2>โ ๏ธ ูู
ูุชู
ุงูุชูุงุท HTML</h2>"
|
| 62 |
+
print(f"๐ HTML captured: {len(html_content)} chars")
|
| 63 |
|
| 64 |
+
# โโ 4. FastAPI โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 65 |
@app.get("/", response_class=HTMLResponse)
|
| 66 |
def read_root():
|
| 67 |
return html_content
|