716this commited on
Commit
6838471
1 Parent(s): 54c9e8a

apply black formatting and add doc strings

Browse files
Files changed (2) hide show
  1. .gitignore +1 -0
  2. app.py +40 -34
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ /venv
app.py CHANGED
@@ -1,10 +1,10 @@
1
- #import csv
2
  import gradio as gr
3
  import pandas as pd
4
  from transformers import pipeline
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
6
- #from datasets import load_dataset
7
 
 
8
 
9
 
10
  # Load the model and define the sentiment classifier
@@ -16,47 +16,53 @@ pipe = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer, config=c
16
 
17
 
18
  def classify_sentiment(sentences):
19
- # Classify the sentiment of each sentence
20
- predictions = pipe(sentences)
21
-
22
- # Extract the predicted labels and confidence scores from the predictions
23
- labels = [prediction['label'] for prediction in predictions]
24
- confidences = [prediction['score'] for prediction in predictions]
25
-
26
- return labels, confidences
 
 
 
27
 
28
  def classify_sentiment_from_csv(csv_file):
29
- # Read the CSV file and extract the list of sentences
30
- df = pd.read_csv(csv_file.name, delimiter=",")
31
- sentences = df['sentence'].tolist()
32
-
33
- # Classify the sentiment of the sentences
34
- labels, confidences = classify_sentiment(sentences)
35
- df['confidences'] = confidences
36
- df['labels'] = labels
37
- return df
38
-
39
- # Define the gradio app
 
 
40
  def main():
41
- iface = gr.Interface(fn=classify_sentiment_from_csv,
42
- inputs=gr.File(),
43
- outputs=gr.Dataframe(),
44
- live=True,
45
- #capture_session=True,
46
- allow_flagging='never')
47
-
48
- iface.launch(enable_queue=False)
 
 
 
49
 
 
50
 
51
 
52
- #debug:
53
  # labels, confidence = classify_sentiment_from_csv("./reviews.csv")
54
  # print(labels)
55
 
56
 
57
-
58
  # Run the gradio app
59
  if __name__ == "__main__":
60
- main()
61
-
62
-
 
1
+ # import csv
2
  import gradio as gr
3
  import pandas as pd
4
  from transformers import pipeline
5
  from transformers import AutoTokenizer, AutoModelForSequenceClassification, AutoConfig
 
6
 
7
+ # from datasets import load_dataset
8
 
9
 
10
  # Load the model and define the sentiment classifier
 
16
 
17
 
18
  def classify_sentiment(sentences):
19
+ """
20
+ Classify the sentiment of each sentence
21
+ """
22
+ predictions = pipe(sentences)
23
+
24
+ # Extract the predicted labels and confidence scores from the predictions
25
+ labels = [prediction["label"] for prediction in predictions]
26
+ confidences = [prediction["score"] for prediction in predictions]
27
+
28
+ return labels, confidences
29
+
30
 
31
  def classify_sentiment_from_csv(csv_file):
32
+ """
33
+ Read the CSV file and extract the list of sentences
34
+ """
35
+ df = pd.read_csv(csv_file.name, delimiter=",")
36
+ sentences = df["sentence"].tolist()
37
+
38
+ # Classify the sentiment of the sentences
39
+ labels, confidences = classify_sentiment(sentences)
40
+ df["confidences"] = confidences
41
+ df["labels"] = labels
42
+ return df
43
+
44
+
45
  def main():
46
+ """
47
+ Define the gradio app
48
+ """
49
+ iface = gr.Interface(
50
+ fn=classify_sentiment_from_csv,
51
+ inputs=gr.File(),
52
+ outputs=gr.Dataframe(),
53
+ live=True,
54
+ # capture_session=True,
55
+ allow_flagging="never",
56
+ )
57
 
58
+ iface.launch(enable_queue=False)
59
 
60
 
61
+ # debug:
62
  # labels, confidence = classify_sentiment_from_csv("./reviews.csv")
63
  # print(labels)
64
 
65
 
 
66
  # Run the gradio app
67
  if __name__ == "__main__":
68
+ main()