Boris Ustyugov commited on
Commit
5553133
·
1 Parent(s): bbfb591

model load from hf model repo

Browse files
.gitattributes DELETED
@@ -1,2 +0,0 @@
1
- *.safetensors filter=lfs diff=lfs merge=lfs -text
2
- *.pt filter=lfs diff=lfs merge=lfs -text
 
 
 
.gitignore CHANGED
@@ -1,17 +1,4 @@
1
- # Exclude large training files that aren't needed for inference
2
- model_chekpoint/optimizer.pt
3
- model_chekpoint/rng_state.pth
4
- model_chekpoint/scheduler.pt
5
- model_chekpoint/trainer_state.json
6
- model_chekpoint/training_args.bin
7
- model_chekpoint/model.safetensors
8
-
9
- # Exclude large files in root directory
10
- model.safetensors
11
- optimizer.pt
12
- rng_state.pth
13
- scheduler.pt
14
- trainer_state.json
15
- training_args.bin
16
- tokenizer.json
17
- vocab.txt
 
1
+ .venv/
2
+ __pycache__/
3
+ .idea
4
+ model_checkpoint/
 
 
 
 
 
 
 
 
 
 
 
 
 
app.py CHANGED
@@ -1,11 +1,10 @@
1
  import gradio as gr
2
- import torch
3
- from utils import load_model_from_checkpoint, get_device
4
 
5
 
6
  def predict_sentiment(text):
7
  """
8
- Predict sentiment of the input text using the loaded model.
9
 
10
  Args:
11
  text (str): Input text to analyze
@@ -14,25 +13,16 @@ def predict_sentiment(text):
14
  str: Sentiment prediction
15
  """
16
  try:
17
- # Load model and tokenizer
18
- model, tokenizer = load_model_from_checkpoint()
19
- device = get_device()
20
- model = model.to(device)
21
 
22
- # Tokenize input text
23
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=512)
24
- inputs = {k: v.to(device) for k, v in inputs.items()}
25
 
26
- # Get prediction
27
- with torch.no_grad():
28
- outputs = model(**inputs)
29
- predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
30
- predicted_class = torch.argmax(predictions, dim=-1).item()
31
-
32
- # Map class to sentiment (adjust based on your model's classes)
33
- sentiment_map = {0: "Negative", 1: "Positive", 2: "Neutral"}
34
- sentiment = sentiment_map.get(predicted_class, "Unknown")
35
- confidence = predictions[0][predicted_class].item()
36
 
37
  return f"Sentiment: {sentiment} (Confidence: {confidence:.2f})"
38
 
 
1
  import gradio as gr
2
+ from utils import load_pipeline_from_huggingface
 
3
 
4
 
5
  def predict_sentiment(text):
6
  """
7
+ Predict sentiment of the input text using the loaded pipeline.
8
 
9
  Args:
10
  text (str): Input text to analyze
 
13
  str: Sentiment prediction
14
  """
15
  try:
16
+ # Load pipeline
17
+ sentiment_pipeline = load_pipeline_from_huggingface()
 
 
18
 
19
+ # Get prediction using pipeline
20
+ results = sentiment_pipeline(text)
 
21
 
22
+ # Extract the highest confidence prediction
23
+ best_result = max(results[0], key=lambda x: x['score'])
24
+ sentiment = best_result['label']
25
+ confidence = best_result['score']
 
 
 
 
 
 
26
 
27
  return f"Sentiment: {sentiment} (Confidence: {confidence:.2f})"
28
 
config.json DELETED
@@ -1,37 +0,0 @@
1
- {
2
- "_name_or_path": "ProsusAI/finbert",
3
- "architectures": [
4
- "BertForSequenceClassification"
5
- ],
6
- "attention_probs_dropout_prob": 0.1,
7
- "classifier_dropout": null,
8
- "gradient_checkpointing": false,
9
- "hidden_act": "gelu",
10
- "hidden_dropout_prob": 0.1,
11
- "hidden_size": 768,
12
- "id2label": {
13
- "0": "positive",
14
- "1": "negative",
15
- "2": "neutral"
16
- },
17
- "initializer_range": 0.02,
18
- "intermediate_size": 3072,
19
- "label2id": {
20
- "negative": 1,
21
- "neutral": 2,
22
- "positive": 0
23
- },
24
- "layer_norm_eps": 1e-12,
25
- "max_position_embeddings": 512,
26
- "model_type": "bert",
27
- "num_attention_heads": 12,
28
- "num_hidden_layers": 12,
29
- "pad_token_id": 0,
30
- "position_embedding_type": "absolute",
31
- "problem_type": "single_label_classification",
32
- "torch_dtype": "float32",
33
- "transformers_version": "4.46.3",
34
- "type_vocab_size": 2,
35
- "use_cache": true,
36
- "vocab_size": 30522
37
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
model_chekpoint/config.json DELETED
@@ -1,37 +0,0 @@
1
- {
2
- "_name_or_path": "ProsusAI/finbert",
3
- "architectures": [
4
- "BertForSequenceClassification"
5
- ],
6
- "attention_probs_dropout_prob": 0.1,
7
- "classifier_dropout": null,
8
- "gradient_checkpointing": false,
9
- "hidden_act": "gelu",
10
- "hidden_dropout_prob": 0.1,
11
- "hidden_size": 768,
12
- "id2label": {
13
- "0": "positive",
14
- "1": "negative",
15
- "2": "neutral"
16
- },
17
- "initializer_range": 0.02,
18
- "intermediate_size": 3072,
19
- "label2id": {
20
- "negative": 1,
21
- "neutral": 2,
22
- "positive": 0
23
- },
24
- "layer_norm_eps": 1e-12,
25
- "max_position_embeddings": 512,
26
- "model_type": "bert",
27
- "num_attention_heads": 12,
28
- "num_hidden_layers": 12,
29
- "pad_token_id": 0,
30
- "position_embedding_type": "absolute",
31
- "problem_type": "single_label_classification",
32
- "torch_dtype": "float32",
33
- "transformers_version": "4.46.3",
34
- "type_vocab_size": 2,
35
- "use_cache": true,
36
- "vocab_size": 30522
37
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
model_chekpoint/special_tokens_map.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "cls_token": "[CLS]",
3
- "mask_token": "[MASK]",
4
- "pad_token": "[PAD]",
5
- "sep_token": "[SEP]",
6
- "unk_token": "[UNK]"
7
- }
 
 
 
 
 
 
 
 
model_chekpoint/tokenizer.json DELETED
The diff for this file is too large to render. See raw diff
 
model_chekpoint/tokenizer_config.json DELETED
@@ -1,57 +0,0 @@
1
- {
2
- "added_tokens_decoder": {
3
- "0": {
4
- "content": "[PAD]",
5
- "lstrip": false,
6
- "normalized": false,
7
- "rstrip": false,
8
- "single_word": false,
9
- "special": true
10
- },
11
- "100": {
12
- "content": "[UNK]",
13
- "lstrip": false,
14
- "normalized": false,
15
- "rstrip": false,
16
- "single_word": false,
17
- "special": true
18
- },
19
- "101": {
20
- "content": "[CLS]",
21
- "lstrip": false,
22
- "normalized": false,
23
- "rstrip": false,
24
- "single_word": false,
25
- "special": true
26
- },
27
- "102": {
28
- "content": "[SEP]",
29
- "lstrip": false,
30
- "normalized": false,
31
- "rstrip": false,
32
- "single_word": false,
33
- "special": true
34
- },
35
- "103": {
36
- "content": "[MASK]",
37
- "lstrip": false,
38
- "normalized": false,
39
- "rstrip": false,
40
- "single_word": false,
41
- "special": true
42
- }
43
- },
44
- "clean_up_tokenization_spaces": true,
45
- "cls_token": "[CLS]",
46
- "do_basic_tokenize": true,
47
- "do_lower_case": true,
48
- "mask_token": "[MASK]",
49
- "model_max_length": 512,
50
- "never_split": null,
51
- "pad_token": "[PAD]",
52
- "sep_token": "[SEP]",
53
- "strip_accents": null,
54
- "tokenize_chinese_chars": true,
55
- "tokenizer_class": "BertTokenizer",
56
- "unk_token": "[UNK]"
57
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
model_chekpoint/vocab.txt DELETED
The diff for this file is too large to render. See raw diff
 
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ transformers
4
+ huggingface-hub
5
+ pyyaml
special_tokens_map.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "cls_token": "[CLS]",
3
- "mask_token": "[MASK]",
4
- "pad_token": "[PAD]",
5
- "sep_token": "[SEP]",
6
- "unk_token": "[UNK]"
7
- }
 
 
 
 
 
 
 
 
tokenizer_config.json DELETED
@@ -1,57 +0,0 @@
1
- {
2
- "added_tokens_decoder": {
3
- "0": {
4
- "content": "[PAD]",
5
- "lstrip": false,
6
- "normalized": false,
7
- "rstrip": false,
8
- "single_word": false,
9
- "special": true
10
- },
11
- "100": {
12
- "content": "[UNK]",
13
- "lstrip": false,
14
- "normalized": false,
15
- "rstrip": false,
16
- "single_word": false,
17
- "special": true
18
- },
19
- "101": {
20
- "content": "[CLS]",
21
- "lstrip": false,
22
- "normalized": false,
23
- "rstrip": false,
24
- "single_word": false,
25
- "special": true
26
- },
27
- "102": {
28
- "content": "[SEP]",
29
- "lstrip": false,
30
- "normalized": false,
31
- "rstrip": false,
32
- "single_word": false,
33
- "special": true
34
- },
35
- "103": {
36
- "content": "[MASK]",
37
- "lstrip": false,
38
- "normalized": false,
39
- "rstrip": false,
40
- "single_word": false,
41
- "special": true
42
- }
43
- },
44
- "clean_up_tokenization_spaces": true,
45
- "cls_token": "[CLS]",
46
- "do_basic_tokenize": true,
47
- "do_lower_case": true,
48
- "mask_token": "[MASK]",
49
- "model_max_length": 512,
50
- "never_split": null,
51
- "pad_token": "[PAD]",
52
- "sep_token": "[SEP]",
53
- "strip_accents": null,
54
- "tokenize_chinese_chars": true,
55
- "tokenizer_class": "BertTokenizer",
56
- "unk_token": "[UNK]"
57
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
upload_model.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import upload_folder
2
+
3
+ upload_folder(
4
+ repo_id="utyug1/finbert-finetuned-model",
5
+ folder_path="model_checkpoint",
6
+ repo_type="model",
7
+ )
utils.py CHANGED
@@ -1,16 +1,15 @@
1
  import os
2
  import yaml
3
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
- import torch
5
 
6
 
7
- def load_model_from_checkpoint():
8
  """
9
- Load model from transformers checkpoint.
10
- The checkpoint location is specified by model_path in config.yaml.
11
 
12
  Returns:
13
- tuple: (model, tokenizer) loaded from the checkpoint
14
  """
15
  # Read config to get model_path
16
  config_path = "config.yaml"
@@ -24,32 +23,15 @@ def load_model_from_checkpoint():
24
  if not model_path:
25
  raise ValueError("model_path not found in config.yaml")
26
 
27
- # Check if checkpoint directory exists
28
- if not os.path.exists(model_path):
29
- raise FileNotFoundError(f"Model checkpoint not found at: {model_path}")
30
-
31
- # Load tokenizer and model from the checkpoint
32
  try:
33
- tokenizer = AutoTokenizer.from_pretrained(model_path)
34
- model = AutoModelForSequenceClassification.from_pretrained(model_path)
35
-
36
- # Set model to evaluation mode
37
- model.eval()
38
 
39
- return model, tokenizer
40
 
41
  except Exception as e:
42
- raise RuntimeError(f"Failed to load model from checkpoint {model_path}: {str(e)}")
43
-
44
-
45
- def get_device():
46
- """
47
- Get the appropriate device for model inference.
48
-
49
- Returns:
50
- torch.device: Device to use for inference
51
- """
52
- if torch.cuda.is_available():
53
- return torch.device('cuda')
54
- else:
55
- return torch.device('cpu')
 
1
  import os
2
  import yaml
3
+ from transformers import pipeline
 
4
 
5
 
6
+ def load_pipeline_from_huggingface():
7
  """
8
+ Load sentiment analysis pipeline from Hugging Face repository.
9
+ The repository name is specified by model_path in config.yaml.
10
 
11
  Returns:
12
+ pipeline: Sentiment analysis pipeline loaded from Hugging Face
13
  """
14
  # Read config to get model_path
15
  config_path = "config.yaml"
 
23
  if not model_path:
24
  raise ValueError("model_path not found in config.yaml")
25
 
26
+ # Load pipeline from Hugging Face repository
 
 
 
 
27
  try:
28
+ sentiment_pipeline = pipeline(
29
+ "sentiment-analysis",
30
+ model=model_path,
31
+ return_all_scores=True
32
+ )
33
 
34
+ return sentiment_pipeline
35
 
36
  except Exception as e:
37
+ raise RuntimeError(f"Failed to load pipeline from Hugging Face repository {model_path}: {str(e)}")