Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,34 @@
|
|
| 1 |
import os
|
| 2 |
-
from transformers import
|
| 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 |
-
#
|
| 13 |
HF_TOKEN = os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 14 |
|
| 15 |
-
if
|
| 16 |
-
|
| 17 |
|
| 18 |
-
# Function to load the model safely
|
| 19 |
def load_model(model_id):
|
|
|
|
|
|
|
|
|
|
| 20 |
try:
|
| 21 |
-
|
| 22 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id,
|
| 23 |
-
model = AutoModelForCausalLM.from_pretrained(model_id,
|
| 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 |
-
#
|
| 32 |
generator = load_model(MODEL_ID)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
# Model configuration
|
| 5 |
MODEL_ID = "PuruAI/Medini_Intelligence"
|
| 6 |
FALLBACK_MODEL = "gpt2"
|
| 7 |
|
| 8 |
+
# Get Hugging Face token from secret/environment variable
|
| 9 |
HF_TOKEN = os.environ.get("HUGGINGFACE_HUB_TOKEN")
|
| 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, token=HF_TOKEN)
|
| 21 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, token=HF_TOKEN)
|
|
|
|
| 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, token=HF_TOKEN)
|
| 27 |
|
| 28 |
+
# Initialize the generator
|
| 29 |
generator = load_model(MODEL_ID)
|
| 30 |
|
| 31 |
+
# Example usage
|
| 32 |
+
prompt = "Once upon a time"
|
| 33 |
+
result = generator(prompt, max_length=50, do_sample=True)
|
| 34 |
+
print(result)
|