abhyast123 commited on
Commit
38cc304
1 Parent(s): 8c6f469

load the app with model

Browse files
Files changed (1) hide show
  1. app.py +56 -42
app.py CHANGED
@@ -1,43 +1,57 @@
 
1
  import gradio as gr
2
- from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
- import torch
4
-
5
- # Load the pre-trained model and tokenizer
6
- tokenizer = AutoTokenizer.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
7
- model = AutoModelForSequenceClassification.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
8
-
9
- # Define a function for sentiment analysis
10
- def predict_sentiment(text):
11
- # Tokenize the input text and prepare it to be used by the model
12
- inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
13
-
14
- # Forward pass through the model
15
- with torch.no_grad():
16
- outputs = model(**inputs)
17
-
18
- # Get the predicted probabilities and convert them to percentages
19
- probabilities = torch.softmax(outputs.logits, dim=1).squeeze().tolist()
20
- positive_percent = probabilities[2] * 100
21
- negative_percent = probabilities[0] * 100
22
- neutral_percent = probabilities[1] * 100
23
-
24
- # Construct the result dictionary
25
- result = {
26
- "Positive": round(positive_percent, 2),
27
- "Negative": round(negative_percent, 2),
28
- "Neutral": round(neutral_percent, 2)
29
- }
30
-
31
- return result
32
-
33
- # Define inputs and outputs directly without using gr.inputs or gr.outputs
34
- iface = gr.Interface(
35
- fn=predict_sentiment,
36
- inputs=gr.inputs.Textbox(lines=10, label="Enter financial statement"),
37
- outputs=gr.outputs.Label(num_top_classes=3, label="Sentiment Percentages"),
38
- title="Financial Statement Sentiment Analysis",
39
- description="Predict the sentiment percentages of a financial statement."
40
- )
41
-
42
- if __name__ == "__main__":
43
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline
2
  import gradio as gr
3
+
4
+ #load the model directly
5
+ # Use a pipeline as a high-level helper
6
+ pipe = pipeline("text-classification", model="mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
7
+
8
+
9
+ #run the application
10
+ demo=gr.Interface.from_pipeline(pipe)
11
+ demo.launch()
12
+
13
+
14
+
15
+ # import gradio as gr
16
+ # from transformers import AutoModelForSequenceClassification, AutoTokenizer
17
+ # import torch
18
+
19
+ # # Load the pre-trained model and tokenizer
20
+ # tokenizer = AutoTokenizer.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
21
+ # model = AutoModelForSequenceClassification.from_pretrained("mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis")
22
+
23
+ # # Define a function for sentiment analysis
24
+ # def predict_sentiment(text):
25
+ # # Tokenize the input text and prepare it to be used by the model
26
+ # inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
27
+
28
+ # # Forward pass through the model
29
+ # with torch.no_grad():
30
+ # outputs = model(**inputs)
31
+
32
+ # # Get the predicted probabilities and convert them to percentages
33
+ # probabilities = torch.softmax(outputs.logits, dim=1).squeeze().tolist()
34
+ # positive_percent = probabilities[2] * 100
35
+ # negative_percent = probabilities[0] * 100
36
+ # neutral_percent = probabilities[1] * 100
37
+
38
+ # # Construct the result dictionary
39
+ # result = {
40
+ # "Positive": round(positive_percent, 2),
41
+ # "Negative": round(negative_percent, 2),
42
+ # "Neutral": round(neutral_percent, 2)
43
+ # }
44
+
45
+ # return result
46
+
47
+ # # Define inputs and outputs directly without using gr.inputs or gr.outputs
48
+ # iface = gr.Interface(
49
+ # fn=predict_sentiment,
50
+ # inputs=gr.inputs.Textbox(lines=10, label="Enter financial statement"),
51
+ # outputs=gr.outputs.Label(num_top_classes=3, label="Sentiment Percentages"),
52
+ # title="Financial Statement Sentiment Analysis",
53
+ # description="Predict the sentiment percentages of a financial statement."
54
+ # )
55
+
56
+ # if __name__ == "__main__":
57
+ # iface.launch()