EmmaL1 commited on
Commit
9a30469
·
verified ·
1 Parent(s): 1be0b82

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import part
2
+ import streamlit as st
3
+ from transformers import pipeline
4
+ import numpy as np # Import numpy
5
+
6
+ # function part
7
+ # text classification
8
+ def textclassification():
9
+ sentiment_pipeline = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
10
+
11
+ st.title("Sentiment Analysis with HuggingFace Spaces")
12
+ st.write("Enter a sentence to analyze its sentiment:")
13
+
14
+ user_input = st.text_input("")
15
+ if user_input:
16
+ result = sentiment_pipeline(user_input)
17
+ sentiment = result[0]["label"]
18
+ confidence = result[0]["score"]
19
+
20
+ st.write(f"Sentiment: {sentiment}")
21
+ st.write(f"Confidence: {confidence:.2f}")
22
+
23
+ # Determine the rating based on sentiment
24
+ if sentiment == "POSITIVE":
25
+ rating = 5 # Assuming positive sentiment corresponds to a higher rating
26
+ else:
27
+ rating = 1 # Assuming negative sentiment corresponds to a lower rating
28
+
29
+ # Print the rating
30
+ st.write(f"The rating is {rating} stars") # Use Streamlit to display instead of print
31
+
32
+ def main():
33
+ textclassification()
34
+
35
+ if __name__ == "__main__":
36
+ main()