Boris Ustyugov commited on
Commit
44cfbeb
·
1 Parent(s): c455085

Add model files and exclude large training files with .gitignore

Browse files
.gitattributes DELETED
@@ -1,35 +0,0 @@
1
- *.7z filter=lfs diff=lfs merge=lfs -text
2
- *.arrow filter=lfs diff=lfs merge=lfs -text
3
- *.bin filter=lfs diff=lfs merge=lfs -text
4
- *.bz2 filter=lfs diff=lfs merge=lfs -text
5
- *.ckpt filter=lfs diff=lfs merge=lfs -text
6
- *.ftz filter=lfs diff=lfs merge=lfs -text
7
- *.gz filter=lfs diff=lfs merge=lfs -text
8
- *.h5 filter=lfs diff=lfs merge=lfs -text
9
- *.joblib filter=lfs diff=lfs merge=lfs -text
10
- *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
- *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
- *.model filter=lfs diff=lfs merge=lfs -text
13
- *.msgpack filter=lfs diff=lfs merge=lfs -text
14
- *.npy filter=lfs diff=lfs merge=lfs -text
15
- *.npz filter=lfs diff=lfs merge=lfs -text
16
- *.onnx filter=lfs diff=lfs merge=lfs -text
17
- *.ot filter=lfs diff=lfs merge=lfs -text
18
- *.parquet filter=lfs diff=lfs merge=lfs -text
19
- *.pb filter=lfs diff=lfs merge=lfs -text
20
- *.pickle filter=lfs diff=lfs merge=lfs -text
21
- *.pkl filter=lfs diff=lfs merge=lfs -text
22
- *.pt filter=lfs diff=lfs merge=lfs -text
23
- *.pth filter=lfs diff=lfs merge=lfs -text
24
- *.rar filter=lfs diff=lfs merge=lfs -text
25
- *.safetensors filter=lfs diff=lfs merge=lfs -text
26
- saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
- *.tar.* filter=lfs diff=lfs merge=lfs -text
28
- *.tar filter=lfs diff=lfs merge=lfs -text
29
- *.tflite filter=lfs diff=lfs merge=lfs -text
30
- *.tgz filter=lfs diff=lfs merge=lfs -text
31
- *.wasm filter=lfs diff=lfs merge=lfs -text
32
- *.xz filter=lfs diff=lfs merge=lfs -text
33
- *.zip filter=lfs diff=lfs merge=lfs -text
34
- *.zst filter=lfs diff=lfs merge=lfs -text
35
- *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
.gitignore ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
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
README.md CHANGED
@@ -10,4 +10,4 @@ pinned: false
10
  short_description: ProsusAI/finbert finetuned just for test
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
10
  short_description: ProsusAI/finbert finetuned just for test
11
  ---
12
 
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-references
app.py CHANGED
@@ -1,7 +1,58 @@
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
12
+
13
+ Returns:
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
+
39
+ except Exception as e:
40
+ return f"Error: {str(e)}"
41
+
42
+
43
+ # Create Gradio interface
44
+ demo = gr.Interface(
45
+ fn=predict_sentiment,
46
+ inputs="text",
47
+ outputs="text",
48
+ title="Financial Sentiment Analysis",
49
+ description="Enter financial text to analyze sentiment using the finetuned FinBERT model.",
50
+ examples=[
51
+ "The stock market is performing well today.",
52
+ "The company's earnings report was disappointing.",
53
+ "Investors are optimistic about the future prospects."
54
+ ]
55
+ )
56
+
57
+ if __name__ == "__main__":
58
+ demo.launch()
config.yaml ADDED
@@ -0,0 +1 @@
 
 
1
+ model_path: "model_checkpoint"
model_chekpoint/config.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
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 ADDED
The diff for this file is too large to render. See raw diff
 
model_chekpoint/tokenizer_config.json ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 ADDED
The diff for this file is too large to render. See raw diff
 
utils.py ADDED
@@ -0,0 +1,55 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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"
17
+ if not os.path.exists(config_path):
18
+ raise FileNotFoundError(f"Config file not found: {config_path}")
19
+
20
+ with open(config_path, 'r') as f:
21
+ config = yaml.safe_load(f)
22
+
23
+ model_path = config.get('model_path')
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')