206 lines
6.1 KiB
Plaintext
206 lines
6.1 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from flask import Flask, request, jsonify\n",
|
|
"import pickle\n",
|
|
"import pandas as pd\n",
|
|
"from sklearn.feature_extraction.text import TfidfVectorizer\n",
|
|
"from sklearn.svm import SVC\n",
|
|
"from sklearn.metrics import accuracy_score\n",
|
|
"\n",
|
|
"app = Flask(__name__)\n",
|
|
"\n",
|
|
"# Load model dan TF-IDF\n",
|
|
"model_path = \"model_svm.pkl\"\n",
|
|
"tfidf_path = \"tfidf_vectorizer.pkl\"\n",
|
|
"\n",
|
|
"def load_model():\n",
|
|
" with open(model_path, 'rb') as f:\n",
|
|
" model = pickle.load(f)\n",
|
|
" with open(tfidf_path, 'rb') as f:\n",
|
|
" vectorizer = pickle.load(f)\n",
|
|
" return model, vectorizer\n",
|
|
"\n",
|
|
"model, vectorizer = load_model()\n",
|
|
"\n",
|
|
"@app.route('/predict', methods=['POST'])\n",
|
|
"def predict():\n",
|
|
" data = request.json\n",
|
|
" full_text = data.get('full_text', '')\n",
|
|
" if not full_text:\n",
|
|
" return jsonify({\"error\": \"full_text is required\"}), 400\n",
|
|
" \n",
|
|
" text_tfidf = vectorizer.transform([full_text])\n",
|
|
" prediction = model.predict(text_tfidf)[0]\n",
|
|
" return jsonify({\"prediction\": prediction})\n",
|
|
"\n",
|
|
"# @app.route('/train', methods=['POST'])\n",
|
|
"# def train():\n",
|
|
"# data = request.json\n",
|
|
"# df = pd.DataFrame(data)\n",
|
|
"# if 'full_text' not in df.columns or 'polarity' not in df.columns:\n",
|
|
"# return jsonify({\"error\": \"Data must contain 'full_text' and 'polarity'\"}), 400\n",
|
|
" \n",
|
|
"# X = df['full_text']\n",
|
|
"# y = df['polarity']\n",
|
|
" \n",
|
|
"# global vectorizer, model\n",
|
|
"# vectorizer = TfidfVectorizer()\n",
|
|
"# X_tfidf = vectorizer.fit_transform(X)\n",
|
|
" \n",
|
|
"# model = SVC()\n",
|
|
"# model.fit(X_tfidf, y)\n",
|
|
" \n",
|
|
"# # Save model & vectorizer\n",
|
|
"# with open(model_path, 'wb') as f:\n",
|
|
"# pickle.dump(model, f)\n",
|
|
"# with open(tfidf_path, 'wb') as f:\n",
|
|
"# pickle.dump(vectorizer, f)\n",
|
|
" \n",
|
|
"# return jsonify({\"message\": \"Model trained successfully\"})\n",
|
|
"\n",
|
|
"# @app.route('/accuracy', methods=['POST'])\n",
|
|
"# def accuracy():\n",
|
|
"# data = request.json\n",
|
|
"# df = pd.DataFrame(data)\n",
|
|
"# if 'full_text' not in df.columns or 'polarity' not in df.columns:\n",
|
|
"# return jsonify({\"error\": \"Data must contain 'full_text' and 'polarity'\"}), 400\n",
|
|
" \n",
|
|
"# X = df['full_text']\n",
|
|
"# y_true = df['polarity']\n",
|
|
"# X_tfidf = vectorizer.transform(X)\n",
|
|
"# y_pred = model.predict(X_tfidf)\n",
|
|
"# acc = accuracy_score(y_true, y_pred)\n",
|
|
" \n",
|
|
"# return jsonify({\"accuracy\": acc})\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" app.run(debug=True)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Prediksi Sentimen: 0\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import joblib\n",
|
|
"\n",
|
|
"# Load model dan vectorizer\n",
|
|
"model = joblib.load('model_svm.pkl')\n",
|
|
"vectorizer = joblib.load('tfidf_vectorizer.pkl')\n",
|
|
"\n",
|
|
"# Contoh prediksi teks baru\n",
|
|
"new_text = [\"masyarakat tidak suka ikn\"]\n",
|
|
"new_text_tfidf = vectorizer.transform(new_text)\n",
|
|
"\n",
|
|
"# Prediksi sentimen\n",
|
|
"prediction = model.predict(new_text_tfidf)\n",
|
|
"print(f\"Prediksi Sentimen: {prediction[0]}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
" * Serving Flask app '__main__'\n",
|
|
" * Debug mode: on\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
|
|
" * Running on all addresses (0.0.0.0)\n",
|
|
" * Running on http://127.0.0.1:9000\n",
|
|
" * Running on http://192.168.125.140:9000\n",
|
|
"Press CTRL+C to quit\n",
|
|
" * Restarting with stat\n"
|
|
]
|
|
},
|
|
{
|
|
"ename": "SystemExit",
|
|
"evalue": "1",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"An exception has occurred, use %tb to see the full traceback.\n",
|
|
"\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 1\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from flask import Flask, request, jsonify\n",
|
|
"from flask_cors import CORS\n",
|
|
"import joblib\n",
|
|
"\n",
|
|
"app = Flask(__name__)\n",
|
|
"CORS(app)\n",
|
|
"\n",
|
|
"# Load model dan vectorizer\n",
|
|
"model = joblib.load('model_svm.pkl')\n",
|
|
"vectorizer = joblib.load('tfidf_vectorizer.pkl')\n",
|
|
"\n",
|
|
"@app.route('/predict', methods=['POST'])\n",
|
|
"def predict():\n",
|
|
" try:\n",
|
|
" data = request.json\n",
|
|
" text = data.get(\"text\", \"\")\n",
|
|
" \n",
|
|
" if not text:\n",
|
|
" return jsonify({\"error\": \"No text provided\"}), 400\n",
|
|
" \n",
|
|
" # Transformasi teks\n",
|
|
" text_tfidf = vectorizer.transform([text])\n",
|
|
" prediction = model.predict(text_tfidf)\n",
|
|
" \n",
|
|
" return jsonify({\"sentiment\": prediction[0]})\n",
|
|
" except Exception as e:\n",
|
|
" return jsonify({\"error\": str(e)}), 500\n",
|
|
"\n",
|
|
"if __name__ == '__main__':\n",
|
|
" app.run(host='0.0.0.0', port=9000, debug=True)\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|