Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,22 +1,17 @@
|
|
1 |
import os
|
2 |
-
from dotenv import load_dotenv
|
3 |
from transformers import TFBertForSequenceClassification, BertTokenizerFast
|
4 |
-
import tensorflow as tf
|
5 |
-
|
6 |
-
# Load environment variables
|
7 |
-
load_dotenv()
|
8 |
|
9 |
def load_model(model_name):
|
10 |
try:
|
11 |
-
#
|
12 |
-
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token=
|
13 |
except OSError:
|
14 |
-
#
|
15 |
-
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token=
|
16 |
return model
|
17 |
|
18 |
def load_tokenizer(model_name):
|
19 |
-
tokenizer = BertTokenizerFast.from_pretrained(model_name, use_auth_token=
|
20 |
return tokenizer
|
21 |
|
22 |
def predict(text, model, tokenizer):
|
@@ -25,10 +20,13 @@ def predict(text, model, tokenizer):
|
|
25 |
return outputs
|
26 |
|
27 |
def main():
|
28 |
-
|
|
|
|
|
29 |
model = load_model(model_name)
|
30 |
tokenizer = load_tokenizer(model_name)
|
31 |
-
|
|
|
32 |
text = "Sample input text"
|
33 |
result = predict(text, model, tokenizer)
|
34 |
print(result)
|
|
|
1 |
import os
|
|
|
2 |
from transformers import TFBertForSequenceClassification, BertTokenizerFast
|
|
|
|
|
|
|
|
|
3 |
|
4 |
def load_model(model_name):
|
5 |
try:
|
6 |
+
# Load TensorFlow model first
|
7 |
+
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token="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")
|
8 |
except OSError:
|
9 |
+
# Fallback to PyTorch model if TensorFlow fails
|
10 |
+
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token="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", from_pt=True)
|
11 |
return model
|
12 |
|
13 |
def load_tokenizer(model_name):
|
14 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_name, use_auth_token="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")
|
15 |
return tokenizer
|
16 |
|
17 |
def predict(text, model, tokenizer):
|
|
|
20 |
return outputs
|
21 |
|
22 |
def main():
|
23 |
+
# Replace 'Erfan11/Neuracraft' with the correct model path if necessary
|
24 |
+
model_name = "Erfan11/Neuracraft"
|
25 |
+
|
26 |
model = load_model(model_name)
|
27 |
tokenizer = load_tokenizer(model_name)
|
28 |
+
|
29 |
+
# Example prediction
|
30 |
text = "Sample input text"
|
31 |
result = predict(text, model, tokenizer)
|
32 |
print(result)
|