yaya36095 commited on
Commit
b0e99ab
·
verified ·
1 Parent(s): 992886b

Rename handler.py to inference.py

Browse files
Files changed (2) hide show
  1. handler.py +0 -27
  2. inference.py +14 -0
handler.py DELETED
@@ -1,27 +0,0 @@
1
-
2
- from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
- import torch
4
-
5
- class TextClassifier:
6
- def __init__(self, model_path=""):
7
- self.tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
8
- self.model = AutoModelForSequenceClassification.from_pretrained(model_path, ignore_mismatched_sizes=True)
9
- self.model.eval()
10
-
11
- def predict(self, text):
12
- inputs = self.tokenizer(text, return_tensors="pt", padding=True, truncation=True)
13
- with torch.no_grad():
14
- outputs = self.model(**inputs)
15
- probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
16
- return {
17
- "label": "AI" if torch.argmax(probs) == 1 else "HUMAN",
18
- "confidence": probs[0].tolist()
19
- }
20
-
21
- classifier = None
22
-
23
- def predict(inputs):
24
- global classifier
25
- if classifier is None:
26
- classifier = TextClassifier(model_path=".")
27
- return classifier.predict(inputs)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
inference.py ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer, pipeline
2
+
3
+ class EndpointHandler:
4
+ def __init__(self, path=""):
5
+ self.model = AutoModelForSequenceClassification.from_pretrained(
6
+ path,
7
+ ignore_mismatched_sizes=True # ضروري لتجاوز المشكلة
8
+ )
9
+ self.tokenizer = AutoTokenizer.from_pretrained(path)
10
+ self.pipeline = pipeline("text-classification", model=self.model, tokenizer=self.tokenizer)
11
+
12
+ def __call__(self, data):
13
+ inputs = data.get("inputs") if isinstance(data, dict) else data
14
+ return self.pipeline(inputs)