peace4ever commited on
Commit
378a235
1 Parent(s): b89d49a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -36
app.py CHANGED
@@ -4,51 +4,39 @@ import torch as torch
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
5
 
6
 
7
- import requests
8
- from transformers import pipeline
9
 
10
- # Load the model using its ID or name
11
- model_id_or_name = "peace4ever/roberta-large-finetuned-mongolian_v4" # Update with your model ID or name
12
- classifier = pipeline("sentiment-analysis", model=model_id_or_name)
13
 
14
- # Example usage
15
- result = classifier("I loved Star Wars so much!")
16
- print(result)
17
 
18
 
19
- # API_URL = "https://api-inference.huggingface.co/models/peace4ever/roberta-large-finetuned-mongolian_v4"
20
- #
 
21
 
22
- # def query(payload):
23
- # response = requests.post(API_URL, headers=headers, json=payload)
24
- # return response.json()
25
-
26
- # output = query({
27
- # "inputs": "I like you. I love you",
28
- # })
29
 
 
30
 
31
- # model_name = "peace4ever/roberta-large-finetuned-mongolian_v4"
32
- # tokenizer = AutoTokenizer.from_pretrained(model_name)
33
- # model = AutoModelForSequenceClassification.from_pretrained(model_name)
 
 
34
 
35
- # text = st.text_area("Өгүүлбэр оруулна уу?")
36
- # encoded_input = tokenizer(text, return_tensors="pt")
37
- # output = model(**encoded_input)
38
 
39
- # label_map = {"positive": 0, "negative": 1, "neutral": 2}
40
-
41
- # # Update the model configuration with custom labels
42
- # config = AutoConfig.from_pretrained(model_name)
43
- # config.label2id = {"positive": 0, "negative": 1, "neutral": 2}
44
- # config.id2label = {0: "positive", 1: "negative", 2: "neutral"}
45
- # config.save_pretrained(model_name)
46
-
47
- # predicted_label_id = torch.argmax(output.logits, dim=1).item()
48
- # id2label = model.config.id2label
49
- # predicted_label = id2label[predicted_label_id]
50
-
51
- # print("Predicted Class:", predicted_label)
52
 
53
  # st.json(predicted_label)
54
 
 
4
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
5
 
6
 
7
+ # import requests
8
+ # from transformers import pipeline
9
 
10
+ # # Load the model using its ID or name
11
+ # model_id_or_name = "peace4ever/roberta-large-finetuned-mongolian_v4" # Update with your model ID or name
12
+ # classifier = pipeline("sentiment-analysis", model=model_id_or_name)
13
 
14
+ # # Example usage
15
+ # result = classifier("I loved Star Wars so much!")
16
+ # print(result)
17
 
18
 
19
+ model_name = "peace4ever/roberta-large-finetuned-mongolian_v4"
20
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
21
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
22
 
23
+ text = st.text_area("Өгүүлбэр оруулна уу?")
24
+ encoded_input = tokenizer(text, return_tensors="pt")
25
+ output = model(**encoded_input)
 
 
 
 
26
 
27
+ label_map = {"positive": 0, "negative": 1, "neutral": 2}
28
 
29
+ # Update the model configuration with custom labels
30
+ config = AutoConfig.from_pretrained(model_name)
31
+ config.label2id = {"positive": 0, "negative": 1, "neutral": 2}
32
+ config.id2label = {0: "positive", 1: "negative", 2: "neutral"}
33
+ config.save_pretrained(model_name)
34
 
35
+ predicted_label_id = torch.argmax(output.logits, dim=1).item()
36
+ id2label = model.config.id2label
37
+ predicted_label = id2label[predicted_label_id]
38
 
39
+ print("Predicted Class:", predicted_label)
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # st.json(predicted_label)
42