Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,9 +3,12 @@ from transformers import AutoProcessor, PaliGemmaForConditionalGeneration
|
|
3 |
import torch
|
4 |
from PIL import Image
|
5 |
from huggingface_hub import login
|
|
|
6 |
|
7 |
-
#
|
8 |
-
HF_TOKEN = "
|
|
|
|
|
9 |
login(HF_TOKEN) # Hugging Face'e oturum aç
|
10 |
|
11 |
# PaliGemma modelini ve işlemcisini yükle
|
@@ -15,20 +18,13 @@ processor = AutoProcessor.from_pretrained(model_id)
|
|
15 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
16 |
model.to(device)
|
17 |
|
18 |
-
# Modeli kullanarak tahmin yapma fonksiyonu
|
19 |
def predict(image, question):
|
20 |
-
# Görüntüyü ve soruyu işle
|
21 |
inputs = processor(images=image, text=question, return_tensors="pt").to(device)
|
22 |
-
|
23 |
-
# Tahmin yap
|
24 |
with torch.no_grad():
|
25 |
output = model.generate(**inputs, max_new_tokens=50)
|
26 |
-
|
27 |
-
# Çıktıyı decode et
|
28 |
answer = processor.decode(output[0], skip_special_tokens=True)[len(question):].strip()
|
29 |
return answer
|
30 |
|
31 |
-
# Gradio arayüzünü oluştur
|
32 |
interface = gr.Interface(
|
33 |
fn=predict,
|
34 |
inputs=[
|
@@ -40,5 +36,4 @@ interface = gr.Interface(
|
|
40 |
description="Bir görüntü yükleyin ve ona dair bir soru sorun. PaliGemma sizin için cevaplayacak!"
|
41 |
)
|
42 |
|
43 |
-
# Arayüzü başlat
|
44 |
interface.launch()
|
|
|
3 |
import torch
|
4 |
from PIL import Image
|
5 |
from huggingface_hub import login
|
6 |
+
import os
|
7 |
|
8 |
+
# Token’ı çevreden al
|
9 |
+
HF_TOKEN = os.getenv("HF_TOKEN")
|
10 |
+
if not HF_TOKEN:
|
11 |
+
raise ValueError("HF_TOKEN bulunamadı. Lütfen Space ayarlarından token’ı ekleyin.")
|
12 |
login(HF_TOKEN) # Hugging Face'e oturum aç
|
13 |
|
14 |
# PaliGemma modelini ve işlemcisini yükle
|
|
|
18 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
model.to(device)
|
20 |
|
|
|
21 |
def predict(image, question):
|
|
|
22 |
inputs = processor(images=image, text=question, return_tensors="pt").to(device)
|
|
|
|
|
23 |
with torch.no_grad():
|
24 |
output = model.generate(**inputs, max_new_tokens=50)
|
|
|
|
|
25 |
answer = processor.decode(output[0], skip_special_tokens=True)[len(question):].strip()
|
26 |
return answer
|
27 |
|
|
|
28 |
interface = gr.Interface(
|
29 |
fn=predict,
|
30 |
inputs=[
|
|
|
36 |
description="Bir görüntü yükleyin ve ona dair bir soru sorun. PaliGemma sizin için cevaplayacak!"
|
37 |
)
|
38 |
|
|
|
39 |
interface.launch()
|