raghavd99 commited on
Commit
01ed15c
1 Parent(s): ea16a82

app update

Browse files
.gitignore CHANGED
@@ -33,3 +33,6 @@ gradio.egg-info/
33
 
34
  # Virtual Environment
35
  .env/
 
 
 
 
33
 
34
  # Virtual Environment
35
  .env/
36
+
37
+ #others
38
+ test_inputs/
README.md CHANGED
@@ -1,13 +1,4 @@
1
- ---
2
- title: Imagebind Demo
3
- emoji: 📉
4
- colorFrom: indigo
5
- colorTo: purple
6
- sdk: gradio
7
- sdk_version: 4.19.2
8
- app_file: app.py
9
- pinned: false
10
- license: mit
11
- ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
+ # Imagebind demo
2
+
3
+ A gradio app showcasing multi-modal capabilities of Imagebind supported via lanceDB API
 
 
 
 
 
 
 
 
4
 
 
app.py CHANGED
@@ -2,166 +2,89 @@ import lancedb
2
  import lancedb.embeddings.imagebind
3
  from lancedb.embeddings import get_registry
4
  from lancedb.pydantic import LanceModel, Vector
5
- import pandas as pd
6
- import os
7
  import gradio as gr
 
8
 
9
  model = get_registry().get("imagebind").create()
10
 
 
11
  class TextModel(LanceModel):
12
- text: str
13
- image_uri: str = model.SourceField()
14
- audio_path: str
15
- vector: Vector(model.ndims()) = model.VectorField()
16
-
17
- text_list=["A dragon", "A car", "A bird"]
18
-
19
- base_path = os.path.dirname(os.path.abspath(__file__))
20
- # image_paths=["/test_inputs/dragon.jpg",
21
- # "/test_inputs/car.jpg",
22
- # "/test_inputs/bird_image.jpg"]
23
- # audio_paths=["/test_inputs/dragon-growl-37570.wav",
24
- # "/test_inputs/car_audio.wav",
25
- # "/test_inputs/bird_audio.wav"]
26
-
27
- image_paths=["./test_inputs/dragon.jpg",
28
- "./test_inputs/car.jpg",
29
- "./test_inputs/bird_image.jpg"]
30
- audio_paths=["./test_inputs/dragon-growl-37570.wav",
31
- "./test_inputs/car_audio.wav",
32
- "./test_inputs/bird_audio.wav"]
33
  # Load data
34
  inputs = [
35
- {
36
- "text": a,
37
- "audio_path":b,
38
- "image_uri":c
39
- } for a,
40
- b,
41
- c in zip(text_list,
42
- audio_paths,
43
- image_paths)
44
  ]
45
 
46
-
47
  db = lancedb.connect("/tmp/lancedb")
48
- table = db.create_table("img_bind",schema=TextModel)
49
  table.add(inputs)
50
 
51
 
52
  def process_image(inp_img) -> str:
53
-
54
  actual = (
55
- table.search(inp_img, vector_column_name="vector")
56
- .limit(1)
57
- .to_pydantic(TextModel)[0]
58
  )
59
 
60
  return actual.text, actual.audio_path
61
 
62
- def process_text(inp_text) -> str:
63
 
 
64
  actual = (
65
- table.search(inp_text, vector_column_name="vector")
66
- .limit(1)
67
- .to_pydantic(TextModel)[0]
68
  )
69
 
70
  return actual.image_uri, actual.audio_path
71
 
72
- def process_audio(inp_audio) -> str:
73
 
 
74
  actual = (
75
- table.search(inp_audio, vector_column_name="vector")
76
- .limit(1)
77
- .to_pydantic(TextModel)[0]
78
  )
79
 
80
  return actual.image_uri, actual.text
81
 
82
- css = """
83
- output-audio, output-text {
84
- display: none;
85
- }
86
- img {
87
- width: 300px;
88
- height: 450px;
89
- object-fit: cover;
90
-
91
- """
92
- with gr.Blocks(css=css) as app:
93
- # Using Markdown for custom CSS (optional)
94
- with gr.Tab("Image to Text and Audio"):
95
- with gr.Row():
96
- with gr.Column():
97
-
98
- inp1 = gr.Image(value=image_paths[0],type='filepath',elem_id='img',interactive=False)
99
- output_audio1 = gr.Audio(label="Output Audio", elem_id="output-audio")
100
- output_text1 = gr.Textbox(label="Output Text", elem_id="output-text")
101
- btn_img1 = gr.Button("Retrieve")
102
-
103
- # output_audio1 = gr.Audio(label="Output Audio 1", elem_id="output-audio1")
104
- with gr.Column():
105
- inp2 = gr.Image(value=image_paths[1],type='filepath',elem_id='img',interactive=False)
106
- output_audio2 = gr.Audio(label="Output Audio", elem_id="output-audio")
107
- output_text2 = gr.Textbox(label="Output Text", elem_id="output-text")
108
- btn_img2 = gr.Button("Retrieve")
109
-
110
- with gr.Column():
111
- inp3 = gr.Image(value=image_paths[2],type='filepath',elem_id='img',interactive=False)
112
- output_audio3 = gr.Audio(label="Output Audio", elem_id="output-audio")
113
- output_text3 = gr.Textbox(label="Output Text", elem_id="output-text")
114
- btn_img3 = gr.Button("Retrieve")
115
-
116
- with gr.Tab("Text to Image and Audio"):
117
- with gr.Row():
118
- with gr.Column():
119
- input_txt1 = gr.Textbox(label="Enter a prompt:", elem_id="output-text")
120
- output_audio4 = gr.Audio(label="Output Audio", elem_id="output-audio")
121
- output_img1 = gr.Image(type='filepath',elem_id='img')
122
-
123
- # with gr.Column():
124
- # input_txt2 = gr.Textbox(label="Enter a prompt:", elem_id="output-text")
125
- # output_audio5 = gr.Audio(label="Output Audio", elem_id="output-audio")
126
- # output_img2 = gr.Image(type='filepath',elem_id='img')
127
-
128
-
129
- # with gr.Column():
130
- # input_txt3 = gr.Textbox(label="Enter a prompt:", elem_id="output-text")
131
- # output_audio6 = gr.Audio(label="Output Audio", elem_id="output-audio")
132
- # output_img3 = gr.Image(type='filepath',elem_id='img')
133
-
134
- with gr.Tab("Audio to Image and Text"):
135
- with gr.Row():
136
- with gr.Column():
137
- inp_audio1 = gr.Audio(value='./test_inputs/dragon-growl-37570.wav',type='filepath',elem_id='output-audio')
138
- output_img7 = gr.Image(type='filepath',elem_id='img')
139
- output_text7 = gr.Textbox(label="Output Text", elem_id="output-text")
140
- btn_audio1 = gr.Button("Retrieve")
141
-
142
- with gr.Column():
143
- inp_audio2 = gr.Audio(value='./test_inputs/car_audio.wav',type='filepath',elem_id='output-audio')
144
- output_img8 = gr.Image(type='filepath',elem_id='img')
145
- output_text8 = gr.Textbox(label="Output Text", elem_id="output-text")
146
- btn_audio2 = gr.Button("Retrieve")
147
-
148
- with gr.Column():
149
- inp_audio3 = gr.Audio(value='./test_inputs/bird_audio.wav',type='filepath',elem_id='output-audio')
150
- output_img9 = gr.Image(type='filepath',elem_id='img')
151
- output_text9 = gr.Textbox(label="Output Text", elem_id="output-text")
152
- btn_audio3 = gr.Button("Retrieve")
153
-
154
- # Click actions for buttons/Textboxes
155
- btn_img1.click(process_image, inputs=[inp1],outputs=[output_text1,output_audio1])
156
- btn_img2.click(process_image, inputs=[inp2],outputs=[output_text2,output_audio2])
157
- btn_img3.click(process_image, inputs=[inp3],outputs=[output_text3,output_audio3])
158
-
159
- input_txt1.submit(process_text, inputs=[input_txt1],outputs=[output_img1,output_audio4])
160
-
161
- btn_audio1.click(process_audio, inputs=[inp_audio1],outputs=[output_img7,output_text7])
162
- btn_audio2.click(process_audio, inputs=[inp_audio2],outputs=[output_img8,output_text8])
163
- btn_audio3.click(process_audio, inputs=[inp_audio3],outputs=[output_img9,output_text9])
164
 
165
- if __name__ == "__main__":
166
- app.launch(share=True,allowed_paths=['./test_inputs'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
167
 
 
 
 
2
  import lancedb.embeddings.imagebind
3
  from lancedb.embeddings import get_registry
4
  from lancedb.pydantic import LanceModel, Vector
 
 
5
  import gradio as gr
6
+ from downloader import dowload_and_save_audio, dowload_and_save_image, base_path
7
 
8
  model = get_registry().get("imagebind").create()
9
 
10
+
11
  class TextModel(LanceModel):
12
+ text: str
13
+ image_uri: str = model.SourceField()
14
+ audio_path: str
15
+ vector: Vector(model.ndims()) = model.VectorField()
16
+
17
+
18
+ text_list = ["A bird", "A dragon", "A car"]
19
+ image_paths = dowload_and_save_image()
20
+ audio_paths = dowload_and_save_audio()
21
+
 
 
 
 
 
 
 
 
 
 
 
22
  # Load data
23
  inputs = [
24
+ {"text": a, "audio_path": b, "image_uri": c}
25
+ for a, b, c in zip(text_list, audio_paths, image_paths)
 
 
 
 
 
 
 
26
  ]
27
 
 
28
  db = lancedb.connect("/tmp/lancedb")
29
+ table = db.create_table("img_bind", schema=TextModel)
30
  table.add(inputs)
31
 
32
 
33
  def process_image(inp_img) -> str:
 
34
  actual = (
35
+ table.search(inp_img, vector_column_name="vector")
36
+ .limit(1)
37
+ .to_pydantic(TextModel)[0]
38
  )
39
 
40
  return actual.text, actual.audio_path
41
 
 
42
 
43
+ def process_text(inp_text) -> str:
44
  actual = (
45
+ table.search(inp_text, vector_column_name="vector")
46
+ .limit(1)
47
+ .to_pydantic(TextModel)[0]
48
  )
49
 
50
  return actual.image_uri, actual.audio_path
51
 
 
52
 
53
+ def process_audio(inp_audio) -> str:
54
  actual = (
55
+ table.search(inp_audio, vector_column_name="vector")
56
+ .limit(1)
57
+ .to_pydantic(TextModel)[0]
58
  )
59
 
60
  return actual.image_uri, actual.text
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
+ im_to_at = gr.Interface(
64
+ process_image,
65
+ gr.Image(type="filepath", value=image_paths[0]),
66
+ [gr.Text(label="Output Text"), gr.Audio(label="Output Audio")],
67
+ examples=image_paths,
68
+ allow_flagging="never",
69
+ )
70
+ txt_to_ia = gr.Interface(
71
+ process_text,
72
+ gr.Textbox(label="Enter a prompt:"),
73
+ [gr.Image(label="Output Image"), gr.Audio(label="Output Audio")],
74
+ allow_flagging="never",
75
+ examples=text_list,
76
+ )
77
+ a_to_it = gr.Interface(
78
+ process_audio,
79
+ gr.Audio(type="filepath", value=audio_paths[0]),
80
+ [gr.Image(label="Output Image"), gr.Text(label="Output Text")],
81
+ examples=audio_paths,
82
+ allow_flagging="never",
83
+ )
84
+ demo = gr.TabbedInterface(
85
+ [im_to_at, txt_to_ia, a_to_it],
86
+ ["Image to Text/Audio", "Text to Image/Audio", "Audio to Image/Text"],
87
+ )
88
 
89
+ if __name__ == "__main__":
90
+ demo.launch(share=True, allowed_paths=[f"{base_path}/test_inputs/"])
downloader.py ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ from pathlib import Path
4
+
5
+ # URL of the raw audio file on GitHub
6
+ audio_file_urls = [
7
+ "https://github.com/raghavdixit99/assets/raw/main/bird_audio.wav",
8
+ "https://github.com/raghavdixit99/assets/raw/main/dragon-growl-37570.wav",
9
+ "https://github.com/raghavdixit99/assets/raw/main/car_audio.wav",
10
+ ]
11
+ image_urls = [
12
+ "https://github.com/raghavdixit99/assets/assets/34462078/abf47cc4-d979-4aaa-83be-53a2115bf318",
13
+ "https://github.com/raghavdixit99/assets/assets/34462078/93be928e-522b-4e37-889d-d4efd54b2112",
14
+ "https://github.com/raghavdixit99/assets/assets/34462078/025deaff-632a-4829-a86c-3de6e326402f",
15
+ ]
16
+
17
+ base_path = os.path.dirname(os.path.abspath(__file__))
18
+
19
+
20
+ # Local path where you want to save the .wav file
21
+ def dowload_and_save_audio():
22
+ audio_pths = []
23
+ for url in audio_file_urls:
24
+ filename = url.split("/")[-1]
25
+ local_file_path = Path(f"{base_path}/test_inputs/{filename}")
26
+ local_file_path.parent.mkdir(parents=True, exist_ok=True)
27
+ # Perform the GET request
28
+ response = requests.get(url)
29
+
30
+ # Check if the request was successful
31
+ if response.status_code == 200:
32
+ # Write the content of the response to a local file
33
+ with open(local_file_path, "wb") as audio_file:
34
+ audio_file.write(response.content)
35
+ audio_pths.append(str(local_file_path))
36
+ print(
37
+ f"Audio file downloaded successfully and saved as '{local_file_path}'."
38
+ )
39
+ else:
40
+ print(f"Failed to download file. Status code: {response.status_code}")
41
+ return audio_pths
42
+
43
+
44
+ def dowload_and_save_image():
45
+ image_paths = []
46
+ for url in image_urls:
47
+ filename = url.split("/")[-1]
48
+ local_file_path = Path(f"{base_path}/test_inputs/{filename}.jpeg")
49
+
50
+ local_file_path.parent.mkdir(parents=True, exist_ok=True)
51
+ # Perform the GET request
52
+ response = requests.get(url)
53
+
54
+ # Check if the request was successful
55
+ if response.status_code == 200:
56
+ # Write the content of the response to a local file
57
+ with open(local_file_path, "wb") as image_file:
58
+ image_file.write(response.content)
59
+ image_paths.append(str(local_file_path))
60
+ print(
61
+ f"Image file downloaded successfully and saved as '{local_file_path}'."
62
+ )
63
+ else:
64
+ print(f"Failed to download file. Status code: {response.status_code}")
65
+
66
+ return image_paths
test_inputs/bird_image.jpg DELETED
Binary file (21.4 kB)
 
test_inputs/car.jpg DELETED

Git LFS Details

  • SHA256: a274722e399b255aa472f0d2dee22fd5dbc27959a8e70a0b0f09e07144c3573d
  • Pointer size: 130 Bytes
  • Size of remote file: 53.9 kB
test_inputs/dog_audio.wav DELETED
Binary file (461 kB)
 
test_inputs/dog_image.jpg DELETED
Binary file (20.5 kB)
 
test_inputs/dragon.jpg DELETED
Binary file (24.5 kB)