Spaces:
Build error
Build error
Addind appilcation file
Browse files- app.py +35 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
def classifier(sentence):
|
| 5 |
+
classifier = pipeline(
|
| 6 |
+
"text-classification",
|
| 7 |
+
model="AirrStorm/DistilBERT-SST2",
|
| 8 |
+
tokenizer="AirrStorm/DistilBERT-SST2",
|
| 9 |
+
device=0
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
label_mapping = {"LABEL_0": "negative", "LABEL_1": "positive"}
|
| 13 |
+
result = classifier(sentence)
|
| 14 |
+
predicted_label = label_mapping[result[0]['label']]
|
| 15 |
+
return predicted_label # Should print "negative" or "positive"
|
| 16 |
+
|
| 17 |
+
# Define the Gradio Interface
|
| 18 |
+
demo = gr.Interface(
|
| 19 |
+
fn=classifier,
|
| 20 |
+
inputs=gr.Textbox(
|
| 21 |
+
lines=4,
|
| 22 |
+
placeholder="Type a sentence to classify the sentiment...",
|
| 23 |
+
label="Input Text"
|
| 24 |
+
),
|
| 25 |
+
outputs=gr.Textbox(
|
| 26 |
+
label="Predicted Sentiment"
|
| 27 |
+
),
|
| 28 |
+
title="Sentiment Analysis",
|
| 29 |
+
description="Classify the sentiment of the input text as positive or negative.",
|
| 30 |
+
theme="hugging-face", # Optional, you can experiment with other themes like 'huggingface'
|
| 31 |
+
allow_flagging="never", # Disable flagging if not needed
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Launch the interface
|
| 35 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
torch
|
| 3 |
+
SentencePiece
|
| 4 |
+
sacremoses
|