Update app.py
Browse files
app.py
CHANGED
|
@@ -1,93 +1,109 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Haklısın, parça parça eklemeler yapınca kafa karışması çok normal. Şimdi her şeyi tek bir pakette birleştirelim.
|
| 2 |
+
|
| 3 |
+
Hugging Face'te çalışan, dosya yollarını otomatik düzelten ve büyük/küçük harf hatalarını (Linux kaynaklı) engelleyen en sağlam kodu aşağıya yazıyorum.
|
| 4 |
+
|
| 5 |
+
1. app.py (Tüm Kod)
|
| 6 |
+
Bu kodu kopyala ve Hugging Face'teki app.py dosyasının içine tamamen yapıştır (eskisini sil):
|
| 7 |
+
|
| 8 |
+
Python
|
| 9 |
+
import streamlit as st
|
| 10 |
+
import os
|
| 11 |
+
import numpy as np
|
| 12 |
+
import pickle as pkl
|
| 13 |
+
import tensorflow as tf
|
| 14 |
+
from tensorflow.keras.applications.resnet50 import ResNet50, preprocess_input
|
| 15 |
+
from tensorflow.keras.preprocessing import image
|
| 16 |
+
from tensorflow.keras.layers import GlobalMaxPool2D
|
| 17 |
+
from sklearn.neighbors import NearestNeighbors
|
| 18 |
+
from numpy.linalg import norm
|
| 19 |
+
from PIL import Image
|
| 20 |
+
|
| 21 |
+
# Sayfa tasarımı
|
| 22 |
+
st.set_page_config(page_title="Moda Öneri Sistemi", layout="centered")
|
| 23 |
+
|
| 24 |
+
st.markdown("""
|
| 25 |
+
<style>
|
| 26 |
+
.stTitle, .stSubheader, p { text-align: center; }
|
| 27 |
+
.stImage { display: flex; justify-content: center; }
|
| 28 |
+
</style>
|
| 29 |
+
""", unsafe_allow_html=True)
|
| 30 |
+
|
| 31 |
+
st.title('🛍️ Moda Öneri Sistemi')
|
| 32 |
+
|
| 33 |
+
# Model ve verileri yükle
|
| 34 |
+
@st.cache_resource
|
| 35 |
+
def load_data():
|
| 36 |
+
# ResNet50 modelini hazırla
|
| 37 |
+
base_model = ResNet50(weights='imagenet', include_top=False, input_shape=(224, 224, 3))
|
| 38 |
+
base_model.trainable = False
|
| 39 |
+
model = tf.keras.models.Sequential([base_model, GlobalMaxPool2D()])
|
| 40 |
+
|
| 41 |
+
# Pickle dosyalarını kontrol et (Ana dizinde arar)
|
| 42 |
+
try:
|
| 43 |
+
features = np.array(pkl.load(open('Images_features.pkl', 'rb')))
|
| 44 |
+
filenames = pkl.load(open('filenames.pkl', 'rb'))
|
| 45 |
+
return model, features, filenames
|
| 46 |
+
except FileNotFoundError:
|
| 47 |
+
st.error("HATA: .pkl dosyaları ana dizinde bulunamadı!")
|
| 48 |
+
return None, None, None
|
| 49 |
+
|
| 50 |
+
model, feature_list, filenames = load_data()
|
| 51 |
+
|
| 52 |
+
def extract_features(img_path, model):
|
| 53 |
+
img = image.load_img(img_path, target_size=(224, 224))
|
| 54 |
+
img_array = image.img_to_array(img)
|
| 55 |
+
img_expand_dim = np.expand_dims(img_array, axis=0)
|
| 56 |
+
img_preprocess = preprocess_input(img_expand_dim)
|
| 57 |
+
result = model.predict(img_preprocess).flatten()
|
| 58 |
+
norm_result = result / norm(result)
|
| 59 |
+
return norm_result
|
| 60 |
+
|
| 61 |
+
# Dosya yükleme alanı
|
| 62 |
+
uploaded_file = st.file_uploader("Kıyafet resmi seçin...", type=['jpg', 'png', 'jpeg'])
|
| 63 |
+
|
| 64 |
+
if uploaded_file is not None and model is not None:
|
| 65 |
+
# Seçilen resmi göster
|
| 66 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
| 67 |
+
with col2:
|
| 68 |
+
display_image = Image.open(uploaded_file)
|
| 69 |
+
st.image(display_image, use_container_width=True, caption='Yüklediğiniz Resim')
|
| 70 |
+
|
| 71 |
+
# Geçici olarak kaydet
|
| 72 |
+
temp_path = "temp_upload.jpg"
|
| 73 |
+
with open(temp_path, "wb") as f:
|
| 74 |
+
f.write(uploaded_file.getbuffer())
|
| 75 |
+
|
| 76 |
+
# Benzerleri bul
|
| 77 |
+
with st.spinner('Öneriler hazırlanıyor...'):
|
| 78 |
+
input_features = extract_features(temp_path, model)
|
| 79 |
+
neighbors = NearestNeighbors(n_neighbors=6, algorithm='brute', metric='euclidean')
|
| 80 |
+
neighbors.fit(feature_list)
|
| 81 |
+
distances, indices = neighbors.kneighbors([input_features])
|
| 82 |
+
|
| 83 |
+
st.markdown("---")
|
| 84 |
+
st.subheader('✨ Benzer Ürünler')
|
| 85 |
+
|
| 86 |
+
cols = st.columns(5)
|
| 87 |
+
|
| 88 |
+
# Resim klasörünü tara (Büyük/küçük harf duyarlılığı için)
|
| 89 |
+
image_folder = 'images'
|
| 90 |
+
if os.path.exists(image_folder):
|
| 91 |
+
available_files = os.listdir(image_folder)
|
| 92 |
+
# Dosya adlarını küçük harfe çevirerek bir sözlük oluştur (Hızlı arama için)
|
| 93 |
+
file_map = {f.lower(): f for f in available_files}
|
| 94 |
+
else:
|
| 95 |
+
file_map = {}
|
| 96 |
+
st.error("'images' klasörü bulunamadı!")
|
| 97 |
+
|
| 98 |
+
for i in range(1, 6):
|
| 99 |
+
with cols[i-1]:
|
| 100 |
+
# Pickle içindeki yolu temizle (Windows yollarını Linux'a çevir)
|
| 101 |
+
raw_path = filenames[indices[0][i]].replace('\\', '/')
|
| 102 |
+
target_name = os.path.basename(raw_path).lower()
|
| 103 |
+
|
| 104 |
+
# Klasörde bu resim var mı bak
|
| 105 |
+
if target_name in file_map:
|
| 106 |
+
final_path = os.path.join(image_folder, file_map[target_name])
|
| 107 |
+
st.image(final_path, use_container_width=True)
|
| 108 |
+
else:
|
| 109 |
+
st.warning(f"Bulunamadı: {target_name}")
|