sid22669 commited on
Commit
912ab32
·
1 Parent(s): 84e42b0

Add application file

Browse files
Files changed (4) hide show
  1. Dockerfile +21 -0
  2. app.py +52 -0
  3. requirements.txt +6 -0
  4. templates/index.html +72 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use an official Python runtime as the base image
2
+ FROM python:3.9-slim
3
+
4
+ # Set the working directory in the container
5
+ WORKDIR /app
6
+
7
+ # Copy the application files to the container
8
+ COPY app.py .
9
+ COPY templates/ ./templates/
10
+ COPY model.pkl .
11
+ COPY vectorizer.pkl .
12
+
13
+ # Copy the requirements file and install dependencies
14
+ COPY requirements.txt .
15
+ RUN pip install --no-cache-dir -r requirements.txt
16
+
17
+ # Expose the port the app will run on
18
+ EXPOSE 8080
19
+
20
+ # Command to run the application
21
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, render_template
2
+ import nltk
3
+ from nltk.stem import PorterStemmer
4
+ from nltk.corpus import stopwords
5
+ import string
6
+ import pickle
7
+
8
+ # Initialize the app
9
+ app = Flask(__name__)
10
+
11
+ # Ensure NLTK resources are downloaded
12
+ nltk.download('stopwords')
13
+ nltk.download('punkt')
14
+
15
+ # Load pre-trained model and vectorizer
16
+ with open('model.pkl', 'rb') as file:
17
+ model = pickle.load(file)
18
+
19
+ with open('vectorizer.pkl', 'rb') as file:
20
+ vectorizer = pickle.load(file)
21
+
22
+ # Initialize stop words and stemmer
23
+ stop_words = set(stopwords.words('english'))
24
+ stemmer = PorterStemmer()
25
+
26
+ # Preprocessing function
27
+ def preprocess_text(input_text):
28
+ lowered = input_text.lower()
29
+ translator = str.maketrans('', '', string.punctuation)
30
+ cleaned_text = lowered.translate(translator)
31
+ tokenized_text = nltk.word_tokenize(cleaned_text)
32
+ stop_words_removed = [word for word in tokenized_text if word not in stop_words]
33
+ stemmed = [stemmer.stem(word) for word in stop_words_removed]
34
+ return ' '.join(stemmed)
35
+
36
+ # Route for the HTML form
37
+ @app.route('/')
38
+ def home():
39
+ return render_template('index.html')
40
+
41
+ # Prediction API
42
+ @app.route('/predict', methods=['POST'])
43
+ def predict():
44
+ data = request.json
45
+ input_text = data.get("text", "")
46
+ preprocessed = preprocess_text(input_text)
47
+ prediction = model.predict(vectorizer.transform([preprocessed]))
48
+ return jsonify({"prediction": prediction[0]})
49
+
50
+ # Run the app
51
+ if __name__ == '__main__':
52
+ app.run(host='0.0.0.0', port=8080)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ Flask==2.3.3
2
+ nltk==3.8.1
3
+ numpy==1.25.1
4
+ scikit-learn==1.3.0
5
+ pandas
6
+ pickle
templates/index.html ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!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>Text Classification</title>
7
+ <style>
8
+ body {
9
+ font-family: Arial, sans-serif;
10
+ margin: 50px;
11
+ }
12
+ .container {
13
+ max-width: 500px;
14
+ margin: 0 auto;
15
+ text-align: center;
16
+ }
17
+ input[type="text"] {
18
+ width: 100%;
19
+ padding: 10px;
20
+ margin-bottom: 10px;
21
+ font-size: 16px;
22
+ }
23
+ button {
24
+ padding: 10px 20px;
25
+ font-size: 16px;
26
+ cursor: pointer;
27
+ }
28
+ .result {
29
+ margin-top: 20px;
30
+ font-weight: bold;
31
+ }
32
+ </style>
33
+ </head>
34
+ <body>
35
+ <div class="container">
36
+ <h1>Text Classification</h1>
37
+ <input type="text" id="textInput" placeholder="Enter your text here">
38
+ <button onclick="classifyText()">Classify</button>
39
+ <div id="result" class="result"></div>
40
+ </div>
41
+ <script>
42
+ async function classifyText() {
43
+ const text = document.getElementById('textInput').value;
44
+
45
+ if (!text.trim()) {
46
+ document.getElementById('result').textContent = "Please enter some text!";
47
+ return;
48
+ }
49
+
50
+ try {
51
+ const response = await fetch("/predict", {
52
+ method: "POST",
53
+ headers: {
54
+ "Content-Type": "application/json"
55
+ },
56
+ body: JSON.stringify({ text: text })
57
+ });
58
+
59
+ if (response.ok) {
60
+ const data = await response.json();
61
+ document.getElementById('result').textContent = `Prediction: ${data.prediction}`;
62
+ } else {
63
+ document.getElementById('result').textContent = "Error: Unable to fetch prediction.";
64
+ }
65
+ } catch (error) {
66
+ console.error("Error:", error);
67
+ document.getElementById('result').textContent = "Error: Unable to fetch prediction.";
68
+ }
69
+ }
70
+ </script>
71
+ </body>
72
+ </html>