66import base64
77import os
88from pathlib import Path
9- from typing import Dict , Any
9+ from typing import Dict , Any , Optional
1010import requests
1111from multimind .api .unified_api import UnifiedRequest , ModalityInput
1212
1313def get_data_path (filename : str ) -> Path :
1414 """Get the absolute path to a data file."""
15- return Path (os .path .join (os .path .dirname (__file__ ), ".." , ".." , "data" , filename ))
15+ # examples/multi_modal/basic/process_request.py -> parents[2] == examples/
16+ return Path (__file__ ).resolve ().parents [2 ] / "data" / filename
17+
18+
19+ def _pick_first (data_dir : Path , patterns : list [str ]) -> Optional [Path ]:
20+ """Pick first matching file in data_dir for given glob patterns."""
21+ for pat in patterns :
22+ matches = sorted (data_dir .glob (pat ))
23+ if matches :
24+ return matches [0 ]
25+ return None
1626
1727async def process_image_caption ():
1828 """Process an image captioning request."""
1929 # Load and encode image
20- image_path = get_data_path ("sample_image.jpg" )
30+ data_dir = Path (__file__ ).resolve ().parents [2 ] / "data"
31+ image_path = get_data_path ("sample_image1.png" )
2132 if not image_path .exists ():
22- print (f"Error: Image file not found at { image_path } " )
23- return
33+ fallback = _pick_first (data_dir , ["*.png" , "*.jpg" , "*.jpeg" , "*.webp" ])
34+ if fallback is None :
35+ print (f"Error: Image file not found at { image_path } " )
36+ return
37+ image_path = fallback
38+ print (f"Warning: Using fallback image file { image_path } " )
2439
2540 with open (image_path , "rb" ) as f :
2641 image_data = base64 .b64encode (f .read ()).decode ()
@@ -47,7 +62,7 @@ async def process_image_caption():
4762 # Send request to API
4863 response = requests .post (
4964 "http://localhost:8000/v1/process" ,
50- json = request .dict ()
65+ json = request .model_dump ()
5166 )
5267
5368 if response .status_code == 200 :
@@ -63,10 +78,15 @@ async def process_image_caption():
6378async def process_audio_transcription ():
6479 """Process an audio transcription request."""
6580 # Load and encode audio
81+ data_dir = Path (__file__ ).resolve ().parents [2 ] / "data"
6682 audio_path = get_data_path ("sample_audio.mp3" )
6783 if not audio_path .exists ():
68- print (f"Error: Audio file not found at { audio_path } " )
69- return
84+ fallback = _pick_first (data_dir , ["*.mp3" , "*.wav" , "*.m4a" , "*.flac" , "*.ogg" ])
85+ if fallback is None :
86+ print (f"Error: Audio file not found at { audio_path } " )
87+ return
88+ audio_path = fallback
89+ print (f"Warning: Using fallback audio file { audio_path } " )
7090
7191 with open (audio_path , "rb" ) as f :
7292 audio_data = base64 .b64encode (f .read ()).decode ()
@@ -93,7 +113,7 @@ async def process_audio_transcription():
93113 # Send request to API
94114 response = requests .post (
95115 "http://localhost:8000/v1/process" ,
96- json = request .dict ()
116+ json = request .model_dump ()
97117 )
98118
99119 if response .status_code == 200 :
@@ -109,15 +129,24 @@ async def process_audio_transcription():
109129async def process_multi_modal_analysis ():
110130 """Process a complex multi-modal analysis request."""
111131 # Load and encode media
112- image_path = get_data_path ("sample_image.jpg" )
132+ data_dir = Path (__file__ ).resolve ().parents [2 ] / "data"
133+ image_path = get_data_path ("sample_image1.png" )
113134 audio_path = get_data_path ("sample_audio.mp3" )
114135
115136 if not image_path .exists ():
116- print (f"Error: Image file not found at { image_path } " )
117- return
137+ fallback = _pick_first (data_dir , ["*.png" , "*.jpg" , "*.jpeg" , "*.webp" ])
138+ if fallback is None :
139+ print (f"Error: Image file not found at { image_path } " )
140+ return
141+ image_path = fallback
142+ print (f"Warning: Using fallback image file { image_path } " )
118143 if not audio_path .exists ():
119- print (f"Error: Audio file not found at { audio_path } " )
120- return
144+ fallback = _pick_first (data_dir , ["*.mp3" , "*.wav" , "*.m4a" , "*.flac" , "*.ogg" ])
145+ if fallback is None :
146+ print (f"Error: Audio file not found at { audio_path } " )
147+ return
148+ audio_path = fallback
149+ print (f"Warning: Using fallback audio file { audio_path } " )
121150
122151 with open (image_path , "rb" ) as f :
123152 image_data = base64 .b64encode (f .read ()).decode ()
@@ -150,12 +179,16 @@ async def process_multi_modal_analysis():
150179 # Send request to API
151180 response = requests .post (
152181 "http://localhost:8000/v1/process" ,
153- json = request .dict ()
182+ json = request .model_dump ()
154183 )
155184
156185 if response .status_code == 200 :
157186 result = response .json ()
158187 print ("Multi-modal analysis completed successfully!" )
188+ if "image_text" in result .get ("outputs" , {}):
189+ print (f"\n Image expert output:\n { result ['outputs' ]['image_text' ]} " )
190+ if "audio_text" in result .get ("outputs" , {}):
191+ print (f"\n Audio expert output:\n { result ['outputs' ]['audio_text' ]} " )
159192 print (f"Analysis: { result ['outputs' ]['text' ]} " )
160193 print ("\n Expert weights:" )
161194 for expert , weight in result ['expert_weights' ].items ():
@@ -165,7 +198,7 @@ async def process_multi_modal_analysis():
165198
166199if __name__ == "__main__" :
167200 # Create example data directory if it doesn't exist
168- data_dir = Path (os . path . join ( os . path . dirname ( __file__ ), ".." , ".." , "data" ))
201+ data_dir = Path (__file__ ). resolve (). parents [ 2 ] / "data"
169202 data_dir .mkdir (parents = True , exist_ok = True )
170203
171204 # Run examples
0 commit comments