teatwots commited on
Commit
dedd7dd
1 Parent(s): 843c957

Create appy.py

Browse files
Files changed (1) hide show
  1. appy.py +92 -0
appy.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from PIL import Image
3
+ import base64
4
+ from io import BytesIO
5
+ from gtts import gTTS
6
+ import gdown
7
+
8
+ # Define the URLs of the images
9
+ image_urls = [
10
+ "https://drive.google.com/uc?id=19zhvDXwloycllKv0q8fN4AkNWJ3WC74I",
11
+ "https://drive.google.com/uc?id=1_9MzEhc_YfwYFGVLUUlFW3YpSuZk6FSV",
12
+ "https://drive.google.com/uc?id=1v1JRBsza_pnpLGv_vgORpeYDo9DLmMAD",
13
+ "https://drive.google.com/uc?id=1aunq6D-MuES5BKkkxPFYH4R5h6W6WlDE"
14
+ ]
15
+
16
+ # Define the local file names
17
+ image_files = [
18
+ "image1.png",
19
+ "image2.png",
20
+ "image3.png",
21
+ "image4.png"
22
+ ]
23
+
24
+ # Download the images
25
+ for url, file in zip(image_urls, image_files):
26
+ gdown.download(url, file, quiet=False)
27
+
28
+ # Descriptions for each scene
29
+ descriptions = [
30
+ "Alex, Mia, and Sam eagerly plan their treasure hunt in a room filled with maps and exploration tools.",
31
+ "The friends hike through a dense forest, guided by their ancient map.",
32
+ "Deep in Whispering Hollow, they discover an ancient chest filled with historical artifacts.",
33
+ "The village celebrates as Alex, Mia, and Sam present their find to the local museum."
34
+ ]
35
+
36
+ # Generate audio files for each description
37
+ audio_files = []
38
+ for i, desc in enumerate(descriptions):
39
+ tts = gTTS(desc)
40
+ audio_file = f"audio_{i+1}.mp3"
41
+ tts.save(audio_file)
42
+ audio_files.append(audio_file)
43
+
44
+ # Load images
45
+ images = [
46
+ Image.open("image1.png"),
47
+ Image.open("image2.png"),
48
+ Image.open("image3.png"),
49
+ Image.open("image4.png")
50
+ ]
51
+
52
+ # Assign labels to images
53
+ labels = ['B', 'D', 'A', 'C']
54
+ label_image_description_audio = list(zip(labels, images, descriptions, audio_files))
55
+
56
+ # Ensure the display order is A -> B -> C -> D
57
+ display_order = ['A', 'B', 'C', 'D']
58
+ label_image_description_audio_sorted = sorted(label_image_description_audio, key=lambda x: display_order.index(x[0]))
59
+
60
+ # Function to check the order
61
+ def check_order(a, b, c, d):
62
+ user_order = [a, b, c, d]
63
+ correct_order = ['B', 'D', 'A', 'C']
64
+ return "Correct!" if user_order == correct_order else "Try again."
65
+
66
+ # Helper function to convert image to base64 string
67
+ def img_to_base64(img):
68
+ buffered = BytesIO()
69
+ img.save(buffered, format="PNG")
70
+ img_str = base64.b64encode(buffered.getvalue()).decode()
71
+ return img_str
72
+
73
+ # Create Gradio interface using gr.Blocks for layout control
74
+ with gr.Blocks(css=".small-audio { height: 180px !important; width: 230px !important; }") as iface:
75
+ gr.Markdown("The images are displayed below with labels A, B, C, and D. Enter the correct sequence according to the story.")
76
+ with gr.Row():
77
+ for label, img, desc, audio in label_image_description_audio_sorted:
78
+ with gr.Column():
79
+ gr.Image(value=img, label=label, width=300) # Display image with label
80
+ gr.Markdown(f"**{label}**: {desc}") # Display description below the image
81
+ gr.Audio(value=audio, elem_classes="small-audio") # Display TTS audio below the description with smaller size
82
+ with gr.Row():
83
+ a = gr.Textbox(label="Enter label for the first part of story")
84
+ b = gr.Textbox(label="Enter label for the second part of story")
85
+ c = gr.Textbox(label="Enter label for the third part of story")
86
+ d = gr.Textbox(label="Enter label for the fourth part of story")
87
+ btn = gr.Button("Check Order")
88
+ output = gr.Textbox(label="Result")
89
+
90
+ btn.click(fn=check_order, inputs=[a, b, c, d], outputs=output)
91
+
92
+ iface.launch()