Spaces:
Build error
Build error
apply black formatting and add doc strings
Browse files- .gitignore +1 -0
- 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 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
27 |
|
28 |
def classify_sentiment_from_csv(csv_file):
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
40 |
def main():
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
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 |
-
|
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()
|
|
|
|