Download test_working_models.py from cong182/firstAI: direct link, hf CLI and curl.
- Browser
- Download file 3.41 kB
-
https://huggingface.co/spaces/cong182/firstAI/resolve/db8cd85abd6bedbbcf5f3f371400a8f749ff3d93/test_working_models.py
- Command line
-
hf download hf://spaces/cong182/firstAI@db8cd85abd6bedbbcf5f3f371400a8f749ff3d93/test_working_models.py
-
curl -L -o test_working_models.py https://huggingface.co/spaces/cong182/firstAI/resolve/db8cd85abd6bedbbcf5f3f371400a8f749ff3d93/test_working_models.py
3.41 kB
| #!/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") | |