Update app.py
Browse files
app.py
CHANGED
|
@@ -4,22 +4,36 @@ from scipy import spatial
|
|
| 4 |
import random
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# --- SAYFA
|
| 8 |
st.set_page_config(page_title="CastMatch AI", layout="wide")
|
| 9 |
|
| 10 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@st.cache_resource
|
| 12 |
def load_model():
|
| 13 |
try:
|
| 14 |
with open('movie_model.pkl', 'rb') as f:
|
| 15 |
return pickle.load(f)
|
| 16 |
-
except
|
| 17 |
return None
|
| 18 |
|
| 19 |
model_data = load_model()
|
| 20 |
movie_dict = model_data['movie_dict'] if model_data else {}
|
| 21 |
|
| 22 |
-
#
|
| 23 |
tum_unlu_verileri = {
|
| 24 |
"Sylvester Stallone": {"c": "Male / Erkek", "v": [0.9, 0.2, 0.1, 0.1, 0.1, 0.8]},
|
| 25 |
"Robert De Niro": {"c": "Male / Erkek", "v": [0.2, 0.1, 0.1, 0.9, 0.1, 0.8]},
|
|
@@ -60,102 +74,57 @@ tum_unlu_verileri = {
|
|
| 60 |
"Cameron Diaz": {"c": "Female / Kadın", "v": [0.1, 0.2, 0.95, 0.7, 0.1, 0.1]}
|
| 61 |
}
|
| 62 |
|
| 63 |
-
# ---
|
| 64 |
@st.cache_data
|
| 65 |
-
def
|
| 66 |
-
|
| 67 |
-
files = os.listdir(current_dir)
|
| 68 |
-
return [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
| 69 |
|
| 70 |
-
|
| 71 |
-
available_files = get_available_images()
|
| 72 |
-
mevcut_unlular = {}
|
| 73 |
-
for name, data in tum_unlu_verileri.items():
|
| 74 |
-
search_target = name.lower().replace(" ", "_")
|
| 75 |
-
search_target_alt = name.lower()
|
| 76 |
-
if any(search_target in f.lower() or search_target_alt in f.lower() for f in available_files):
|
| 77 |
-
mevcut_unlular[name] = data
|
| 78 |
-
return mevcut_unlular
|
| 79 |
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
# ---
|
| 83 |
-
st.title("🎬 CastMatch AI
|
| 84 |
-
st.markdown("#### Discover the perfect roles for global stars! / Dünya yıldızları için en ideal rolleri keşfedin!")
|
| 85 |
st.markdown("---")
|
| 86 |
|
| 87 |
-
# SOL PANEL
|
| 88 |
-
st.sidebar.title("🔍 Control Panel / Kontrol Paneli")
|
| 89 |
-
gender_choice = st.sidebar.radio(
|
| 90 |
-
"1. Select Category / Kategori Seçin:",
|
| 91 |
-
["Male / Erkek", "Female / Kadın"],
|
| 92 |
-
key="gender_radio"
|
| 93 |
-
)
|
| 94 |
-
|
| 95 |
-
filtered_names = [name for name, data in unlu_verileri.items() if data['c'] == gender_choice]
|
| 96 |
-
|
| 97 |
-
st.sidebar.markdown("### 📋 Available Cast / Mevcut Oyuncular")
|
| 98 |
-
if not filtered_names:
|
| 99 |
-
st.sidebar.warning("No images found in this category / Bu kategoride resim bulunamadı.")
|
| 100 |
-
else:
|
| 101 |
-
for n in filtered_names:
|
| 102 |
-
st.sidebar.write(f"• {n}")
|
| 103 |
-
|
| 104 |
-
# ANA EKRAN KOLONLARI
|
| 105 |
col_actor, col_match = st.columns([1, 2])
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
with col_actor:
|
| 108 |
-
st.subheader("👤
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
filtered_names if filtered_names else ["Yok"],
|
| 112 |
-
key="actor_select"
|
| 113 |
-
)
|
| 114 |
|
| 115 |
-
# Resim
|
| 116 |
-
|
| 117 |
|
| 118 |
-
if
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
search_target_alt = selected_actor.lower()
|
| 123 |
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
if f.lower().startswith(search_target) or f.lower().startswith(search_target_alt):
|
| 127 |
-
found_file = f
|
| 128 |
-
break
|
| 129 |
-
|
| 130 |
-
with img_container:
|
| 131 |
-
if found_file:
|
| 132 |
-
st.image(found_file, caption=f"Profile: {selected_actor}", use_container_width=True)
|
| 133 |
-
else:
|
| 134 |
-
st.error("Image not found / Resim bulunamadı.")
|
| 135 |
|
| 136 |
with col_match:
|
| 137 |
-
st.subheader("🎯
|
| 138 |
-
st.
|
| 139 |
-
|
| 140 |
-
if st.button("🚀 Match and Recommend / Eşleştir ve Öner", key="match_btn") and selected_actor != "Yok":
|
| 141 |
if movie_dict:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
for i in movie_dict:
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
dist = spatial.distance.cosine(actor_v, movie_dict[i][1][:6]) + noise
|
| 148 |
-
results.append((movie_dict[i][0], dist))
|
| 149 |
-
|
| 150 |
-
top_matches = sorted(results, key=lambda x: x[1])[:5]
|
| 151 |
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
st.
|
| 156 |
-
st.progress(
|
| 157 |
-
else:
|
| 158 |
-
st.error("Model not loaded / Model yüklenemedi.")
|
| 159 |
-
|
| 160 |
-
st.markdown("---")
|
| 161 |
-
st.info("💡 **How it works? / Nasıl çalışır?**: This AI analyzes career vectors based on available local assets. / Bu yapay zeka, mevcut yerel varlıklara dayanarak kariyer vektörlerini analiz eder.")
|
|
|
|
| 4 |
import random
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# --- 1. SAYFA YAPISI VE CSS (Titremeyi Engellemek İçin En Önemli Kısım) ---
|
| 8 |
st.set_page_config(page_title="CastMatch AI", layout="wide")
|
| 9 |
|
| 10 |
+
# Resim kutusunun zıplamasını engelleyen özel CSS
|
| 11 |
+
st.markdown("""
|
| 12 |
+
<style>
|
| 13 |
+
.stImage > img {
|
| 14 |
+
border-radius: 10px;
|
| 15 |
+
max-height: 450px;
|
| 16 |
+
object-fit: cover;
|
| 17 |
+
}
|
| 18 |
+
div[data-testid="stVerticalBlock"] > div:has(div.stImage) {
|
| 19 |
+
min-height: 450px;
|
| 20 |
+
}
|
| 21 |
+
</style>
|
| 22 |
+
""", unsafe_allow_html=True)
|
| 23 |
+
|
| 24 |
+
# --- 2. VERİ VE MODEL (Cache/Önbellek) ---
|
| 25 |
@st.cache_resource
|
| 26 |
def load_model():
|
| 27 |
try:
|
| 28 |
with open('movie_model.pkl', 'rb') as f:
|
| 29 |
return pickle.load(f)
|
| 30 |
+
except:
|
| 31 |
return None
|
| 32 |
|
| 33 |
model_data = load_model()
|
| 34 |
movie_dict = model_data['movie_dict'] if model_data else {}
|
| 35 |
|
| 36 |
+
# Veritabanı (Sabit tutuldu)
|
| 37 |
tum_unlu_verileri = {
|
| 38 |
"Sylvester Stallone": {"c": "Male / Erkek", "v": [0.9, 0.2, 0.1, 0.1, 0.1, 0.8]},
|
| 39 |
"Robert De Niro": {"c": "Male / Erkek", "v": [0.2, 0.1, 0.1, 0.9, 0.1, 0.8]},
|
|
|
|
| 74 |
"Cameron Diaz": {"c": "Female / Kadın", "v": [0.1, 0.2, 0.95, 0.7, 0.1, 0.1]}
|
| 75 |
}
|
| 76 |
|
| 77 |
+
# --- 3. DOSYA TARAMA (Cache/Önbellek) ---
|
| 78 |
@st.cache_data
|
| 79 |
+
def get_images():
|
| 80 |
+
return [f for f in os.listdir(os.getcwd()) if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
|
|
|
|
|
|
| 81 |
|
| 82 |
+
all_images = get_images()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
# Mevcut olanları filtrele
|
| 85 |
+
unlu_verileri = {}
|
| 86 |
+
for n, v in tum_unlu_verileri.items():
|
| 87 |
+
if any(n.lower().replace(" ","_") in f.lower() or n.lower() in f.lower() for f in all_images):
|
| 88 |
+
unlu_verileri[n] = v
|
| 89 |
|
| 90 |
+
# --- 4. ARAYÜZ ---
|
| 91 |
+
st.title("🎬 CastMatch AI")
|
|
|
|
| 92 |
st.markdown("---")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
col_actor, col_match = st.columns([1, 2])
|
| 95 |
|
| 96 |
+
# Sidebar Kategorisi
|
| 97 |
+
gender = st.sidebar.radio("Category / Kategori:", ["Male / Erkek", "Female / Kadın"], key="g_radio")
|
| 98 |
+
names = [n for n, d in unlu_verileri.items() if d['c'] == gender]
|
| 99 |
+
|
| 100 |
with col_actor:
|
| 101 |
+
st.subheader("👤 Cast / Oyuncu")
|
| 102 |
+
# INDEX kullanarak seçimi sabitlemek titremeyi azaltır
|
| 103 |
+
actor = st.selectbox("Select / Seç:", names if names else ["-"], key="act_select")
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# Resim alanı için sabit bir placeholder
|
| 106 |
+
placeholder = st.empty()
|
| 107 |
|
| 108 |
+
if actor != "-":
|
| 109 |
+
target = actor.lower().replace(" ","_")
|
| 110 |
+
target_alt = actor.lower()
|
| 111 |
+
img_file = next((f for f in all_images if f.lower().startswith(target) or f.lower().startswith(target_alt)), None)
|
|
|
|
| 112 |
|
| 113 |
+
if img_file:
|
| 114 |
+
placeholder.image(img_file, use_container_width=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
with col_match:
|
| 117 |
+
st.subheader("🎯 Matches / Eşleşmeler")
|
| 118 |
+
if st.button("🚀 Match / Eşleştir", key="m_btn") and actor != "-":
|
|
|
|
|
|
|
| 119 |
if movie_dict:
|
| 120 |
+
vec = unlu_verileri[actor]['v']
|
| 121 |
+
res = []
|
| 122 |
for i in movie_dict:
|
| 123 |
+
d = spatial.distance.cosine(vec, movie_dict[i][1][:6]) + random.uniform(0, 0.00001)
|
| 124 |
+
res.append((movie_dict[i][0], d))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
|
| 126 |
+
top = sorted(res, key=lambda x: x[1])[:5]
|
| 127 |
+
for i, (f, s) in enumerate(top, 1):
|
| 128 |
+
p = round((1-s)*100, 1)
|
| 129 |
+
st.success(f"**{i}. {f}** (%{p})")
|
| 130 |
+
st.progress(p/100)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|