#!/usr/bin/env python3 """ Test different HuggingFace approaches to find a working method """ import os import requests import json from huggingface_hub import InferenceClient import traceback # HuggingFace token HF_TOKEN = os.environ.get("HF_TOKEN", "") def test_inference_api_direct(model_name, prompt="Hello, how are you?"): """Test using direct HTTP requests to HuggingFace API""" print(f"\n🌐 Testing direct HTTP API for: {model_name}") headers = { "Authorization": f"Bearer {HF_TOKEN}" if HF_TOKEN else "", "Content-Type": "application/json" } url = f"https://api-inference.huggingface.co/models/{model_name}" payload = { "inputs": prompt, "parameters": { "max_new_tokens": 50, "temperature": 0.7, "top_p": 0.95, "do_sample": True } } try: response = requests.post(url, headers=headers, json=payload, timeout=30) print(f"Status: {response.status_code}") if response.status_code == 200: result = response.json() print(f"āœ… Success: {result}") return True else: print(f"āŒ Error: {response.text}") return False except Exception as e: print(f"āŒ Exception: {e}") return False def test_serverless_models(): """Test known working models that support serverless inference""" # List of models that typically work well with serverless inference working_models = [ "microsoft/DialoGPT-medium", "google/flan-t5-base", "distilbert-base-uncased-finetuned-sst-2-english", "gpt2", "microsoft/DialoGPT-small", "facebook/blenderbot-400M-distill" ] results = {} for model in working_models: result = test_inference_api_direct(model) results[model] = result return results def test_chat_completion_models(): """Test models specifically for chat completion""" chat_models = [ "microsoft/DialoGPT-medium", "facebook/blenderbot-400M-distill", "microsoft/DialoGPT-small" ] for model in chat_models: print(f"\nšŸ’¬ Testing chat model: {model}") test_inference_api_direct(model, "Human: Hello! How are you?\nAssistant:") if __name__ == "__main__": print("šŸ” HuggingFace Inference API Debug") print("=" * 50) if HF_TOKEN: print(f"šŸ”‘ Using HF_TOKEN: {HF_TOKEN[:10]}...") else: print("āš ļø No HF_TOKEN - trying anonymous access") # Test serverless models print("\n" + "="*60) print("TESTING SERVERLESS MODELS") print("="*60) results = test_serverless_models() # Test chat completion models print("\n" + "="*60) print("TESTING CHAT MODELS") print("="*60) test_chat_completion_models() # Summary print("\n" + "="*60) print("SUMMARY") print("="*60) working_models = [model for model, result in results.items() if result] if working_models: print("āœ… Working models:") for model in working_models: print(f" - {model}") print(f"\nšŸŽÆ Recommended model to switch to: {working_models[0]}") else: print("āŒ No models working - API might be down or authentication issue")