Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI
|
2 |
from pydantic import BaseModel
|
3 |
import torch
|
4 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
5 |
from peft import PeftModel
|
6 |
-
import os
|
7 |
from huggingface_hub import login
|
8 |
|
9 |
# β
Read token from Hugging Face Secrets
|
10 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
11 |
|
12 |
-
# β
Login only if token exists
|
13 |
if HF_TOKEN:
|
14 |
-
login(token=HF_TOKEN)
|
15 |
|
16 |
# β
Initialize FastAPI
|
17 |
app = FastAPI()
|
18 |
|
19 |
-
# β
Define Base Model & LoRA Adapter Repository (Smaller Model
|
20 |
-
base_model_name = "mistralai/Mistral-7B-Instruct-v0.
|
21 |
lora_repo_id = "khushi1234455687/fine-tuned-medical-qa-V8"
|
22 |
|
23 |
-
# β
|
24 |
-
device = "
|
25 |
|
26 |
# β
Load Tokenizer
|
27 |
-
tokenizer = AutoTokenizer.from_pretrained(base_model_name)
|
28 |
|
29 |
-
# β
Configure 4-bit Quantization
|
30 |
-
quantization_config = BitsAndBytesConfig(
|
31 |
-
load_in_4bit=True
|
32 |
-
)
|
33 |
|
34 |
-
# β
Load Base Model
|
35 |
try:
|
36 |
base_model = AutoModelForCausalLM.from_pretrained(
|
37 |
base_model_name,
|
38 |
quantization_config=quantization_config,
|
39 |
-
device_map="
|
40 |
-
torch_dtype=torch.float16
|
|
|
41 |
)
|
42 |
except Exception as e:
|
43 |
print(f"β Error loading base model: {e}")
|
@@ -45,8 +50,8 @@ except Exception as e:
|
|
45 |
|
46 |
# β
Load LoRA Adapter
|
47 |
try:
|
48 |
-
model = PeftModel.from_pretrained(base_model, lora_repo_id)
|
49 |
-
model.to(device)
|
50 |
model.eval()
|
51 |
except Exception as e:
|
52 |
print(f"β Error loading LoRA adapter: {e}")
|
@@ -62,7 +67,7 @@ class QueryRequest(BaseModel):
|
|
62 |
async def generate_answer(request: QueryRequest):
|
63 |
"""Generate an answer for a given medical question."""
|
64 |
try:
|
65 |
-
inputs = tokenizer(request.question, return_tensors="pt").to(device)
|
66 |
with torch.no_grad():
|
67 |
output = model.generate(**inputs, max_length=256)
|
68 |
answer = tokenizer.decode(output[0], skip_special_tokens=True)
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
# β
Set a writable cache directory inside `/app`
|
4 |
+
os.environ["HF_HOME"] = "/app/huggingface"
|
5 |
+
os.environ["TRANSFORMERS_CACHE"] = "/app/huggingface"
|
6 |
+
os.environ["HF_HUB_CACHE"] = "/app/huggingface"
|
7 |
+
|
8 |
from fastapi import FastAPI
|
9 |
from pydantic import BaseModel
|
10 |
import torch
|
11 |
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
|
12 |
from peft import PeftModel
|
|
|
13 |
from huggingface_hub import login
|
14 |
|
15 |
# β
Read token from Hugging Face Secrets
|
16 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
17 |
|
18 |
+
# β
Login only if token exists (Prevent writing to protected directories)
|
19 |
if HF_TOKEN:
|
20 |
+
login(token=HF_TOKEN, cache_dir="/app/huggingface")
|
21 |
|
22 |
# β
Initialize FastAPI
|
23 |
app = FastAPI()
|
24 |
|
25 |
+
# β
Define Base Model & LoRA Adapter Repository (Use a Smaller Model)
|
26 |
+
base_model_name = "mistralai/Mistral-7B-Instruct-v0.1" # πΉ Switched to a smaller model
|
27 |
lora_repo_id = "khushi1234455687/fine-tuned-medical-qa-V8"
|
28 |
|
29 |
+
# β
Force CPU Usage (Hugging Face Spaces Does NOT Support GPUs)
|
30 |
+
device = "cpu"
|
31 |
|
32 |
# β
Load Tokenizer
|
33 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_name, cache_dir="/app/huggingface")
|
34 |
|
35 |
+
# β
Configure 4-bit Quantization (Optimized for Spaces)
|
36 |
+
quantization_config = BitsAndBytesConfig(load_in_4bit=True)
|
|
|
|
|
37 |
|
38 |
+
# β
Load Base Model
|
39 |
try:
|
40 |
base_model = AutoModelForCausalLM.from_pretrained(
|
41 |
base_model_name,
|
42 |
quantization_config=quantization_config,
|
43 |
+
device_map="cpu",
|
44 |
+
torch_dtype=torch.float16,
|
45 |
+
cache_dir="/app/huggingface"
|
46 |
)
|
47 |
except Exception as e:
|
48 |
print(f"β Error loading base model: {e}")
|
|
|
50 |
|
51 |
# β
Load LoRA Adapter
|
52 |
try:
|
53 |
+
model = PeftModel.from_pretrained(base_model, lora_repo_id, cache_dir="/app/huggingface")
|
54 |
+
model.to(device)
|
55 |
model.eval()
|
56 |
except Exception as e:
|
57 |
print(f"β Error loading LoRA adapter: {e}")
|
|
|
67 |
async def generate_answer(request: QueryRequest):
|
68 |
"""Generate an answer for a given medical question."""
|
69 |
try:
|
70 |
+
inputs = tokenizer(request.question, return_tensors="pt").to(device)
|
71 |
with torch.no_grad():
|
72 |
output = model.generate(**inputs, max_length=256)
|
73 |
answer = tokenizer.decode(output[0], skip_special_tokens=True)
|