Ahtisham1583 commited on
Commit
5989b9d
·
verified ·
1 Parent(s): 4e8cdb4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
3
+
4
+ # Replace with your actual model ID or access token from Hugging Face Hub
5
+ model_id = "your-model-id"
6
+
7
+ # Load the pre-trained sentiment analysis model and tokenizer from Hugging Face Hub
8
+ model = AutoModelForSequenceClassification.from_pretrained(model_id)
9
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
10
+
11
+ def classify_sentiment(text):
12
+ """
13
+ Function to preprocess text, make predictions using the loaded model,
14
+ and return the predicted sentiment.
15
+ """
16
+ # Preprocess text (tokenization)
17
+ encoded_text = tokenizer(text, return_tensors="pt")
18
+
19
+ # Make prediction using the loaded model
20
+ output = model(**encoded_text)
21
+ predictions = output.logits.argmax(-1)
22
+
23
+ # Map predicted class label to sentiment category
24
+ sentiment_mapping = {0: "Negative", 1: "Neutral", 2: "Positive"}
25
+ sentiment = sentiment_mapping[predictions.item()]
26
+
27
+ return sentiment
28
+
29
+ def main():
30
+ """
31
+ Streamlit app for sentiment analysis
32
+ """
33
+ st.title("Sentiment Analysis App")
34
+ text_input = st.text_input("Enter text to analyze:")
35
+
36
+ if text_input:
37
+ sentiment = classify_sentiment(text_input)
38
+ st.write(f"Predicted Sentiment: {sentiment}")
39
+
40
+ if __name__ == "__main__":
41
+ main()