| |
| """ |
| Test the updated multimodal AI backend service on port 8001 |
| """ |
|
|
| import requests |
| import json |
|
|
| |
| BASE_URL = "http://localhost:8001" |
|
|
| def test_multimodal_updated(): |
| """Test multimodal (image + text) chat completion with working model""" |
| print("πΌοΈ Testing multimodal chat completion with Salesforce/blip-image-captioning-base...") |
| |
| payload = { |
| "model": "Salesforce/blip-image-captioning-base", |
| "messages": [ |
| { |
| "role": "user", |
| "content": [ |
| { |
| "type": "image", |
| "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG" |
| }, |
| { |
| "type": "text", |
| "text": "What animal is on the candy?" |
| } |
| ] |
| } |
| ], |
| "max_tokens": 150, |
| "temperature": 0.7 |
| } |
| |
| try: |
| response = requests.post(f"{BASE_URL}/v1/chat/completions", json=payload, timeout=120) |
| if response.status_code == 200: |
| result = response.json() |
| print(f"β
Multimodal response: {result['choices'][0]['message']['content']}") |
| return True |
| else: |
| print(f"β Multimodal failed: {response.status_code} - {response.text}") |
| return False |
| except Exception as e: |
| print(f"β Multimodal error: {e}") |
| return False |
|
|
| def test_models_endpoint(): |
| """Test updated models endpoint""" |
| print("π Testing models endpoint...") |
| |
| try: |
| response = requests.get(f"{BASE_URL}/v1/models", timeout=10) |
| if response.status_code == 200: |
| result = response.json() |
| model_ids = [model['id'] for model in result['data']] |
| print(f"β
Available models: {model_ids}") |
| |
| if "Salesforce/blip-image-captioning-base" in model_ids: |
| print("β
Vision model is available!") |
| return True |
| else: |
| print("β οΈ Vision model not listed") |
| return False |
| else: |
| print(f"β Models endpoint failed: {response.status_code}") |
| return False |
| except Exception as e: |
| print(f"β Models endpoint error: {e}") |
| return False |
|
|
| def test_text_only_updated(): |
| """Test text-only functionality on new port""" |
| print("π¬ Testing text-only chat completion...") |
| |
| payload = { |
| "model": "microsoft/DialoGPT-medium", |
| "messages": [ |
| {"role": "user", "content": "Hello! How are you today?"} |
| ], |
| "max_tokens": 100, |
| "temperature": 0.7 |
| } |
| |
| try: |
| response = requests.post(f"{BASE_URL}/v1/chat/completions", json=payload, timeout=30) |
| if response.status_code == 200: |
| result = response.json() |
| print(f"β
Text response: {result['choices'][0]['message']['content']}") |
| return True |
| else: |
| print(f"β Text failed: {response.status_code} - {response.text}") |
| return False |
| except Exception as e: |
| print(f"β Text error: {e}") |
| return False |
|
|
| def test_image_only(): |
| """Test with image only (no text)""" |
| print("πΌοΈ Testing image-only analysis...") |
| |
| payload = { |
| "model": "Salesforce/blip-image-captioning-base", |
| "messages": [ |
| { |
| "role": "user", |
| "content": [ |
| { |
| "type": "image", |
| "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG" |
| } |
| ] |
| } |
| ], |
| "max_tokens": 100, |
| "temperature": 0.7 |
| } |
| |
| try: |
| response = requests.post(f"{BASE_URL}/v1/chat/completions", json=payload, timeout=60) |
| if response.status_code == 200: |
| result = response.json() |
| print(f"β
Image-only response: {result['choices'][0]['message']['content']}") |
| return True |
| else: |
| print(f"β Image-only failed: {response.status_code} - {response.text}") |
| return False |
| except Exception as e: |
| print(f"β Image-only error: {e}") |
| return False |
|
|
| def main(): |
| """Run all tests for updated service""" |
| print("π Testing Updated Multimodal AI Backend (Port 8001)...\n") |
| |
| tests = [ |
| ("Models Endpoint", test_models_endpoint), |
| ("Text-only Chat", test_text_only_updated), |
| ("Image-only Analysis", test_image_only), |
| ("Multimodal Chat", test_multimodal_updated), |
| ] |
| |
| passed = 0 |
| total = len(tests) |
| |
| for test_name, test_func in tests: |
| print(f"\n--- {test_name} ---") |
| if test_func(): |
| passed += 1 |
| print() |
| |
| print(f"π― Test Results: {passed}/{total} tests passed") |
| |
| if passed == total: |
| print("π All tests passed! Multimodal AI backend is fully working!") |
| print("π₯ Your backend now supports:") |
| print(" β
Text-only chat completions") |
| print(" β
Image analysis and captioning") |
| print(" β
Multimodal image+text conversations") |
| print(" β
OpenAI-compatible API format") |
| else: |
| print("β οΈ Some tests failed. Check the output above for details.") |
|
|
| if __name__ == "__main__": |
| main() |
|
|