Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,7 +31,7 @@ def load_model():
|
|
| 31 |
adapter_id = "tugrulkaya/GeoQwen-VL-2B-EuroSAT"
|
| 32 |
|
| 33 |
if DEVICE == "cuda":
|
| 34 |
-
# GPU
|
| 35 |
bnb_config = BitsAndBytesConfig(
|
| 36 |
load_in_4bit=True,
|
| 37 |
bnb_4bit_quant_type="nf4",
|
|
@@ -42,29 +42,27 @@ def load_model():
|
|
| 42 |
quantization_config=bnb_config,
|
| 43 |
device_map="auto",
|
| 44 |
trust_remote_code=True,
|
| 45 |
-
_attn_implementation="flash_attention_2"
|
| 46 |
)
|
| 47 |
else:
|
| 48 |
-
# CPU
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
|
| 52 |
base_model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 53 |
model_id,
|
| 54 |
torch_dtype=torch.float32,
|
| 55 |
-
device_map="auto",
|
| 56 |
-
offload_folder="offload",
|
| 57 |
trust_remote_code=True,
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
)
|
| 61 |
|
| 62 |
# LoRA Adaptörünü Yükle
|
|
|
|
| 63 |
model = PeftModel.from_pretrained(
|
| 64 |
base_model,
|
| 65 |
-
adapter_id
|
| 66 |
-
offload_folder="offload" if DEVICE == "cpu" else None
|
| 67 |
)
|
|
|
|
|
|
|
| 68 |
model.eval()
|
| 69 |
|
| 70 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
|
@@ -72,10 +70,12 @@ def load_model():
|
|
| 72 |
return model, processor
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
-
print(f"❌ Model yükleme
|
|
|
|
|
|
|
| 76 |
raise e
|
| 77 |
|
| 78 |
-
# Global değişkenler
|
| 79 |
model, processor = load_model()
|
| 80 |
|
| 81 |
# --- SINIFLANDIRMA FONKSİYONU ---
|
|
@@ -84,7 +84,6 @@ def classify_satellite_image(image):
|
|
| 84 |
return "⚠️ Lütfen bir görüntü yükleyin.", ""
|
| 85 |
|
| 86 |
try:
|
| 87 |
-
# Görüntü kontrolü
|
| 88 |
if not isinstance(image, Image.Image):
|
| 89 |
image = Image.fromarray(image)
|
| 90 |
|
|
@@ -99,7 +98,7 @@ def classify_satellite_image(image):
|
|
| 99 |
}
|
| 100 |
]
|
| 101 |
|
| 102 |
-
#
|
| 103 |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 104 |
image_inputs, video_inputs = process_vision_info(messages)
|
| 105 |
|
|
@@ -119,6 +118,7 @@ def classify_satellite_image(image):
|
|
| 119 |
do_sample=False
|
| 120 |
)
|
| 121 |
|
|
|
|
| 122 |
generated_ids_trimmed = [
|
| 123 |
out_ids[len(in_ids):]
|
| 124 |
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
|
@@ -130,7 +130,7 @@ def classify_satellite_image(image):
|
|
| 130 |
clean_up_tokenization_spaces=False
|
| 131 |
)[0].strip()
|
| 132 |
|
| 133 |
-
#
|
| 134 |
clean_result = result.replace('.', '').strip()
|
| 135 |
|
| 136 |
if clean_result in CLASS_DESCRIPTIONS:
|
|
@@ -138,9 +138,8 @@ def classify_satellite_image(image):
|
|
| 138 |
display_text = f"### 🎯 Sonuç: {formatted_result}\n\n**Orijinal Sınıf:** `{clean_result}`"
|
| 139 |
else:
|
| 140 |
display_text = f"### 🤖 Model Çıktısı: {result}"
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
return display_text, clean_result
|
| 144 |
|
| 145 |
except Exception as e:
|
| 146 |
return f"❌ Hata: {str(e)}", "Error"
|
|
@@ -160,27 +159,10 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 160 |
input_image = gr.Image(label="Uydu Görüntüsü Yükle", type="pil", height=300)
|
| 161 |
classify_btn = gr.Button("🔍 Sınıflandır", variant="primary", size="lg")
|
| 162 |
|
| 163 |
-
# Örnek sınıfları göster
|
| 164 |
-
gr.Examples(
|
| 165 |
-
examples=[], # Buraya örnek resim yolları eklenebilir
|
| 166 |
-
inputs=input_image
|
| 167 |
-
)
|
| 168 |
-
|
| 169 |
with gr.Column():
|
| 170 |
output_text = gr.Markdown(label="Analiz Sonucu", value="*Görüntü bekleniyor...*")
|
| 171 |
output_raw = gr.Textbox(label="Ham Çıktı", interactive=False)
|
| 172 |
|
| 173 |
gr.HTML("""
|
| 174 |
<div style="margin-top: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 5px;">
|
| 175 |
-
<p style="margin:0"><b>Not:</b>
|
| 176 |
-
</div>
|
| 177 |
-
""")
|
| 178 |
-
|
| 179 |
-
classify_btn.click(
|
| 180 |
-
fn=classify_satellite_image,
|
| 181 |
-
inputs=[input_image],
|
| 182 |
-
outputs=[output_text, output_raw]
|
| 183 |
-
)
|
| 184 |
-
|
| 185 |
-
if __name__ == "__main__":
|
| 186 |
-
demo.launch()
|
|
|
|
| 31 |
adapter_id = "tugrulkaya/GeoQwen-VL-2B-EuroSAT"
|
| 32 |
|
| 33 |
if DEVICE == "cuda":
|
| 34 |
+
# GPU Varsa: 4-bit quantization ile yükle
|
| 35 |
bnb_config = BitsAndBytesConfig(
|
| 36 |
load_in_4bit=True,
|
| 37 |
bnb_4bit_quant_type="nf4",
|
|
|
|
| 42 |
quantization_config=bnb_config,
|
| 43 |
device_map="auto",
|
| 44 |
trust_remote_code=True,
|
| 45 |
+
_attn_implementation="flash_attention_2"
|
| 46 |
)
|
| 47 |
else:
|
| 48 |
+
# CPU (Hugging Face Spaces Free Tier):
|
| 49 |
+
# 'offload_folder' ve 'device_map="auto"' KALDIRILDI.
|
| 50 |
+
# 2B model RAM'e (16GB) rahatça sığar. Diske taşımak hataya sebep oluyor.
|
|
|
|
| 51 |
base_model = Qwen2VLForConditionalGeneration.from_pretrained(
|
| 52 |
model_id,
|
| 53 |
torch_dtype=torch.float32,
|
|
|
|
|
|
|
| 54 |
trust_remote_code=True,
|
| 55 |
+
_attn_implementation="eager" # CPU için kritik
|
| 56 |
+
).to("cpu")
|
|
|
|
| 57 |
|
| 58 |
# LoRA Adaptörünü Yükle
|
| 59 |
+
# Burada da offload_folder argümanını kaldırdık
|
| 60 |
model = PeftModel.from_pretrained(
|
| 61 |
base_model,
|
| 62 |
+
adapter_id
|
|
|
|
| 63 |
)
|
| 64 |
+
|
| 65 |
+
# Modeli değerlendirme moduna al
|
| 66 |
model.eval()
|
| 67 |
|
| 68 |
processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=True)
|
|
|
|
| 70 |
return model, processor
|
| 71 |
|
| 72 |
except Exception as e:
|
| 73 |
+
print(f"❌ Model yükleme hatası detaylı: {str(e)}")
|
| 74 |
+
# Hatayı gradio ekranında da görmek için tekrar fırlatabiliriz ama
|
| 75 |
+
# uygulamanın çökmemesi için loglayıp devam ediyoruz.
|
| 76 |
raise e
|
| 77 |
|
| 78 |
+
# Global değişkenler
|
| 79 |
model, processor = load_model()
|
| 80 |
|
| 81 |
# --- SINIFLANDIRMA FONKSİYONU ---
|
|
|
|
| 84 |
return "⚠️ Lütfen bir görüntü yükleyin.", ""
|
| 85 |
|
| 86 |
try:
|
|
|
|
| 87 |
if not isinstance(image, Image.Image):
|
| 88 |
image = Image.fromarray(image)
|
| 89 |
|
|
|
|
| 98 |
}
|
| 99 |
]
|
| 100 |
|
| 101 |
+
# Girdileri hazırla
|
| 102 |
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 103 |
image_inputs, video_inputs = process_vision_info(messages)
|
| 104 |
|
|
|
|
| 118 |
do_sample=False
|
| 119 |
)
|
| 120 |
|
| 121 |
+
# Çıktıyı işle
|
| 122 |
generated_ids_trimmed = [
|
| 123 |
out_ids[len(in_ids):]
|
| 124 |
for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
|
|
|
|
| 130 |
clean_up_tokenization_spaces=False
|
| 131 |
)[0].strip()
|
| 132 |
|
| 133 |
+
# Temizlik
|
| 134 |
clean_result = result.replace('.', '').strip()
|
| 135 |
|
| 136 |
if clean_result in CLASS_DESCRIPTIONS:
|
|
|
|
| 138 |
display_text = f"### 🎯 Sonuç: {formatted_result}\n\n**Orijinal Sınıf:** `{clean_result}`"
|
| 139 |
else:
|
| 140 |
display_text = f"### 🤖 Model Çıktısı: {result}"
|
| 141 |
+
|
| 142 |
+
return display_text, result
|
|
|
|
| 143 |
|
| 144 |
except Exception as e:
|
| 145 |
return f"❌ Hata: {str(e)}", "Error"
|
|
|
|
| 159 |
input_image = gr.Image(label="Uydu Görüntüsü Yükle", type="pil", height=300)
|
| 160 |
classify_btn = gr.Button("🔍 Sınıflandır", variant="primary", size="lg")
|
| 161 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
with gr.Column():
|
| 163 |
output_text = gr.Markdown(label="Analiz Sonucu", value="*Görüntü bekleniyor...*")
|
| 164 |
output_raw = gr.Textbox(label="Ham Çıktı", interactive=False)
|
| 165 |
|
| 166 |
gr.HTML("""
|
| 167 |
<div style="margin-top: 20px; padding: 10px; background-color: #f0f0f0; border-radius: 5px;">
|
| 168 |
+
<p style="margin:0"><b>Not:</b>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|