File size: 8,244 Bytes
9b0ae85 69ad306 9b0ae85 69ad306 |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
import gradio as gr
import pandas as pd
def add_model_responses_tab(block, data_manager, available_models):
with gr.Tab("Model Cevapları"):
gr.Markdown("### Model Cevaplarını Göz At")
gr.Markdown("**6.200 soruyu ve model cevaplarını model ve kategoriye göre filtreleyin.**")
# Prepare category choices from actual data
categories = ["Tümü"]
if not data_manager.responses_data.empty:
# Check what category column actually exists
category_col = None
for col in data_manager.responses_data.columns:
if 'kategori' in col.lower() or 'category' in col.lower() or 'bolum' in col.lower():
category_col = col
break
if category_col:
categories += sorted(data_manager.responses_data[category_col].dropna().unique().tolist())
with gr.Row():
model_filter = gr.Dropdown(
choices=["Tümü"] + available_models,
label="Model Filtresi",
value="Tümü"
)
category_filter = gr.Dropdown(
choices=categories,
label="Kategori Filtresi",
value="Tümü"
)
filter_btn = gr.Button("Filtrele", variant="primary")
clear_btn = gr.Button("Temizle")
with gr.Row():
page_size = gr.Slider(
minimum=10,
maximum=100,
value=20,
step=10,
label="Sayfa Başına Gösterim"
)
page_info = gr.HTML(value="<div style='text-align: center; padding: 10px;'>Sayfa 1</div>")
responses_table = gr.DataFrame(
headers=["Soru", "Doğru Cevap", "Model Cevabı", "Kategori"],
datatype=["str", "str", "str", "str"],
col_count=(4, "fixed"),
interactive=False,
wrap=True
)
with gr.Row():
prev_btn = gr.Button("← Önceki", variant="secondary")
next_btn = gr.Button("Sonraki →", variant="secondary")
# State variables
current_page = gr.State(1)
def filter_responses(model, category, page_size_val, page):
try:
df = data_manager.responses_data.copy()
if df.empty:
return pd.DataFrame(), 1, "<div style='text-align: center; padding: 10px;'>Veri bulunamadı</div>"
# Find the actual column names in the dataset
question_col = None
answer_col = None
category_col = None
# Look for question column
for col in df.columns:
if 'soru' in col.lower() or 'question' in col.lower():
question_col = col
break
# Look for answer column
for col in df.columns:
if 'cevap' in col.lower() and not col.endswith('_cevap'):
answer_col = col
break
# Look for category column
for col in df.columns:
if 'kategori' in col.lower() or 'category' in col.lower() or 'bolum' in col.lower():
category_col = col
break
# If we can't find required columns, return empty
if not question_col or not answer_col:
return pd.DataFrame(), 1, "<div style='text-align: center; padding: 10px;'>Gerekli sütunlar bulunamadı</div>"
# Filter by model
if model and model != "Tümü":
model_col = f"{model}_cevap"
if model_col in df.columns:
df = df[df[model_col].notna()]
# Filter by category
if category and category != "Tümü" and category_col:
df = df[df[category_col] == category]
# Prepare display data
display_cols = [question_col, answer_col]
if category_col:
display_cols.append(category_col)
if model and model != "Tümü":
model_col = f"{model}_cevap"
if model_col in df.columns:
display_cols.append(model_col)
df_display = df[display_cols].copy()
df_display.columns = ['Soru', 'Doğru Cevap', 'Kategori', 'Model Cevabı'] if category_col else ['Soru', 'Doğru Cevap', 'Model Cevabı']
else:
df_display = df[display_cols].copy()
df_display['Model Cevabı'] = 'Cevap yok'
df_display.columns = ['Soru', 'Doğru Cevap', 'Kategori', 'Model Cevabı'] if category_col else ['Soru', 'Doğru Cevap', 'Model Cevabı']
else:
# Show first available model response
model_cols = [col for col in df.columns if col.endswith('_cevap')]
if model_cols:
display_cols.append(model_cols[0])
df_display = df[display_cols].copy()
df_display.columns = ['Soru', 'Doğru Cevap', 'Kategori', 'Model Cevabı'] if category_col else ['Soru', 'Doğru Cevap', 'Model Cevabı']
else:
df_display = df[display_cols].copy()
df_display['Model Cevabı'] = 'Cevap yok'
df_display.columns = ['Soru', 'Doğru Cevap', 'Kategori', 'Model Cevabı'] if category_col else ['Soru', 'Doğru Cevap', 'Model Cevabı']
# Pagination
total_rows = len(df_display)
total_pages = (total_rows + page_size_val - 1) // page_size_val
page = max(1, min(page, total_pages))
start_idx = (page - 1) * page_size_val
end_idx = start_idx + page_size_val
page_data = df_display.iloc[start_idx:end_idx]
page_info_text = f"<div style='text-align: center; padding: 10px;'>Sayfa {page}/{total_pages} ({total_rows} sonuç)</div>"
return page_data, page, page_info_text
except Exception as e:
print(f"Filter error: {e}")
return pd.DataFrame(), 1, "<div style='text-align: center; padding: 10px;'>Filtreleme hatası</div>"
def next_page(current_page_val):
return current_page_val + 1
def prev_page(current_page_val):
return max(1, current_page_val - 1)
def clear_filters():
return "Tümü", "Tümü", 1
# Event handlers
filter_btn.click(
filter_responses,
inputs=[model_filter, category_filter, page_size, current_page],
outputs=[responses_table, current_page, page_info]
)
next_btn.click(
next_page,
inputs=[current_page],
outputs=[current_page]
).then(
filter_responses,
inputs=[model_filter, category_filter, page_size, current_page],
outputs=[responses_table, current_page, page_info]
)
prev_btn.click(
prev_page,
inputs=[current_page],
outputs=[current_page]
).then(
filter_responses,
inputs=[model_filter, category_filter, page_size, current_page],
outputs=[responses_table, current_page, page_info]
)
clear_btn.click(
clear_filters,
outputs=[model_filter, category_filter, current_page]
).then(
filter_responses,
inputs=[model_filter, category_filter, page_size, current_page],
outputs=[responses_table, current_page, page_info]
) |