pszemraj commited on
Commit
307cdfe
1 Parent(s): 6020c4e

simpler and better example inference

Browse files
Files changed (1) hide show
  1. README.md +10 -27
README.md CHANGED
@@ -28,52 +28,35 @@ this predicts the `ret` column of the training dataset, given the `text` column.
28
 
29
  ```py
30
  import json
31
-
32
- import numpy as np
33
- import torch
34
  from huggingface_hub import hf_hub_download
35
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
36
 
37
- # Define the model repository on Hugging Face Hub
38
  model_repo_name = "pszemraj/deberta-v3-small-sp500-edgar-10k"
 
39
 
40
  # Download the regression_config.json file
41
  regression_config_path = hf_hub_download(
42
  repo_id=model_repo_name, filename="regression_config.json"
43
  )
44
-
45
- # Load regression configuration
46
  with open(regression_config_path, "r") as f:
47
  regression_config = json.load(f)
48
 
49
- # Load the tokenizer and model
50
- tokenizer = AutoTokenizer.from_pretrained(model_repo_name)
51
- model = AutoModelForSequenceClassification.from_pretrained(model_repo_name)
52
-
53
-
54
- # Function to apply inverse scaling to a prediction
55
  def inverse_scale(prediction, config):
 
56
  min_value, max_value = config["min_value"], config["max_value"]
57
  return prediction * (max_value - min_value) + min_value
58
 
 
 
 
 
 
 
59
 
60
- # Example of using the model for inference
61
- def predict(text, tokenizer, model, config, ndigits=4):
62
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
63
- with torch.no_grad():
64
- outputs = model(**inputs)
65
- logits = outputs.logits
66
- predictions = logits.numpy()
67
- # Assuming regression task, apply inverse scaling
68
- scaled_predictions = [inverse_scale(pred[0], config) for pred in predictions]
69
- return round(scaled_predictions[0], ndigits)
70
-
71
-
72
- # Example text
73
  text = "This is an example text for regression prediction."
74
 
75
  # Get predictions
76
- predictions = predict(text, tokenizer, model, regression_config)
77
  print("Predicted Value:", predictions)
78
  ```
79
 
 
28
 
29
  ```py
30
  import json
31
+ from transformers import pipeline
 
 
32
  from huggingface_hub import hf_hub_download
 
33
 
 
34
  model_repo_name = "pszemraj/deberta-v3-small-sp500-edgar-10k"
35
+ pipe = pipeline("text-classification", model=model_repo_name)
36
 
37
  # Download the regression_config.json file
38
  regression_config_path = hf_hub_download(
39
  repo_id=model_repo_name, filename="regression_config.json"
40
  )
 
 
41
  with open(regression_config_path, "r") as f:
42
  regression_config = json.load(f)
43
 
 
 
 
 
 
 
44
  def inverse_scale(prediction, config):
45
+ """apply inverse scaling to a prediction"""
46
  min_value, max_value = config["min_value"], config["max_value"]
47
  return prediction * (max_value - min_value) + min_value
48
 
49
+ def predict_with_pipeline(text, pipe, config, ndigits=4):
50
+ result = pipe(text)[0] # Get the first (and likely only) result
51
+ score = result['score'] if result['label'] == 'LABEL_1' else 1 - result['score']
52
+ # Apply inverse scaling
53
+ scaled_score = inverse_scale(score, config)
54
+ return round(scaled_score, ndigits)
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  text = "This is an example text for regression prediction."
57
 
58
  # Get predictions
59
+ predictions = predict_with_pipeline(text, pipe, regression_config)
60
  print("Predicted Value:", predictions)
61
  ```
62