Spaces:
Build error
Build error
Upload 2 files
Browse files- app.py +62 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,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 |
+
#from datasets import load_dataset
|
7 |
+
|
8 |
+
|
9 |
+
|
10 |
+
# Load the model and define the sentiment classifier
|
11 |
+
MODEL = "LiYuan/amazon-review-sentiment-analysis"
|
12 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL)
|
13 |
+
config = AutoConfig.from_pretrained(MODEL)
|
14 |
+
model = AutoModelForSequenceClassification.from_pretrained(MODEL)
|
15 |
+
pipe = pipeline("sentiment-analysis", model=model, tokenizer=tokenizer, config=config)
|
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 |
+
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
diffusers
|
3 |
+
#transformers
|
4 |
+
git+https://github.com/huggingface/transformers
|
5 |
+
transformers
|
6 |
+
pandas
|