abhyast123 commited on
Commit
f87ccc3
1 Parent(s): 918f45e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+ import torch
4
+
5
+ # Load the pre-trained model and tokenizer
6
+ model_name = "distilbert-base-uncased" # Replace this with the desired model
7
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
9
+
10
+ # Define a function for sentiment analysis
11
+ def predict_sentiment(text):
12
+ # Tokenize the input text and prepare it to be used by the model
13
+ inputs = tokenizer(text, return_tensors="pt", padding=True, truncation=True)
14
+
15
+ # Forward pass through the model
16
+ with torch.no_grad():
17
+ outputs = model(**inputs)
18
+
19
+ # Get the predicted probabilities and convert them to percentages
20
+ probabilities = torch.softmax(outputs.logits, dim=1).squeeze().tolist()
21
+ positive_percent = probabilities[2] * 100
22
+ negative_percent = probabilities[0] * 100
23
+ neutral_percent = probabilities[1] * 100
24
+
25
+ # Construct the result dictionary
26
+ result = {
27
+ "Positive": round(positive_percent, 2),
28
+ "Negative": round(negative_percent, 2),
29
+ "Neutral": round(neutral_percent, 2)
30
+ }
31
+
32
+ return result
33
+
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()