Yonatan-Bitton commited on
Commit
62557b1
β€’
1 Parent(s): e487f44

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ from datasets import load_dataset
4
+ import random
5
+
6
+ auth_token = os.environ.get("auth_token")
7
+ iiw_400 = load_dataset('google/imageinwords', token=auth_token, name="IIW-400")
8
+
9
+ def display_iiw_data(index):
10
+ data = iiw_400['test'][index]
11
+ image_html = f'<img src="{data["image/url"]}" style="width:100%; max-width:800px; height:auto;">'
12
+ iiw_text = f"<h2>IIW Human Descriptions:</h2><p style='font-size: 16px'>{data['IIW']}</p>"
13
+ iiw_p5b_text = f"<h2>IIW PaLI 5B Predictions:</h2><p style='font-size: 16px'>{data['IIW-P5B']}</p>"
14
+ ratings = "<h2>Ratings:</h2>"
15
+ if data['iiw-human-sxs-iiw-p5b'] is not None:
16
+ for key, value in data['iiw-human-sxs-iiw-p5b'].items():
17
+ key = key.split("metrics/")[-1]
18
+ emoji = ""
19
+ if key == "Comprehensiveness":
20
+ emoji = "πŸ“š" # Book
21
+ elif key == "Specificity":
22
+ emoji = "🎯" # Bullseye
23
+ elif key == "Hallucination":
24
+ emoji = "πŸ‘»" # Ghost
25
+ elif key == "First few line(s) as tldr":
26
+ emoji = "πŸ”" # Magnifying Glass Tilted Left
27
+ elif key == "Human Like":
28
+ emoji = "πŸ‘€" # Bust in Silhouette
29
+ ratings += f"<p style='font-size: 16px'>{emoji} <strong>{key}</strong>: {value}</p>"
30
+ return image_html, iiw_text, iiw_p5b_text, ratings
31
+
32
+ def random_index():
33
+ while True:
34
+ index = random.randint(0, len(iiw_400['test']) - 1)
35
+ if iiw_400['test'][index]['iiw-human-sxs-iiw-p5b'] is not None:
36
+ return index
37
+
38
+ demo = gr.Blocks()
39
+
40
+ with demo:
41
+ gr.Markdown("# Slide across the slider to see various examples from IIW-400")
42
+
43
+ with gr.Column():
44
+ slider = gr.Slider(minimum=0, maximum=400)
45
+ with gr.Row():
46
+ index = random_index()
47
+ with gr.Column():
48
+ image_output = gr.HTML(display_iiw_data(index)[0])
49
+ with gr.Column():
50
+ iiw_text_output = gr.HTML(display_iiw_data(index)[1])
51
+ iiw_p5b_text_output = gr.HTML(display_iiw_data(index)[2])
52
+ ratings_output = gr.HTML(display_iiw_data(index)[3])
53
+
54
+ slider.change(display_iiw_data, inputs=[slider], outputs=[image_output, iiw_text_output, iiw_p5b_text_output, ratings_output])
55
+
56
+ demo.launch(debug=True)