Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,36 @@
|
|
| 1 |
import os
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
#
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from transformers import pipeline
|
| 7 |
|
| 8 |
# Model configuration
|
| 9 |
MODEL_ID = "PuruAI/Medini_Intelligence"
|
| 10 |
FALLBACK_MODEL = "gpt2"
|
| 11 |
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
# Test the
|
| 22 |
-
|
| 23 |
-
print(
|
|
|
|
| 1 |
import os
|
| 2 |
+
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
from transformers.utils import logging
|
| 4 |
|
| 5 |
+
# Suppress warnings
|
| 6 |
+
logging.set_verbosity_error()
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Model configuration
|
| 9 |
MODEL_ID = "PuruAI/Medini_Intelligence"
|
| 10 |
FALLBACK_MODEL = "gpt2"
|
| 11 |
|
| 12 |
+
# Load token from environment variable
|
| 13 |
+
HF_TOKEN = os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 14 |
+
|
| 15 |
+
if HF_TOKEN is None:
|
| 16 |
+
print("⚠️ Warning: HUGGINGFACE_HUB_TOKEN not set. Private models may fail to load.")
|
| 17 |
+
|
| 18 |
+
# Function to load the model safely
|
| 19 |
+
def load_model(model_id):
|
| 20 |
+
try:
|
| 21 |
+
# Load tokenizer and model from Hugging Face
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
| 24 |
+
print(f"✅ Successfully loaded {model_id}")
|
| 25 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"❌ Failed to load {model_id}: {e}")
|
| 28 |
+
print(f"⏩ Falling back to {FALLBACK_MODEL}")
|
| 29 |
+
return pipeline("text-generation", model=FALLBACK_MODEL)
|
| 30 |
+
|
| 31 |
+
# Load Medini AI (with fallback to GPT-2)
|
| 32 |
+
generator = load_model(MODEL_ID)
|
| 33 |
|
| 34 |
+
# Test the model
|
| 35 |
+
output = generator("Hello, how are you?", max_length=50)
|
| 36 |
+
print(output)
|