ellyothim commited on
Commit
9245d3d
1 Parent(s): a1d1763

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from transformers import AutoModelForSequenceClassification
4
+ from transformers import TFAutoModelForSequenceClassification
5
+ from transformers import AutoTokenizer, AutoConfig
6
+ import numpy as np
7
+ from scipy.special import softmax
8
+
9
+
10
+ # setting up the requiremnts
11
+
12
+ model_path = f"mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis"
13
+ tokenizer = AutoTokenizer.from_pretrained('mrm8488/distilroberta-finetuned-financial-news-sentiment-analysis')
14
+ config = AutoConfig.from_pretrained(model_path)
15
+ model = AutoModelForSequenceClassification.from_pretrained(model_path)
16
+
17
+ # Preprocess text (username and link placeholders)
18
+ def preprocess(text):
19
+ new_text = []
20
+ for t in text.split(" "):
21
+ t = '@user' if t.startswith('@') and len(t) > 1 else t
22
+ t = 'http' if t.startswith('http') else t
23
+ new_text.append(t)
24
+ return " ".join(new_text)
25
+
26
+ # Defining the main function
27
+ def sentiment_analysis(text):
28
+ text = preprocess(text)
29
+
30
+ # PyTorch-based models
31
+ encoded_input = tokenizer(text, return_tensors='pt')
32
+ output = model(**encoded_input)
33
+ scores_ = output[0][0].detach().numpy()
34
+ scores_ = softmax(scores_)
35
+
36
+ # Format output dict of scores
37
+ labels = ['Negative😢😢', 'Neutral', 'Positive😃😃']
38
+ scores = {l:float(s) for (l,s) in zip(labels, scores_) }
39
+
40
+ return scores
41
+
42
+ welcome_message = "Welcome to Team Paris tweets first shot Sentimental Analysis App 😃 😃 😃 😃 "
43
+ demo = gr.Interface(
44
+ fn=sentiment_analysis,
45
+ inputs=gr.Textbox(placeholder="Write your tweet here..."),
46
+ outputs="label",
47
+ interpretation="default",
48
+ examples=[["This is wonderful!"]],
49
+ title=welcome_message
50
+ )
51
+ demo.launch()
52
+ # def greet(name):
53
+ # return "Hello " + name + "!!"
54
+
55
+ # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
56
+ # iface.launch(inline = False)