Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,37 @@
|
|
1 |
-
|
2 |
-
from
|
|
|
|
|
3 |
|
4 |
-
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
model
|
9 |
-
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
outputs = model(**inputs)
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
if __name__ ==
|
20 |
-
|
|
|
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 |
+
# Try loading the model as a TensorFlow model
|
12 |
+
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token=os.getenv('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'))
|
13 |
+
except OSError:
|
14 |
+
# If loading fails, assume it's a PyTorch model and use from_pt=True
|
15 |
+
model = TFBertForSequenceClassification.from_pretrained(model_name, use_auth_token=os.getenv('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'), from_pt=True)
|
16 |
+
return model
|
17 |
|
18 |
+
def load_tokenizer(model_name):
|
19 |
+
tokenizer = BertTokenizerFast.from_pretrained(model_name, use_auth_token=os.getenv('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'))
|
20 |
+
return tokenizer
|
21 |
+
|
22 |
+
def predict(text, model, tokenizer):
|
23 |
+
inputs = tokenizer(text, return_tensors="tf")
|
24 |
outputs = model(**inputs)
|
25 |
+
return outputs
|
26 |
+
|
27 |
+
def main():
|
28 |
+
model_name = os.getenv('Erfan11/Neuracraft')
|
29 |
+
model = load_model(model_name)
|
30 |
+
tokenizer = load_tokenizer(model_name)
|
31 |
+
# Example usage
|
32 |
+
text = "Sample input text"
|
33 |
+
result = predict(text, model, tokenizer)
|
34 |
+
print(result)
|
35 |
|
36 |
+
if __name__ == "__main__":
|
37 |
+
main()
|