Update handler.py
Browse files- handler.py +15 -18
handler.py
CHANGED
|
@@ -15,9 +15,9 @@ class EndpointHandler:
|
|
| 15 |
"""
|
| 16 |
Load a simple DistilBERT model for text classification.
|
| 17 |
"""
|
| 18 |
-
model_name = "
|
| 19 |
-
self.tokenizer =
|
| 20 |
-
self.model =
|
| 21 |
self.model.eval() # Set model to evaluation mode (no training)
|
| 22 |
print(f"Loaded model: {model_name}")
|
| 23 |
|
|
@@ -41,21 +41,18 @@ class EndpointHandler:
|
|
| 41 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 42 |
|
| 43 |
# Tokenize input text
|
| 44 |
-
|
| 45 |
|
| 46 |
# Perform inference
|
| 47 |
with torch.no_grad():
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
except Exception as e:
|
| 61 |
-
return {"error": f"Unexpected error: {str(e)}"}
|
|
|
|
| 15 |
"""
|
| 16 |
Load a simple DistilBERT model for text classification.
|
| 17 |
"""
|
| 18 |
+
model_name = "./json_extraction_point_activity" # Pretrained model for sentiment analysis
|
| 19 |
+
self.tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 20 |
+
self.model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 21 |
self.model.eval() # Set model to evaluation mode (no training)
|
| 22 |
print(f"Loaded model: {model_name}")
|
| 23 |
|
|
|
|
| 41 |
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
| 42 |
|
| 43 |
# Tokenize input text
|
| 44 |
+
input_ids = self.tokenizer(user_text, return_tensors="pt").input_ids
|
| 45 |
|
| 46 |
# Perform inference
|
| 47 |
with torch.no_grad():
|
| 48 |
+
output_ids = model.generate(input_ids, max_length=100, temperature=0.3)
|
| 49 |
+
|
| 50 |
+
json_output = tokenizer.decode(output_ids[0], skip_special_tokens=True)
|
| 51 |
+
|
| 52 |
+
try:
|
| 53 |
+
return json.loads(json_output)
|
| 54 |
+
except:
|
| 55 |
+
return json_output
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
return {"error": f"Unexpected error: {str(e)}"}
|
|
|
|
|
|
|
|
|