Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269import osimport numpy as npfrom PIL import Imageimport requestsimport timeimport multiprocessingimport jsonimport sysfrom typing import Tuple, List, Dict, Any# Add Florence model path to Python pathflorence_path = os.path.join(os.path.dirname(__file__), 'florence-2-large')sys.path.append(florence_path)try:from florence_2_large.processing_florence2 import Florence2Processorfrom florence_2_large.configuration_florence2 import Florence2Configimport torchimport torch.nn.functional as F# Initialize processor with local filesconfig = Florence2Config.from_json_file(os.path.join(florence_path, 'config.json'))processor = Florence2Processor(config)HAVE_PROCESSOR = Trueprint("Successfully loaded Florence processor")except Exception as e:print(f"Warning: Could not load Florence processor: {e}")print("Using basic output interpretation")HAVE_PROCESSOR = False# Task-specific configurationTASK = "<MORE_DETAILED_CAPTION>" # For detailed image captioning# Model configurationMODEL_ID = "microsoft/florence-2-base"def load_and_preprocess_image(image_path):# Load image and resize to 32x32img = Image.open(image_path)img = img.resize((32, 32))# Convert to numpy array and normalize to [0,1]img_array = np.array(img).astype(np.float32) / 255.0# Ensure array has shape (32, 32, 3)if len(img_array.shape) == 2:img_array = np.stack([img_array] * 3, axis=-1)# Add batch dimensionimg_array = img_array[np.newaxis, ...]# Convert tensor to list of single-element lists for APItensor_data = [[float(x)] for x in img_array.flatten()]return tensor_datadef run_inference(args: Tuple[str, str, int]) -> dict:"""Run inference on a specific server with given chunk ID."""server_url, image_path, chunk_id = argstry:print(f"\nProcessing server {server_url} with chunk {chunk_id}...")# Load and preprocess imageinput_tensor = load_and_preprocess_image(image_path)# Prepare request datadata = {"inputs": input_tensor}# Send request with timeoutprint(f"Sending request to {server_url}/compute/{chunk_id}")start_time = time.time()response = requests.post(f"{server_url}/compute/{chunk_id}",json=data,headers={"Content-Type": "application/json"},timeout=10)inference_time = time.time() - start_timeif response.status_code == 200:result = response.json()return {"server": server_url,"chunk_id": chunk_id,"success": True,"time": inference_time,"result": result}else:error_msg = f"HTTP {response.status_code}"if hasattr(response, 'text'):error_msg += f": {response.text}"return {"server": server_url,"chunk_id": chunk_id,"success": False,"error": error_msg,"time": inference_time}except Exception as e:return {"server": server_url,"chunk_id": chunk_id,"success": False,"error": str(e),"time": time.time() - start_time if 'start_time' in locals() else None}def process_model_outputs(outputs, original_shape=(1, -1, 51289)):"""Process model outputs using Florence processor for sequence generation."""# Convert outputs to numpy arrayoutputs_array = np.array([x[0] for x in outputs])if HAVE_PROCESSOR:try:# Reshape logits to [batch, seq_len, vocab_size]logits = outputs_array.reshape(original_shape)if torch.is_tensor(logits):# Use torch operations if availabletoken_ids = torch.argmax(logits, dim=-1)else:# Fallback to numpytoken_ids = np.argmax(logits, axis=-1)# Decode tokens to texttext = processor.batch_decode(token_ids, skip_special_tokens=True)# Post-process for the specific taskprocessed_text = processor.post_process_generation(text[0] if isinstance(text, list) else text,task=TASK)return {'text': processed_text,'tokens': token_ids.tolist() if torch.is_tensor(token_ids) else token_ids.tolist(),'logits_shape': logits.shape,'distribution': {'min': float(outputs_array.min()),'max': float(outputs_array.max()),'mean': float(outputs_array.mean()),'std': float(outputs_array.std())}}except Exception as e:print(f"Warning: Error in sequence processing: {e}")# Fallback to basic statistics if processor not availablereturn {'overall_mean': float(outputs_array.mean()),'overall_std': float(outputs_array.std()),'shape': outputs_array.shape,'distribution': {'min': float(outputs_array.min()),'max': float(outputs_array.max()),'median': float(np.median(outputs_array))}}def process_results(results):"""Process and combine results from all servers."""# Filter successful resultssuccessful_results = [r for r in results if r['success']]if not successful_results:print("\nError: No servers returned successful results")return# Sort successful results by chunk IDsuccessful_results.sort(key=lambda x: x['chunk_id'])print(f"\nModel Output Analysis ({len(successful_results)}/{len(results)} servers succeeded):")print("-" * 80)# Get total sequence length from all chunkstotal_outputs = []for result in successful_results:total_outputs.extend(result['result']['outputs'])# Process the combined sequenceprint("\nProcessing complete sequence...")analysis = process_model_outputs(total_outputs, original_shape=(1, -1, 51289))if 'text' in analysis:print("\nGenerated Description:")print("-" * 80)print(analysis['text'])print("\nSequence Statistics:")print(f"- Logits shape: {analysis['logits_shape']}")print(f"- Distribution:")for key, value in analysis['distribution'].items():print(f" {key}: {value:.4f}")else:print("\nBasic Analysis (Florence processor not available):")print(f"- Sequence length: {len(total_outputs)}")print(f"- Overall activation: {analysis['overall_mean']:.4f} ± {analysis['overall_std']:.4f}")print("\nValue Distribution:")for key, value in analysis['distribution'].items():print(f"- {key}: {value:.4f}")# Check server consistencyif len(successful_results) > 1:all_outputs = [np.array([x[0] for x in r['result']['outputs']])for r in successful_results]differences = [np.max(np.abs(all_outputs[0] - tensor))for tensor in all_outputs[1:]]print("\nServer Consistency:")if np.max(differences) < 1e-6:print("Successful servers provided identical results")else:print(f"Variations detected between servers (max diff: {np.max(differences):.6f})")# Print timing summarysuccessful_times = [r['time'] for r in successful_results]print(f"\nProcessing Time Summary:")print(f"- Average: {np.mean(successful_times):.2f}s")print(f"- Range: {min(successful_times):.2f}s - {max(successful_times):.2f}s")def main():# Server configurations with their respective chunk IDsservers = [("https://fred808-ilob.hf.space", 0),("https://fred808-tserv.hf.space", 1),("https://fred808-tserve2.hf.space", 2)]# Image path - using the same image for all serversimage_path = "sample_task/test1.png"print(f"\nTesting with image: {image_path}")# Create process poolwith multiprocessing.Pool() as pool:# Prepare arguments for each serverargs = [(server_url, image_path, chunk_id) for server_url, chunk_id in servers]# Run inference in parallelprint("\nStarting parallel inference across all servers...")results = pool.map(run_inference, args)# Display individual server resultsprint("\nServer Results:")print("-" * 80)for result in results:print(f"\nServer: {result['server']}")print(f"Chunk ID: {result['chunk_id']}")print(f"Success: {result['success']}")print(f"Time: {result['time']:.4f}s" if result['time'] else "Time: N/A")if result['success']:print(f"Output shape: {len(result['result']['outputs'])} elements")print("First few outputs:", result['result']['outputs'][:5])else:print(f"Error: {result['error']}")print("-" * 80)# Process and display combined resultsprocess_results(results)if __name__ == "__main__":main()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269import osimport numpy as npfrom PIL import Imageimport requestsimport timeimport multiprocessingimport jsonimport sysfrom typing import Tuple, List, Dict, Any# Add Florence model path to Python pathflorence_path = os.path.join(os.path.dirname(__file__), 'florence-2-large')sys.path.append(florence_path)try:from processing_florence2 import Florence2Processorfrom configuration_florence2 import Florence2Configimport torchimport torch.nn.functional as F# Initialize processor with local filesconfig = Florence2Config.from_json_file(os.path.join(florence_path, 'config.json'))processor = Florence2Processor(config)HAVE_PROCESSOR = Trueprint("Successfully loaded Florence processor")except Exception as e:print(f"Warning: Could not load Florence processor: {e}")print("Using basic output interpretation")HAVE_PROCESSOR = False# Task-specific configurationTASK = "<MORE_DETAILED_CAPTION>" # For detailed image captioning# Model configurationMODEL_ID = "microsoft/florence-2-base"def load_and_preprocess_image(image_path):# Load image and resize to 32x32img = Image.open(image_path)img = img.resize((32, 32))# Convert to numpy array and normalize to [0,1]img_array = np.array(img).astype(np.float32) / 255.0# Ensure array has shape (32, 32, 3)if len(img_array.shape) == 2:img_array = np.stack([img_array] * 3, axis=-1)# Add batch dimensionimg_array = img_array[np.newaxis, ...]# Convert tensor to list of single-element lists for APItensor_data = [[float(x)] for x in img_array.flatten()]return tensor_datadef run_inference(args: Tuple[str, str, int]) -> dict:"""Run inference on a specific server with given chunk ID."""server_url, image_path, chunk_id = argstry:print(f"\nProcessing server {server_url} with chunk {chunk_id}...")# Load and preprocess imageinput_tensor = load_and_preprocess_image(image_path)# Prepare request datadata = {"inputs": input_tensor}# Send request with timeoutprint(f"Sending request to {server_url}/compute/{chunk_id}")start_time = time.time()response = requests.post(f"{server_url}/compute/{chunk_id}",json=data,headers={"Content-Type": "application/json"},timeout=10)inference_time = time.time() - start_timeif response.status_code == 200:result = response.json()return {"server": server_url,"chunk_id": chunk_id,"success": True,"time": inference_time,"result": result}else:error_msg = f"HTTP {response.status_code}"if hasattr(response, 'text'):error_msg += f": {response.text}"return {"server": server_url,"chunk_id": chunk_id,"success": False,"error": error_msg,"time": inference_time}except Exception as e:return {"server": server_url,"chunk_id": chunk_id,"success": False,"error": str(e),"time": time.time() - start_time if 'start_time' in locals() else None}def process_model_outputs(outputs, original_shape=(1, -1, 51289)):"""Process model outputs using Florence processor for sequence generation."""# Convert outputs to numpy arrayoutputs_array = np.array([x[0] for x in outputs])if HAVE_PROCESSOR:try:# Reshape logits to [batch, seq_len, vocab_size]logits = outputs_array.reshape(original_shape)if torch.is_tensor(logits):# Use torch operations if availabletoken_ids = torch.argmax(logits, dim=-1)else:# Fallback to numpytoken_ids = np.argmax(logits, axis=-1)# Decode tokens to texttext = processor.batch_decode(token_ids, skip_special_tokens=True)# Post-process for the specific taskprocessed_text = processor.post_process_generation(text[0] if isinstance(text, list) else text,task=TASK)return {'text': processed_text,'tokens': token_ids.tolist() if torch.is_tensor(token_ids) else token_ids.tolist(),'logits_shape': logits.shape,'distribution': {'min': float(outputs_array.min()),'max': float(outputs_array.max()),'mean': float(outputs_array.mean()),'std': float(outputs_array.std())}}except Exception as e:print(f"Warning: Error in sequence processing: {e}")# Fallback to basic statistics if processor not availablereturn {'overall_mean': float(outputs_array.mean()),'overall_std': float(outputs_array.std()),'shape': outputs_array.shape,'distribution': {'min': float(outputs_array.min()),'max': float(outputs_array.max()),'median': float(np.median(outputs_array))}}def process_results(results):"""Process and combine results from all servers."""# Filter successful resultssuccessful_results = [r for r in results if r['success']]if not successful_results:print("\nError: No servers returned successful results")return# Sort successful results by chunk IDsuccessful_results.sort(key=lambda x: x['chunk_id'])print(f"\nModel Output Analysis ({len(successful_results)}/{len(results)} servers succeeded):")print("-" * 80)# Get total sequence length from all chunkstotal_outputs = []for result in successful_results:total_outputs.extend(result['result']['outputs'])# Process the combined sequenceprint("\nProcessing complete sequence...")analysis = process_model_outputs(total_outputs, original_shape=(1, -1, 51289))if 'text' in analysis:print("\nGenerated Description:")print("-" * 80)print(analysis['text'])print("\nSequence Statistics:")print(f"- Logits shape: {analysis['logits_shape']}")print(f"- Distribution:")for key, value in analysis['distribution'].items():print(f" {key}: {value:.4f}")else:print("\nBasic Analysis (Florence processor not available):")print(f"- Sequence length: {len(total_outputs)}")print(f"- Overall activation: {analysis['overall_mean']:.4f} ± {analysis['overall_std']:.4f}")print("\nValue Distribution:")for key, value in analysis['distribution'].items():print(f"- {key}: {value:.4f}")# Check server consistencyif len(successful_results) > 1:all_outputs = [np.array([x[0] for x in r['result']['outputs']])for r in successful_results]differences = [np.max(np.abs(all_outputs[0] - tensor))for tensor in all_outputs[1:]]print("\nServer Consistency:")if np.max(differences) < 1e-6:print("Successful servers provided identical results")else:print(f"Variations detected between servers (max diff: {np.max(differences):.6f})")# Print timing summarysuccessful_times = [r['time'] for r in successful_results]print(f"\nProcessing Time Summary:")print(f"- Average: {np.mean(successful_times):.2f}s")print(f"- Range: {min(successful_times):.2f}s - {max(successful_times):.2f}s")def main():# Server configurations with their respective chunk IDsservers = [("https://fred808-ilob.hf.space", 0),("https://fred808-tserv.hf.space", 1),("https://fred808-tserve2.hf.space", 2)]# Image path - using the same image for all serversimage_path = "sample_task/test1.png"print(f"\nTesting with image: {image_path}")# Create process poolwith multiprocessing.Pool() as pool:# Prepare arguments for each serverargs = [(server_url, image_path, chunk_id) for server_url, chunk_id in servers]# Run inference in parallelprint("\nStarting parallel inference across all servers...")results = pool.map(run_inference, args)# Display individual server resultsprint("\nServer Results:")print("-" * 80)for result in results:print(f"\nServer: {result['server']}")print(f"Chunk ID: {result['chunk_id']}")print(f"Success: {result['success']}")print(f"Time: {result['time']:.4f}s" if result['time'] else "Time: N/A")if result['success']:print(f"Output shape: {len(result['result']['outputs'])} elements")print("First few outputs:", result['result']['outputs'][:5])else:print(f"Error: {result['error']}")print("-" * 80)# Process and display combined resultsprocess_results(results)if __name__ == "__main__":main()
app.py+268-268