Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,28 +2,42 @@ import gradio as gr
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
# --- 1. Get the Hugging Face Token from Space Secrets ---
|
| 6 |
-
# The os.getenv() function securely reads the secret you just created.
|
| 7 |
auth_token = os.getenv("HF_TOKEN")
|
| 8 |
|
| 9 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
MODEL_ID = "Bur3hani/karani_ofline"
|
|
|
|
| 11 |
|
| 12 |
try:
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
-
print(f"❌
|
| 21 |
-
|
| 22 |
|
| 23 |
# --- 3. Define the Prediction Function ---
|
| 24 |
def get_chat_response(message, history):
|
| 25 |
if not MODEL_LOADED:
|
| 26 |
-
return "ERROR: The AI model failed to load.
|
| 27 |
|
| 28 |
input_text = ""
|
| 29 |
for user_turn, bot_turn in history:
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
# --- 1. Securely Get the Hugging Face Token from Space Secrets ---
|
|
|
|
| 6 |
auth_token = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
+
# --- DEBUGGING STEP: Check if the token was found ---
|
| 9 |
+
# This will print to your Space's logs.
|
| 10 |
+
if auth_token is not None:
|
| 11 |
+
# Do not print the full token for security. Just confirm it was found.
|
| 12 |
+
print("✅ HF_TOKEN secret was found by the application.")
|
| 13 |
+
else:
|
| 14 |
+
print("❌ HF_TOKEN secret was NOT found. Please double-check it is set in your Space Settings.")
|
| 15 |
+
# --- END DEBUGGING STEP ---
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
# --- 2. Load your Model using the Token ---
|
| 19 |
MODEL_ID = "Bur3hani/karani_ofline"
|
| 20 |
+
MODEL_LOADED = False
|
| 21 |
|
| 22 |
try:
|
| 23 |
+
if auth_token:
|
| 24 |
+
print(f"Attempting to load tokenizer and model from Hub: {MODEL_ID}...")
|
| 25 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, token=auth_token)
|
| 26 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_ID, token=auth_token)
|
| 27 |
+
print("✅ Model and Tokenizer loaded successfully.")
|
| 28 |
+
MODEL_LOADED = True
|
| 29 |
+
else:
|
| 30 |
+
# This will be printed if the secret is missing.
|
| 31 |
+
print("Skipping model loading because HF_TOKEN is missing.")
|
| 32 |
+
|
| 33 |
except Exception as e:
|
| 34 |
+
print(f"❌ An error occurred while loading the model from the Hub: {e}")
|
| 35 |
+
|
| 36 |
|
| 37 |
# --- 3. Define the Prediction Function ---
|
| 38 |
def get_chat_response(message, history):
|
| 39 |
if not MODEL_LOADED:
|
| 40 |
+
return "ERROR: The AI model failed to load. Please check the Space logs for the exact error."
|
| 41 |
|
| 42 |
input_text = ""
|
| 43 |
for user_turn, bot_turn in history:
|