UQ commited on
Commit
e098e64
·
verified ·
1 Parent(s): fc9b151

Upload 5 files

Browse files

updated the code

Files changed (5) hide show
  1. app.py +50 -0
  2. index.html +29 -0
  3. model.pkl +3 -0
  4. requirements.txt +6 -0
  5. vectorizer.pkl +3 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pickle
3
+
4
+ # Load the model and vectorizer
5
+ with open("model.pkl", "rb") as model_file:
6
+ model = pickle.load(model_file)
7
+
8
+ with open("vectorizer.pkl", "rb") as vectorizer_file:
9
+ vectorizer = pickle.load(vectorizer_file)
10
+
11
+ # Define the prediction function
12
+ def predict_sentiment(text):
13
+ text_vectorized = vectorizer.transform([text])
14
+ prediction = model.predict(text_vectorized)
15
+ sentiment = "Positive" if prediction == 1 else "Negative"
16
+ return sentiment
17
+
18
+
19
+ custom_css = """
20
+ body, .gradio-container, p, h1, h2, h3, h4, h5, h6, label {
21
+ color: black !important;
22
+ }
23
+ body { background-color: #FFF9C4; }
24
+ h1, h2 { color: #333333; font-weight: bold; } /* Dark title */
25
+ .gradio-container { background: #FFF9C4; border-radius: 10px; padding: 20px; } /* Pastel pink container */
26
+ input, textarea { background: #AFCBFF; border: none; border-radius: 5px; } /* Light pastel blue */
27
+ button { background: #B5EAD7; color: black; border: none; border-radius: 8px; padding: 10px; font-size: 16px; } /* Pastel green */
28
+ """
29
+
30
+
31
+ with gr.Blocks(css=custom_css) as app:
32
+ gr.Markdown("<h1 style='text-align:center; color:black;'>Sentiment Analysis 💬</h1>")
33
+ gr.Markdown("<p style='text-align:center; color:black;'>Enter a sentence to check if it's positive or negative.</p>")
34
+
35
+
36
+ with gr.Row():
37
+ input_text = gr.Textbox(label="Your Text", placeholder="Type something here...", lines=2)
38
+ predict_button = gr.Button("Analyze Sentiment 🚀")
39
+
40
+ output_text = gr.Textbox(label="Prediction", interactive=False)
41
+
42
+ predict_button.click(fn=predict_sentiment, inputs=input_text, outputs=output_text)
43
+
44
+ app.launch(mcp_server=True)
45
+
46
+
47
+
48
+
49
+
50
+
index.html ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ html_code = """<!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Sentiment Analysis</title>
7
+ <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
8
+ </head>
9
+ <body>
10
+ <div class="container mt-5">
11
+ <h2 class="text-center">Sentiment Analysis</h2>
12
+ <form method="POST" class="mt-4">
13
+ <div class="mb-3">
14
+ <label for="text" class="form-label">Enter Text:</label>
15
+ <textarea class="form-control" id="text" name="text" rows="3"></textarea>
16
+ </div>
17
+ <button type="submit" class="btn btn-primary">Analyze</button>
18
+ </form>
19
+ {% if sentiment %}
20
+ <div class="alert alert-info mt-3">
21
+ Sentiment: <strong>{{ sentiment }}</strong>
22
+ </div>
23
+ {% endif %}
24
+ </div>
25
+ </body>
26
+ </html>"""
27
+
28
+ with open("templates/index.html", "w") as file:
29
+ file.write(html_code)
model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f0f51438e03adde2314509ffc85a8380320605760fed8442cb68ecc8792df59f
3
+ size 40720
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ scikit-learn
3
+ numpy
4
+ pandas
5
+ pickle5
6
+
vectorizer.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b5563381ce99becddce67c407bbb15bd7ce4e636bebd4d49748f7f2449c37f4c
3
+ size 179012