Black Goku commited on
Commit
bbd7dc0
·
verified ·
1 Parent(s): 6be5125

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +6 -0
  2. app.py +74 -0
  3. requirements.txt +7 -0
Dockerfile ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+ WORKDIR /app
3
+ COPY . /app
4
+ RUN pip install --no-cache-dir -r requirements.txt
5
+ EXPOSE 7860
6
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from fastapi import FastAPI
4
+ from pydantic import BaseModel
5
+ from transformers import AutoTokenizer, AutoModelForCausalLM
6
+ from peft import PeftModel
7
+ from fastapi.middleware.cors import CORSMiddleware
8
+
9
+ app = FastAPI()
10
+
11
+ # Security Pass for your HTML file
12
+ app.add_middleware(
13
+ CORSMiddleware,
14
+ allow_origins=["*"],
15
+ allow_credentials=True,
16
+ allow_methods=["*"],
17
+ allow_headers=["*"],
18
+ )
19
+
20
+ model_id = "Qwen/Qwen2.5-1.5B-Instruct"
21
+
22
+ print("Nexus is initializing reasoning cores...")
23
+
24
+ # 1. Load Tokenizer
25
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
26
+
27
+ # 2. Load Base Brain (CPU Optimized)
28
+ base_model = AutoModelForCausalLM.from_pretrained(
29
+ model_id,
30
+ torch_dtype=torch.float32,
31
+ device_map="cpu",
32
+ low_cpu_mem_usage=True
33
+ )
34
+
35
+ # 3. Attach YOUR Nexus Training (Xirrik's Work)
36
+ # We use "." because the files were uploaded to the root of the Space
37
+ try:
38
+ model = PeftModel.from_pretrained(base_model, ".")
39
+ print("Nexus Training Attached Successfully!")
40
+ except Exception as e:
41
+ print(f"Loading files from root failed: {e}. Trying folder...")
42
+ model = PeftModel.from_pretrained(base_model, "./zenvy-nexus-model")
43
+
44
+ class ChatRequest(BaseModel):
45
+ message: str
46
+
47
+ @app.get("/")
48
+ def home():
49
+ return {"status": "Zenvy Nexus is ONLINE", "model": "Qwen-2.5-1.5B-Custom"}
50
+
51
+ @app.post("/chat")
52
+ async def chat(request: ChatRequest):
53
+ try:
54
+ # Nexus Logic Prompt
55
+ prompt = f"<|im_start|>user\n{request.message}<|im_end|>\n<|im_start|>assistant\n"
56
+
57
+ inputs = tokenizer(prompt, return_tensors="pt").to("cpu")
58
+
59
+ with torch.no_grad():
60
+ outputs = model.generate(
61
+ **inputs,
62
+ max_new_tokens=200,
63
+ temperature=0.7,
64
+ do_sample=True,
65
+ repetition_penalty=1.1
66
+ )
67
+
68
+ full_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
69
+ reply = full_text.split("assistant")[-1].strip()
70
+
71
+ return {"reply": reply}
72
+
73
+ except Exception as e:
74
+ return {"reply": f"Nexus Core Error: {str(e)}"}
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ transformers
4
+ torch
5
+ peft
6
+ accelerate
7
+ pydantic