Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +103 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,105 @@
|
|
| 1 |
-
import altair as alt
|
| 2 |
-
import numpy as np
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
|
| 5 |
+
# =========================
|
| 6 |
+
# PAGE CONFIG / SAYFA AYARLARI
|
| 7 |
+
# =========================
|
| 8 |
+
st.set_page_config(page_title="Diamond Predictor", page_icon="💎", layout="wide")
|
| 9 |
+
|
| 10 |
+
# =========================
|
| 11 |
+
# LOAD MODEL / MODELİ YÜKLE
|
| 12 |
+
# =========================
|
| 13 |
+
@st.cache_resource
|
| 14 |
+
def load_model():
|
| 15 |
+
# Model dosyasının adını kontrol et / Check model filename
|
| 16 |
+
model = joblib.load('diamond_catboost_model.pkl')
|
| 17 |
+
return model
|
| 18 |
+
|
| 19 |
+
model = load_model()
|
| 20 |
+
|
| 21 |
+
# =========================
|
| 22 |
+
# HEADER / BAŞLIK
|
| 23 |
+
# =========================
|
| 24 |
+
st.title("💎 Diamond Price Prediction App")
|
| 25 |
+
st.subheader("TR: Elmas Fiyat Tahmini Uygulaması | EN: Diamond Price Prediction Tool")
|
| 26 |
+
st.write("---")
|
| 27 |
+
|
| 28 |
+
# =========================
|
| 29 |
+
# SIDEBAR - INPUTS / YAN PANEL - GİRDİLER
|
| 30 |
+
# =========================
|
| 31 |
+
st.sidebar.header("🔧 Input Features / Girdi Özellikleri")
|
| 32 |
+
|
| 33 |
+
def get_user_inputs():
|
| 34 |
+
carat = st.sidebar.number_input("Carat (Ağırlık)", 0.2, 5.0, 1.0, step=0.01)
|
| 35 |
+
|
| 36 |
+
cut = st.sidebar.selectbox("Cut (Kesim)",
|
| 37 |
+
["Ideal", "Premium", "Very Good", "Good", "Fair"])
|
| 38 |
+
|
| 39 |
+
color = st.sidebar.selectbox("Color (Renk)",
|
| 40 |
+
["D", "E", "F", "G", "H", "I", "J"])
|
| 41 |
+
|
| 42 |
+
clarity = st.sidebar.selectbox("Clarity (Berraklık)",
|
| 43 |
+
["IF", "VVS1", "VVS2", "VS1", "VS2", "SI1", "SI2", "I1"])
|
| 44 |
+
|
| 45 |
+
depth = st.sidebar.slider("Depth (%)", 43.0, 79.0, 61.0)
|
| 46 |
+
table = st.sidebar.slider("Table Width (%)", 43.0, 95.0, 57.0)
|
| 47 |
+
|
| 48 |
+
col1, col2, col3 = st.sidebar.columns(3)
|
| 49 |
+
x = col1.number_input("X (mm)", 0.0, 11.0, 5.0)
|
| 50 |
+
y = col2.number_input("Y (mm)", 0.0, 58.0, 5.0)
|
| 51 |
+
z = col3.number_input("Z (mm)", 0.0, 31.0, 3.0)
|
| 52 |
+
|
| 53 |
+
data = {
|
| 54 |
+
'carat': carat, 'cut': cut, 'color': color, 'clarity': clarity,
|
| 55 |
+
'depth': depth, 'table': table, 'x': x, 'y': y, 'z': z
|
| 56 |
+
}
|
| 57 |
+
return pd.DataFrame([data])
|
| 58 |
+
|
| 59 |
+
input_df = get_user_inputs()
|
| 60 |
+
|
| 61 |
+
# =========================
|
| 62 |
+
# MAIN DISPLAY / ANA EKRAN
|
| 63 |
+
# =========================
|
| 64 |
+
col_main1, col_main2 = st.columns([1, 1])
|
| 65 |
+
|
| 66 |
+
with col_main1:
|
| 67 |
+
st.markdown("### 📋 Selected Features / Seçilen Özellikler")
|
| 68 |
+
st.dataframe(input_df, use_container_width=True)
|
| 69 |
+
|
| 70 |
+
# =========================
|
| 71 |
+
# PREPROCESSING / VERİ ÖN İŞLEME
|
| 72 |
+
# =========================
|
| 73 |
+
# Create dummy variables
|
| 74 |
+
input_encoded = pd.get_dummies(input_df)
|
| 75 |
+
|
| 76 |
+
# Fix 'carat_group_mid' if missing (Modelin beklediği o özel sütun)
|
| 77 |
+
if "carat_group_mid" in model.feature_names_:
|
| 78 |
+
input_encoded["carat_group_mid"] = input_df["carat"].iloc[0]
|
| 79 |
+
|
| 80 |
+
# Align with model features (Modelin beklediği sütun sırasına sok)
|
| 81 |
+
final_df = input_encoded.reindex(columns=model.feature_names_, fill_value=0)
|
| 82 |
+
|
| 83 |
+
# =========================
|
| 84 |
+
# PREDICTION / TAHMİN
|
| 85 |
+
# =========================
|
| 86 |
+
with col_main2:
|
| 87 |
+
st.markdown("### 🎯 Prediction / Tahmin")
|
| 88 |
+
|
| 89 |
+
if st.button("Predict Price / Fiyatı Tahmin Et"):
|
| 90 |
+
prediction = model.predict(final_df)[0]
|
| 91 |
+
|
| 92 |
+
st.balloons()
|
| 93 |
+
st.success(f"💰 Estimated Price / Tahmini Fiyat: **${prediction:,.2f}**")
|
| 94 |
+
|
| 95 |
+
# Additional Info / Ek Bilgi
|
| 96 |
+
st.info("""
|
| 97 |
+
**EN:** This prediction is based on the CatBoost model with 98% accuracy.
|
| 98 |
+
**TR:** Bu tahmin, %98 doğruluk oranına sahip CatBoost modeli tarafından yapılmıştır.
|
| 99 |
+
""")
|
| 100 |
|
| 101 |
+
# =========================
|
| 102 |
+
# FOOTER / ALT BİLGİ
|
| 103 |
+
# =========================
|
| 104 |
+
st.write("---")
|
| 105 |
+
st.caption("Created by Esma | Diamond Price Prediction Project")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|