Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,71 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import torch
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
# ------------------------------
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
# ------------------------------
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
# ------------------------------
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
# ------------------------------
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# Hidden API interface for JS fetch requests
|
| 73 |
-
with gr.Row(visible=False):
|
| 74 |
-
prompt_box = gr.Textbox()
|
| 75 |
-
output_box = gr.Textbox()
|
| 76 |
-
|
| 77 |
-
# API endpoint for your webpage's JS to call
|
| 78 |
-
prompt_box.change(
|
| 79 |
-
fn=generate_code,
|
| 80 |
-
inputs=prompt_box,
|
| 81 |
-
outputs=output_box
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
# ------------------------------
|
| 85 |
-
# Launch App
|
| 86 |
-
# ------------------------------
|
| 87 |
-
if __name__ == "__main__":
|
| 88 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from fastapi import FastAPI, Request
|
| 4 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 6 |
+
from peft import PeftModel
|
| 7 |
+
import uvicorn
|
| 8 |
+
|
| 9 |
+
# ------------------------------
|
| 10 |
+
# Config
|
| 11 |
+
# ------------------------------
|
| 12 |
+
BASE_MODEL = "deepseek-ai/deepseek-coder-6.7b-base"
|
| 13 |
+
ADAPTER_PATH = "Agasthya0/colabmind-coder-6.7b-ml-qlora"
|
| 14 |
+
|
| 15 |
+
# ------------------------------
|
| 16 |
+
# Load Model + Tokenizer
|
| 17 |
+
# ------------------------------
|
| 18 |
+
print("π Loading tokenizer...")
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)
|
| 20 |
+
|
| 21 |
+
print("π§ Loading base model in 4-bit...")
|
| 22 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
+
BASE_MODEL,
|
| 24 |
+
load_in_4bit=True,
|
| 25 |
+
device_map="auto",
|
| 26 |
+
torch_dtype=torch.float16
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
print("π Attaching LoRA adapter...")
|
| 30 |
+
model = PeftModel.from_pretrained(model, ADAPTER_PATH)
|
| 31 |
+
|
| 32 |
+
# ------------------------------
|
| 33 |
+
# Inference Function
|
| 34 |
+
# ------------------------------
|
| 35 |
+
def generate_code(prompt: str):
|
| 36 |
+
if not prompt.strip():
|
| 37 |
+
return "β οΈ Please enter a prompt."
|
| 38 |
+
|
| 39 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 40 |
+
with torch.no_grad():
|
| 41 |
+
outputs = model.generate(
|
| 42 |
+
**inputs,
|
| 43 |
+
max_new_tokens=512,
|
| 44 |
+
temperature=0.2,
|
| 45 |
+
top_p=0.9,
|
| 46 |
+
do_sample=True,
|
| 47 |
+
pad_token_id=tokenizer.eos_token_id
|
| 48 |
+
)
|
| 49 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 50 |
+
|
| 51 |
+
# ------------------------------
|
| 52 |
+
# FastAPI App
|
| 53 |
+
# ------------------------------
|
| 54 |
+
app = FastAPI()
|
| 55 |
+
|
| 56 |
+
@app.get("/")
|
| 57 |
+
def serve_frontend():
|
| 58 |
+
return FileResponse("index.html")
|
| 59 |
+
|
| 60 |
+
@app.post("/run/predict")
|
| 61 |
+
async def predict(request: Request):
|
| 62 |
+
data = await request.json()
|
| 63 |
+
prompt = data.get("data", [""])[0]
|
| 64 |
+
output = generate_code(prompt)
|
| 65 |
+
return JSONResponse({"data": [output]})
|
| 66 |
+
|
| 67 |
+
# ------------------------------
|
| 68 |
+
# Run (for local debugging, Spaces ignores this)
|
| 69 |
+
# ------------------------------
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|