abdullah4 commited on
Commit
dbd48b2
·
1 Parent(s): f96e849

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -0
app.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from sklearn.feature_extraction.text import TfidfVectorizer
4
+ from sklearn.linear_model import LogisticRegression
5
+
6
+ # Load and preprocess the data
7
+ # Replace 'your_dataset.csv' with the actual file path
8
+ data = pd.read_csv('dataset.csv')
9
+ vectorizer = TfidfVectorizer()
10
+ X = vectorizer.fit_transform(data['text'])
11
+ y = data['label']
12
+
13
+ # Train the model
14
+ classifier = LogisticRegression()
15
+ classifier.fit(X, y)
16
+
17
+ # Define the prediction function
18
+ def predict(text):
19
+ text_vectorized = vectorizer.transform([text])
20
+ prediction = classifier.predict(text_vectorized)[0]
21
+ if prediction == 'AI':
22
+ score = classifier.predict_proba(text_vectorized)[0][0]
23
+ else:
24
+ score = 1 - classifier.predict_proba(text_vectorized)[0][1]
25
+ response = [
26
+ {
27
+ 'label': prediction,
28
+ 'score': round(float(score), 4)
29
+ }
30
+ ]
31
+ return response
32
+
33
+ # Streamlit app
34
+ st.title("Sentiment Analysis App")
35
+
36
+ # Text input for prediction
37
+ text = st.text_area("Enter some text")
38
+
39
+ # Perform prediction if text is provided
40
+ if text:
41
+ result = predict(text)
42
+ st.json(result)