Spaces:
Sleeping
Sleeping
File size: 1,165 Bytes
b692cf4 1a44ab8 b692cf4 67bc86d b692cf4 67bc86d b692cf4 94b204b b692cf4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import os
from dotenv import load_dotenv
from transformers import TFBertForSequenceClassification, BertTokenizerFast
# Load environment variables from .env file
load_dotenv()
def load_model(model_name):
try:
# Load TensorFlow model from Hugging Face
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token=os.getenv('API_KEY'), from_tf=True)
except OSError:
raise ValueError("Model loading failed.")
return model
def load_tokenizer(model_name):
tokenizer = BertTokenizerFast.from_pretrained(model_name, use_auth_token=os.getenv('API_KEY'))
return tokenizer
def predict(text, model, tokenizer):
inputs = tokenizer(text, return_tensors="tf")
outputs = model(**inputs)
return outputs
def main():
model_name = os.getenv('MODEL_PATH')
if model_name is None:
raise ValueError("MODEL_PATH environment variable not set or is None")
model = load_model(model_name)
tokenizer = load_tokenizer(model_name)
# Example prediction
text = "Sample input text"
result = predict(text, model, tokenizer)
print(result)
if __name__ == "__main__":
main() |