Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,7 +8,7 @@ from sentence_transformers import SentenceTransformer
|
|
| 8 |
from inspect import signature
|
| 9 |
|
| 10 |
# =====================================================
|
| 11 |
-
# OPTION
|
| 12 |
# =====================================================
|
| 13 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
| 14 |
os.environ["HF_HOME"] = "/tmp/hf_home"
|
|
@@ -18,54 +18,64 @@ os.environ["HF_MODULES_CACHE"] = "/tmp/hf_modules"
|
|
| 18 |
# =====================================================
|
| 19 |
# 1️⃣ Model setup
|
| 20 |
# =====================================================
|
| 21 |
-
|
|
|
|
| 22 |
EMB_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 23 |
|
| 24 |
-
# --- Handle HF authentication ---
|
| 25 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 26 |
-
|
| 27 |
if not HF_TOKEN:
|
| 28 |
-
print("⚠️ No Hugging Face token found.
|
| 29 |
-
print(" Example: export HF_TOKEN=hf_yourtoken123 or add it in Hugging Face Space secrets.")
|
| 30 |
-
else:
|
| 31 |
-
print("✅ Hugging Face token detected.")
|
| 32 |
|
| 33 |
-
# ---
|
| 34 |
try:
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
dtype_value = torch.float16 if torch.cuda.is_available() else torch.float32
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
"cache_dir": "/tmp/hf_cache",
|
| 42 |
-
"device_map": "auto",
|
| 43 |
-
"low_cpu_mem_usage": True,
|
| 44 |
-
"token": HF_TOKEN,
|
| 45 |
-
}
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
-
print(f"❌
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
tokenizer = AutoTokenizer.from_pretrained(GEN_MODEL)
|
| 55 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 56 |
-
GEN_MODEL,
|
| 57 |
-
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 58 |
-
device_map="auto",
|
| 59 |
-
low_cpu_mem_usage=True,
|
| 60 |
-
)
|
| 61 |
|
|
|
|
| 62 |
embedder = SentenceTransformer(EMB_MODEL, cache_folder="/tmp/hf_cache")
|
| 63 |
|
| 64 |
# =====================================================
|
| 65 |
# 2️⃣ Retrieval + generation logic
|
| 66 |
# =====================================================
|
| 67 |
-
# Placeholder FAISS index and chunks (replace with your actual
|
| 68 |
-
index = faiss.IndexFlatL2(384)
|
| 69 |
chunks = ["This is a sample context chunk. Replace with real documents."]
|
| 70 |
|
| 71 |
def retrieve_context(query, k=5):
|
|
@@ -108,7 +118,7 @@ with gr.Blocks(title="Qwen3-Harrison-RAG Chatbot") as demo:
|
|
| 108 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 109 |
|
| 110 |
# =====================================================
|
| 111 |
-
# 4️⃣ Launch
|
| 112 |
# =====================================================
|
| 113 |
if __name__ == "__main__":
|
| 114 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|
|
|
|
| 8 |
from inspect import signature
|
| 9 |
|
| 10 |
# =====================================================
|
| 11 |
+
# OPTION: Use ephemeral /tmp cache
|
| 12 |
# =====================================================
|
| 13 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
| 14 |
os.environ["HF_HOME"] = "/tmp/hf_home"
|
|
|
|
| 18 |
# =====================================================
|
| 19 |
# 1️⃣ Model setup
|
| 20 |
# =====================================================
|
| 21 |
+
GEN_MODEL_PRIVATE = "hackergeek/qwen3-harrison-rag"
|
| 22 |
+
GEN_MODEL_PUBLIC = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 23 |
EMB_MODEL = "sentence-transformers/all-MiniLM-L6-v2"
|
| 24 |
|
|
|
|
| 25 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
| 26 |
if not HF_TOKEN:
|
| 27 |
+
print("⚠️ No Hugging Face token found. Private models may fail to load.")
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# --- Check if accelerate is available ---
|
| 30 |
try:
|
| 31 |
+
import accelerate
|
| 32 |
+
accelerate_available = True
|
| 33 |
+
except ImportError:
|
| 34 |
+
accelerate_available = False
|
| 35 |
+
print("⚠️ `accelerate` not installed. Large private models with device_map='auto' may fail.")
|
| 36 |
+
|
| 37 |
+
# --- Helper to load model safely ---
|
| 38 |
+
def load_model(model_name, token=None):
|
| 39 |
dtype_value = torch.float16 if torch.cuda.is_available() else torch.float32
|
| 40 |
+
try:
|
| 41 |
+
param_names = signature(AutoModelForCausalLM.from_pretrained).parameters
|
| 42 |
+
dtype_arg = "dtype" if "dtype" in param_names else "torch_dtype"
|
| 43 |
+
|
| 44 |
+
load_kwargs = {
|
| 45 |
+
dtype_arg: dtype_value,
|
| 46 |
+
"cache_dir": "/tmp/hf_cache",
|
| 47 |
+
"low_cpu_mem_usage": True,
|
| 48 |
+
}
|
| 49 |
|
| 50 |
+
if accelerate_available:
|
| 51 |
+
load_kwargs["device_map"] = "auto"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
if token:
|
| 54 |
+
load_kwargs["token"] = token
|
| 55 |
|
| 56 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, token=token)
|
| 57 |
+
model = AutoModelForCausalLM.from_pretrained(model_name, **load_kwargs)
|
| 58 |
+
return tokenizer, model
|
| 59 |
+
except Exception as e:
|
| 60 |
+
raise RuntimeError(f"Failed to load model '{model_name}': {e}")
|
| 61 |
+
|
| 62 |
+
# --- Attempt to load private model, fallback to public ---
|
| 63 |
+
try:
|
| 64 |
+
tokenizer, model = load_model(GEN_MODEL_PRIVATE, token=HF_TOKEN)
|
| 65 |
+
print(f"✅ Loaded private model: {GEN_MODEL_PRIVATE}")
|
| 66 |
except Exception as e:
|
| 67 |
+
print(f"❌ {e}\n➡️ Falling back to public model: {GEN_MODEL_PUBLIC}")
|
| 68 |
+
tokenizer, model = load_model(GEN_MODEL_PUBLIC)
|
| 69 |
+
print(f"✅ Loaded public model: {GEN_MODEL_PUBLIC}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# --- Load embedding model ---
|
| 72 |
embedder = SentenceTransformer(EMB_MODEL, cache_folder="/tmp/hf_cache")
|
| 73 |
|
| 74 |
# =====================================================
|
| 75 |
# 2️⃣ Retrieval + generation logic
|
| 76 |
# =====================================================
|
| 77 |
+
# Placeholder FAISS index and chunks (replace with your actual documents)
|
| 78 |
+
index = faiss.IndexFlatL2(384)
|
| 79 |
chunks = ["This is a sample context chunk. Replace with real documents."]
|
| 80 |
|
| 81 |
def retrieve_context(query, k=5):
|
|
|
|
| 118 |
clear.click(lambda: None, None, chatbot, queue=False)
|
| 119 |
|
| 120 |
# =====================================================
|
| 121 |
+
# 4️⃣ Launch
|
| 122 |
# =====================================================
|
| 123 |
if __name__ == "__main__":
|
| 124 |
demo.launch(server_name="0.0.0.0", server_port=int(os.environ.get("PORT", 7860)))
|