Mueris commited on
Commit
f1db530
·
verified ·
1 Parent(s): ec3946b

Update app.py

Browse files

UI design fixes done

Files changed (1) hide show
  1. app.py +25 -32
app.py CHANGED
@@ -3,26 +3,24 @@ from PIL import Image
3
  import torch
4
  import os
5
 
6
- # Assuming inference.py contains load_for_inference and predict
7
- # NOTE: inference.py must be present and contain these functions for VQA to work.
8
  from inference import load_for_inference, predict
9
 
10
- # -----------------------
11
  # Load TAMGA VQA model
12
- # -----------------------
13
  TAMGA_REPO = "Mueris/TurkishVLMTAMGA"
14
- # Added a check for the existence of the imported function
15
  if 'load_for_inference' in globals():
16
  tamga_model, tamga_tokenizer, tamga_device = load_for_inference(TAMGA_REPO)
17
  else:
18
- # Placeholder for running environment where inference module might not be fully available
19
  print("Warning: inference.py functions not loaded. Using placeholder values.")
20
  tamga_model, tamga_tokenizer, tamga_device = None, None, 'cpu'
21
 
22
 
23
- # -----------------------
24
  # Load BLIP Caption Model
25
- # -----------------------
26
  from transformers import BlipProcessor, BlipForConditionalGeneration
27
 
28
  CAPTION_REPO = "Mueris/TurkishVLMTAMGA-CaptioningModel"
@@ -32,17 +30,15 @@ caption_model.to("cuda" if torch.cuda.is_available() else "cpu")
32
  caption_device = caption_model.device
33
 
34
 
35
- # -----------------------
36
  # Utility Functions
37
- # -----------------------
38
 
39
  def toggle_question_input(model_choice):
40
 
41
  if model_choice == "TAMGA VQA":
42
- # VQA seçildiyse: Grubu GÖSTER, Metin kutusuna DOKUNMA (değişiklik yok)
43
  return gr.update(visible=True), gr.update()
44
  else:
45
- # BLIP seçildiyse: Grubu GİZLE, Metin kutusunu TEMİZLE
46
  return gr.update(visible=False), gr.update(value="")
47
 
48
  def select_quick_question(quick_question):
@@ -52,9 +48,9 @@ def select_quick_question(quick_question):
52
  return gr.update(value=quick_question), gr.update(value=None)
53
 
54
 
55
- # -----------------------
56
  # Main Inference Function
57
- # -----------------------
58
  def answer(model_choice, image, question):
59
 
60
  if image is None:
@@ -70,8 +66,7 @@ def answer(model_choice, image, question):
70
  return "**Hata: TAMGA VQA modeli yüklenemedi. 'inference.py' dosyasını ve bağımlılıkları kontrol edin.**"
71
 
72
  pil_image = Image.fromarray(image)
73
- # Note: tamga_device is determined by load_for_inference
74
- # Assuming predict function is correctly implemented in inference.py
75
  response = predict(tamga_model, tamga_tokenizer, tamga_device, pil_image, question)
76
  return f"**Cevap:** {response}"
77
 
@@ -79,9 +74,7 @@ def answer(model_choice, image, question):
79
  elif model_choice == "BLIP Caption (Fine-Tuned)":
80
 
81
  pil_image = Image.fromarray(image)
82
- # Ensure device is correctly set for inputs
83
  inputs = caption_processor(images=pil_image, return_tensors="pt").to(caption_device)
84
- # Generate caption
85
  output = caption_model.generate(**inputs, max_new_tokens=64)
86
  caption = caption_processor.decode(output[0], skip_special_tokens=True)
87
  return f"**Açıklama:** {caption}"
@@ -89,9 +82,9 @@ def answer(model_choice, image, question):
89
  return "**Model bulunamadı.**"
90
 
91
 
92
- # -----------------------
93
- # CSS (Vibrant Theme Applied)
94
- # -----------------------
95
  css = """
96
  #col-container {
97
  max-width: 1100px;
@@ -224,19 +217,19 @@ button[variant="primary"]:hover {
224
  # -----------------------
225
  VQA_QUESTION_CHOICES = [
226
  "Bu görselde kaç tane insan figürü var?",
227
- "Görseldeki baskın renk nedir?",
228
  "Fotoğrafta ne tür bir araç görülüyor?",
229
- "Bu olayın gerçekleştiği mevsime dair ipuçları var mı?"
230
  ]
231
 
232
 
233
- # -----------------------
234
  # UI Layout
235
- # -----------------------
236
  with gr.Blocks(css=css) as demo:
237
 
238
- gr.HTML("<div id='title'>🇹🇷 TAMGA — Çok Modelli Görsel Dil Sistemi</div>")
239
- gr.HTML("<div id='subtitle'>VQA veya Fine-Tuned BLIP Captioning modellerinden birini seçin.</div>")
240
 
241
  with gr.Row(elem_id="col-container"):
242
 
@@ -260,7 +253,7 @@ with gr.Blocks(css=css) as demo:
260
  )
261
  # ----------------------
262
 
263
- # Soru metin kutusu ve örnekleri bir gr.Group içine alıyoruz
264
  with gr.Group(visible=True) as vqa_inputs_group:
265
  question = gr.Textbox(
266
  label="Soru (Sadece VQA Modeli İçin)",
@@ -273,9 +266,9 @@ with gr.Blocks(css=css) as demo:
273
  quick_question_radio = gr.Radio(
274
  choices=VQA_QUESTION_CHOICES,
275
  label="Hızlı Sorular",
276
- value=None, # Başlangıçta hiçbir şey seçili olmasın
277
  elem_id="quick-questions",
278
- container=False # Label'ı yukarıda ayrı bir HTML ile kontrol ettiğimiz için
279
  )
280
  # --------------------------------------
281
 
@@ -286,7 +279,7 @@ with gr.Blocks(css=css) as demo:
286
 
287
  output = gr.Markdown(elem_classes="output-box")
288
 
289
- # Button clickrun model
290
  submit_btn.click(
291
  fn=answer,
292
  inputs=[model_choice, image, question],
@@ -301,7 +294,7 @@ with gr.Blocks(css=css) as demo:
301
  outputs=[vqa_inputs_group, question],
302
  queue=False
303
  )
304
- # -------------------------------------------------
305
 
306
 
307
  # --- Quick Question Selection Logic ---
 
3
  import torch
4
  import os
5
 
6
+
 
7
  from inference import load_for_inference, predict
8
 
 
9
  # Load TAMGA VQA model
10
+
11
  TAMGA_REPO = "Mueris/TurkishVLMTAMGA"
12
+
13
  if 'load_for_inference' in globals():
14
  tamga_model, tamga_tokenizer, tamga_device = load_for_inference(TAMGA_REPO)
15
  else:
16
+
17
  print("Warning: inference.py functions not loaded. Using placeholder values.")
18
  tamga_model, tamga_tokenizer, tamga_device = None, None, 'cpu'
19
 
20
 
21
+
22
  # Load BLIP Caption Model
23
+
24
  from transformers import BlipProcessor, BlipForConditionalGeneration
25
 
26
  CAPTION_REPO = "Mueris/TurkishVLMTAMGA-CaptioningModel"
 
30
  caption_device = caption_model.device
31
 
32
 
33
+
34
  # Utility Functions
35
+
36
 
37
  def toggle_question_input(model_choice):
38
 
39
  if model_choice == "TAMGA VQA":
 
40
  return gr.update(visible=True), gr.update()
41
  else:
 
42
  return gr.update(visible=False), gr.update(value="")
43
 
44
  def select_quick_question(quick_question):
 
48
  return gr.update(value=quick_question), gr.update(value=None)
49
 
50
 
51
+
52
  # Main Inference Function
53
+
54
  def answer(model_choice, image, question):
55
 
56
  if image is None:
 
66
  return "**Hata: TAMGA VQA modeli yüklenemedi. 'inference.py' dosyasını ve bağımlılıkları kontrol edin.**"
67
 
68
  pil_image = Image.fromarray(image)
69
+
 
70
  response = predict(tamga_model, tamga_tokenizer, tamga_device, pil_image, question)
71
  return f"**Cevap:** {response}"
72
 
 
74
  elif model_choice == "BLIP Caption (Fine-Tuned)":
75
 
76
  pil_image = Image.fromarray(image)
 
77
  inputs = caption_processor(images=pil_image, return_tensors="pt").to(caption_device)
 
78
  output = caption_model.generate(**inputs, max_new_tokens=64)
79
  caption = caption_processor.decode(output[0], skip_special_tokens=True)
80
  return f"**Açıklama:** {caption}"
 
82
  return "**Model bulunamadı.**"
83
 
84
 
85
+
86
+ # CSS
87
+
88
  css = """
89
  #col-container {
90
  max-width: 1100px;
 
217
  # -----------------------
218
  VQA_QUESTION_CHOICES = [
219
  "Bu görselde kaç tane insan figürü var?",
220
+ "Görselde ne görüyorsun?",
221
  "Fotoğrafta ne tür bir araç görülüyor?",
222
+ "Bu görselde hava aracı var mı?"
223
  ]
224
 
225
 
226
+
227
  # UI Layout
228
+
229
  with gr.Blocks(css=css) as demo:
230
 
231
+ gr.HTML("<div id='title'>🇹🇷 TAMGA — Çok Modelli Türkçe Görsel Dil Modeli</div>")
232
+ gr.HTML("<div id='subtitle'>TAMGA VQA (Soru Cevap) veya TAMGA Görsel Açıklama modellerinden birini seçin.</div>")
233
 
234
  with gr.Row(elem_id="col-container"):
235
 
 
253
  )
254
  # ----------------------
255
 
256
+
257
  with gr.Group(visible=True) as vqa_inputs_group:
258
  question = gr.Textbox(
259
  label="Soru (Sadece VQA Modeli İçin)",
 
266
  quick_question_radio = gr.Radio(
267
  choices=VQA_QUESTION_CHOICES,
268
  label="Hızlı Sorular",
269
+ value=None,
270
  elem_id="quick-questions",
271
+ container=False
272
  )
273
  # --------------------------------------
274
 
 
279
 
280
  output = gr.Markdown(elem_classes="output-box")
281
 
282
+ # Button click run model
283
  submit_btn.click(
284
  fn=answer,
285
  inputs=[model_choice, image, question],
 
294
  outputs=[vqa_inputs_group, question],
295
  queue=False
296
  )
297
+
298
 
299
 
300
  # --- Quick Question Selection Logic ---