Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, render_template
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.linear_model import LogisticRegression
|
| 4 |
+
|
| 5 |
+
# Load dataset
|
| 6 |
+
url = "https://raw.githubusercontent.com/sarwansingh/Python/master/ClassExamples/data/iris.csv"
|
| 7 |
+
df = pd.read_csv(url, header=None)
|
| 8 |
+
X = df.iloc[:, :4].values
|
| 9 |
+
y = df.iloc[:, 4].values
|
| 10 |
+
|
| 11 |
+
# Train model
|
| 12 |
+
model = LogisticRegression(max_iter=200)
|
| 13 |
+
model.fit(X, y)
|
| 14 |
+
|
| 15 |
+
# Flask app
|
| 16 |
+
app = Flask(__name__)
|
| 17 |
+
|
| 18 |
+
@app.route("/", methods=["GET", "POST"])
|
| 19 |
+
def home():
|
| 20 |
+
if request.method == "POST":
|
| 21 |
+
try:
|
| 22 |
+
sepal_length = float(request.form["sepal_length"])
|
| 23 |
+
sepal_width = float(request.form["sepal_width"])
|
| 24 |
+
petal_length = float(request.form["petal_length"])
|
| 25 |
+
petal_width = float(request.form["petal_width"])
|
| 26 |
+
|
| 27 |
+
prediction = model.predict([[sepal_length, sepal_width, petal_length, petal_width]])[0]
|
| 28 |
+
return render_template("index.html", prediction_text=f"Predicted Flower: {prediction}")
|
| 29 |
+
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return render_template("index.html", prediction_text=f"Error: {e}")
|
| 32 |
+
|
| 33 |
+
return render_template("index.html", prediction_text="")
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
app.run(host="0.0.0.0", port=7860, debug=True)
|
| 37 |
+
|