Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,29 @@
|
|
| 1 |
import os
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
# Model configuration
|
| 5 |
-
MODEL_ID = "PuruAI/Medini_Intelligence"
|
| 6 |
FALLBACK_MODEL = "gpt2"
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
HF_TOKEN = os.environ.get("
|
| 10 |
-
|
| 11 |
-
if not HF_TOKEN:
|
| 12 |
-
raise ValueError("HUGGINGFACE_HUB_TOKEN is not set. Please set it as a secret/environment variable.")
|
| 13 |
|
| 14 |
def load_model(model_id):
|
| 15 |
-
"""
|
| 16 |
-
Try to load the specified model. If it fails, fall back to GPT-2.
|
| 17 |
-
"""
|
| 18 |
try:
|
| 19 |
print(f"Loading model: {model_id}")
|
| 20 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id,
|
| 21 |
-
model = AutoModelForCausalLM.from_pretrained(model_id,
|
| 22 |
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 23 |
except Exception as e:
|
| 24 |
print(f"❌ Failed to load {model_id}: {e}")
|
| 25 |
print(f"⏩ Falling back to {FALLBACK_MODEL}")
|
| 26 |
-
return pipeline("text-generation", model=FALLBACK_MODEL
|
| 27 |
|
| 28 |
-
# Initialize
|
| 29 |
generator = load_model(MODEL_ID)
|
| 30 |
|
| 31 |
-
#
|
| 32 |
prompt = "Once upon a time"
|
| 33 |
-
|
| 34 |
-
print(
|
|
|
|
| 1 |
import os
|
| 2 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
| 3 |
|
| 4 |
# Model configuration
|
| 5 |
+
MODEL_ID = "PuruAI/Medini_Intelligence" # Your private model
|
| 6 |
FALLBACK_MODEL = "gpt2"
|
| 7 |
|
| 8 |
+
# Load Hugging Face token from secret
|
| 9 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
def load_model(model_id):
|
| 12 |
+
"""Load private model if possible, otherwise fallback to GPT-2."""
|
|
|
|
|
|
|
| 13 |
try:
|
| 14 |
print(f"Loading model: {model_id}")
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, use_auth_token=HF_TOKEN)
|
| 17 |
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
| 18 |
except Exception as e:
|
| 19 |
print(f"❌ Failed to load {model_id}: {e}")
|
| 20 |
print(f"⏩ Falling back to {FALLBACK_MODEL}")
|
| 21 |
+
return pipeline("text-generation", model=FALLBACK_MODEL)
|
| 22 |
|
| 23 |
+
# Initialize pipeline
|
| 24 |
generator = load_model(MODEL_ID)
|
| 25 |
|
| 26 |
+
# Test generation
|
| 27 |
prompt = "Once upon a time"
|
| 28 |
+
output = generator(prompt, max_new_tokens=50)
|
| 29 |
+
print(output)
|