Spaces:
Sleeping
Sleeping
Qwen check if model is accessible
Browse files
app.py
CHANGED
|
@@ -126,6 +126,22 @@ def build_index():
|
|
| 126 |
index = faiss.IndexFlatL2(384) # Dummy index with correct dim for all-MiniLM-L6-v2
|
| 127 |
|
| 128 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
def is_token_valid(token: str) -> bool:
|
| 130 |
"""
|
| 131 |
Performs a basic check if the HF token is valid by calling the /api/whoami-v2 endpoint.
|
|
@@ -154,7 +170,7 @@ def is_token_valid(token: str) -> bool:
|
|
| 154 |
def ask_question(question, hf_token, model_id):
|
| 155 |
global index, chunks
|
| 156 |
print("--- Starting ask_question function ---")
|
| 157 |
-
|
| 158 |
# 1. Check index
|
| 159 |
print(f"1. Checking index and chunks...")
|
| 160 |
if index is None:
|
|
@@ -176,9 +192,18 @@ def ask_question(question, hf_token, model_id):
|
|
| 176 |
return error_msg
|
| 177 |
print(" -> Token validated successfully.")
|
| 178 |
|
| 179 |
-
# 3.
|
| 180 |
-
print(f"3.
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
# --- Detailed Steps with Logging ---
|
| 183 |
print(" a. Encoding question...")
|
| 184 |
q_emb = embedder.encode([question], convert_to_numpy=True)
|
|
@@ -203,22 +228,70 @@ def ask_question(question, hf_token, model_id):
|
|
| 203 |
print(" f. Constructing prompt...")
|
| 204 |
prompt = f"Answer based on context:\n{context}\nQuestion: {question}\nAnswer:"
|
| 205 |
print(f" -> Prompt constructed. Length: {len(prompt)} characters.")
|
| 206 |
-
# Optional: Print first part of prompt for debugging
|
| 207 |
-
# print(f" -> Prompt preview: {prompt[:200]}...")
|
| 208 |
|
| 209 |
-
|
| 210 |
-
|
|
|
|
| 211 |
client = InferenceClient(model=model_id, token=hf_token)
|
| 212 |
print(" -> InferenceClient initialized.")
|
| 213 |
|
| 214 |
-
print("
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
print("--- ask_question function completed successfully ---")
|
| 220 |
return resp
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
except Exception as e:
|
| 223 |
error_msg = f"Error processing question: {str(e)}"
|
| 224 |
print(f" -> {error_msg}")
|
|
@@ -227,7 +300,6 @@ def ask_question(question, hf_token, model_id):
|
|
| 227 |
traceback.print_exc()
|
| 228 |
print("--- ask_question function failed ---")
|
| 229 |
return error_msg # Return the error message to the UI
|
| 230 |
-
|
| 231 |
# ... (rest of the code remains the same)
|
| 232 |
# ==========================
|
| 233 |
# Build index on startup
|
|
@@ -262,7 +334,7 @@ with gr.Blocks(title="UCC RAG Chatbot") as demo:
|
|
| 262 |
with gr.Column(scale=1):
|
| 263 |
gr.Markdown("### Configuration")
|
| 264 |
hf_token = gr.Textbox(label="Hugging Face Token", type="password", info="Get your token from https://huggingface.co/settings/tokens")
|
| 265 |
-
model_id = gr.Textbox(label="Model ID", value="
|
| 266 |
with gr.Column(scale=2):
|
| 267 |
gr.Markdown("### Ask a Question")
|
| 268 |
question = gr.Textbox(label="Your Question", placeholder="e.g., What is the role of UCC?")
|
|
@@ -280,7 +352,7 @@ with gr.Blocks(title="UCC RAG Chatbot") as demo:
|
|
| 280 |
# inputs=[
|
| 281 |
# gr.Textbox(label="Question"),
|
| 282 |
# gr.Textbox(label="HF Token", type="password"),
|
| 283 |
-
# gr.Textbox(label="Model ID", value="
|
| 284 |
# ],
|
| 285 |
# outputs=gr.JSON(),
|
| 286 |
# title="API Endpoint",
|
|
|
|
| 126 |
index = faiss.IndexFlatL2(384) # Dummy index with correct dim for all-MiniLM-L6-v2
|
| 127 |
|
| 128 |
|
| 129 |
+
def is_model_accessible(token: str, model_id: str) -> bool:
|
| 130 |
+
"""
|
| 131 |
+
Checks if the model exists and if the user has permission to access it via the Hub API.
|
| 132 |
+
"""
|
| 133 |
+
try:
|
| 134 |
+
api = HfApi()
|
| 135 |
+
# This call requires a valid token and checks permissions
|
| 136 |
+
model_info = api.model_info(model_id, token=token)
|
| 137 |
+
print(f"Model '{model_id}' is accessible. Model info retrieved.")
|
| 138 |
+
# print(f"Model info snippet: {model_info}") # Uncomment for more details
|
| 139 |
+
return True
|
| 140 |
+
except Exception as e:
|
| 141 |
+
print(f"Model accessibility check failed for '{model_id}': {e}")
|
| 142 |
+
# Check specific error types if needed (e.g., HTTP 401, 403, 404)
|
| 143 |
+
return False
|
| 144 |
+
|
| 145 |
def is_token_valid(token: str) -> bool:
|
| 146 |
"""
|
| 147 |
Performs a basic check if the HF token is valid by calling the /api/whoami-v2 endpoint.
|
|
|
|
| 170 |
def ask_question(question, hf_token, model_id):
|
| 171 |
global index, chunks
|
| 172 |
print("--- Starting ask_question function ---")
|
| 173 |
+
|
| 174 |
# 1. Check index
|
| 175 |
print(f"1. Checking index and chunks...")
|
| 176 |
if index is None:
|
|
|
|
| 192 |
return error_msg
|
| 193 |
print(" -> Token validated successfully.")
|
| 194 |
|
| 195 |
+
# 3. Check Model Accessibility
|
| 196 |
+
print(f"3. Checking model accessibility: '{model_id}'...")
|
| 197 |
+
if not is_model_accessible(hf_token, model_id):
|
| 198 |
+
error_msg = f"Model '{model_id}' is not accessible with the provided token or does not exist. Please check the model ID and token permissions."
|
| 199 |
+
print(f" -> {error_msg}")
|
| 200 |
+
return error_msg
|
| 201 |
+
print(" -> Model accessibility check passed.")
|
| 202 |
+
|
| 203 |
+
|
| 204 |
+
# 4. Process Question
|
| 205 |
+
print(f"4. Processing question: '{question}' using model '{model_id}'...")
|
| 206 |
+
|
| 207 |
# --- Detailed Steps with Logging ---
|
| 208 |
print(" a. Encoding question...")
|
| 209 |
q_emb = embedder.encode([question], convert_to_numpy=True)
|
|
|
|
| 228 |
print(" f. Constructing prompt...")
|
| 229 |
prompt = f"Answer based on context:\n{context}\nQuestion: {question}\nAnswer:"
|
| 230 |
print(f" -> Prompt constructed. Length: {len(prompt)} characters.")
|
|
|
|
|
|
|
| 231 |
|
| 232 |
+
|
| 233 |
+
# 5. Call Model API
|
| 234 |
+
print("5. Initializing InferenceClient...")
|
| 235 |
client = InferenceClient(model=model_id, token=hf_token)
|
| 236 |
print(" -> InferenceClient initialized.")
|
| 237 |
|
| 238 |
+
print("6. Calling model API (text_generation)...")
|
| 239 |
+
|
| 240 |
+
# --- Potential Workaround ---
|
| 241 |
+
# Instead of letting the client auto-detect, explicitly specify the endpoint URL
|
| 242 |
+
# This bypasses some of the internal routing logic that might be failing.
|
| 243 |
+
try:
|
| 244 |
+
# Try the standard call first
|
| 245 |
+
resp = client.text_generation(prompt, max_new_tokens=200, temperature=0.7)
|
| 246 |
+
print(" -> Model API call (standard) successful.")
|
| 247 |
+
except StopIteration:
|
| 248 |
+
# If the specific error occurs, try the workaround
|
| 249 |
+
print(" -> Standard call failed with StopIteration. Trying direct endpoint URL...")
|
| 250 |
+
# Construct the direct Inference API endpoint URL for the model
|
| 251 |
+
api_url = f"https://api-inference.huggingface.co/models/{model_id}"
|
| 252 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 253 |
+
payload = {
|
| 254 |
+
"inputs": prompt,
|
| 255 |
+
"parameters": {
|
| 256 |
+
"max_new_tokens": 200,
|
| 257 |
+
"temperature": 0.7,
|
| 258 |
+
# Add other parameters if needed
|
| 259 |
+
}
|
| 260 |
+
}
|
| 261 |
+
print(f" -> Sending POST request to {api_url}")
|
| 262 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
| 263 |
+
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)
|
| 264 |
+
# Handle the response based on the model type
|
| 265 |
+
# For text2text-generation models like Flan-T5, the response is usually JSON with a 'generated_text' key
|
| 266 |
+
# For text-generation models, it might be just the text or a list
|
| 267 |
+
# Let's try parsing as JSON first
|
| 268 |
+
try:
|
| 269 |
+
result = response.json()
|
| 270 |
+
if isinstance(result, list) and len(result) > 0:
|
| 271 |
+
# Often the first item is a dict with 'generated_text'
|
| 272 |
+
if isinstance(result[0], dict) and 'generated_text' in result[0]:
|
| 273 |
+
resp = result[0]['generated_text']
|
| 274 |
+
else:
|
| 275 |
+
# If it's a list of strings or other format, adapt here
|
| 276 |
+
resp = str(result[0])
|
| 277 |
+
elif isinstance(result, dict) and 'generated_text' in result:
|
| 278 |
+
resp = result['generated_text']
|
| 279 |
+
else:
|
| 280 |
+
# Fallback: convert the whole response to string
|
| 281 |
+
resp = str(result)
|
| 282 |
+
except requests.exceptions.JSONDecodeError:
|
| 283 |
+
# If not JSON, assume raw text (less common for this endpoint but possible)
|
| 284 |
+
resp = response.text
|
| 285 |
+
|
| 286 |
+
print(" -> Model API call (direct) successful.")
|
| 287 |
|
| 288 |
print("--- ask_question function completed successfully ---")
|
| 289 |
return resp
|
| 290 |
|
| 291 |
+
except requests.exceptions.RequestException as e: # Catch network-related errors from requests
|
| 292 |
+
error_msg = f"Network error calling model API: {e}"
|
| 293 |
+
print(f" -> {error_msg}")
|
| 294 |
+
return error_msg
|
| 295 |
except Exception as e:
|
| 296 |
error_msg = f"Error processing question: {str(e)}"
|
| 297 |
print(f" -> {error_msg}")
|
|
|
|
| 300 |
traceback.print_exc()
|
| 301 |
print("--- ask_question function failed ---")
|
| 302 |
return error_msg # Return the error message to the UI
|
|
|
|
| 303 |
# ... (rest of the code remains the same)
|
| 304 |
# ==========================
|
| 305 |
# Build index on startup
|
|
|
|
| 334 |
with gr.Column(scale=1):
|
| 335 |
gr.Markdown("### Configuration")
|
| 336 |
hf_token = gr.Textbox(label="Hugging Face Token", type="password", info="Get your token from https://huggingface.co/settings/tokens")
|
| 337 |
+
model_id = gr.Textbox(label="Model ID", value="deepseek-llm-7b-base", info="ID of the Hugging Face model to use")
|
| 338 |
with gr.Column(scale=2):
|
| 339 |
gr.Markdown("### Ask a Question")
|
| 340 |
question = gr.Textbox(label="Your Question", placeholder="e.g., What is the role of UCC?")
|
|
|
|
| 352 |
# inputs=[
|
| 353 |
# gr.Textbox(label="Question"),
|
| 354 |
# gr.Textbox(label="HF Token", type="password"),
|
| 355 |
+
# gr.Textbox(label="Model ID", value="deepseek-llm-7b-base")
|
| 356 |
# ],
|
| 357 |
# outputs=gr.JSON(),
|
| 358 |
# title="API Endpoint",
|