Upload models/msp/api/api_client_example.py with huggingface_hub
Browse files
models/msp/api/api_client_example.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Example client for interacting with the Mistral 7B AHB2APB API
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
import requests
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# API configuration
|
| 10 |
+
API_BASE_URL = "http://localhost:8000"
|
| 11 |
+
|
| 12 |
+
def check_api_health():
|
| 13 |
+
"""Check if the API is running and healthy"""
|
| 14 |
+
try:
|
| 15 |
+
response = requests.get(f"{API_BASE_URL}/health")
|
| 16 |
+
response.raise_for_status()
|
| 17 |
+
health = response.json()
|
| 18 |
+
print("✓ API is healthy!")
|
| 19 |
+
print(f" Model: {health['model_path']}")
|
| 20 |
+
print(f" Device: {health['device']}")
|
| 21 |
+
print(f" Model loaded: {health['model_loaded']}")
|
| 22 |
+
return True
|
| 23 |
+
except requests.exceptions.ConnectionError:
|
| 24 |
+
print("✗ Cannot connect to API. Is the server running?")
|
| 25 |
+
print(f" Start it with: python api_server.py")
|
| 26 |
+
return False
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"✗ Error checking API health: {e}")
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
+
def generate(prompt: str, max_length: int = 512, temperature: float = 0.7):
|
| 32 |
+
"""Generate text using the API"""
|
| 33 |
+
try:
|
| 34 |
+
response = requests.post(
|
| 35 |
+
f"{API_BASE_URL}/api/generate",
|
| 36 |
+
json={
|
| 37 |
+
"prompt": prompt,
|
| 38 |
+
"max_length": max_length,
|
| 39 |
+
"temperature": temperature
|
| 40 |
+
},
|
| 41 |
+
timeout=120
|
| 42 |
+
)
|
| 43 |
+
response.raise_for_status()
|
| 44 |
+
result = response.json()
|
| 45 |
+
return result['response']
|
| 46 |
+
except requests.exceptions.RequestException as e:
|
| 47 |
+
print(f"Error calling API: {e}")
|
| 48 |
+
if hasattr(e.response, 'text'):
|
| 49 |
+
print(f"Response: {e.response.text}")
|
| 50 |
+
return None
|
| 51 |
+
|
| 52 |
+
def generate_batch(prompts: list, max_length: int = 512, temperature: float = 0.7):
|
| 53 |
+
"""Generate text for multiple prompts in batch"""
|
| 54 |
+
try:
|
| 55 |
+
requests_data = [
|
| 56 |
+
{
|
| 57 |
+
"prompt": prompt,
|
| 58 |
+
"max_length": max_length,
|
| 59 |
+
"temperature": temperature
|
| 60 |
+
}
|
| 61 |
+
for prompt in prompts
|
| 62 |
+
]
|
| 63 |
+
|
| 64 |
+
response = requests.post(
|
| 65 |
+
f"{API_BASE_URL}/api/generate/batch",
|
| 66 |
+
json=requests_data,
|
| 67 |
+
timeout=300 # Longer timeout for batch
|
| 68 |
+
)
|
| 69 |
+
response.raise_for_status()
|
| 70 |
+
result = response.json()
|
| 71 |
+
return [item['response'] for item in result['results']]
|
| 72 |
+
except requests.exceptions.RequestException as e:
|
| 73 |
+
print(f"Error calling batch API: {e}")
|
| 74 |
+
if hasattr(e.response, 'text'):
|
| 75 |
+
print(f"Response: {e.response.text}")
|
| 76 |
+
return None
|
| 77 |
+
|
| 78 |
+
def main():
|
| 79 |
+
"""Example usage"""
|
| 80 |
+
print("=" * 70)
|
| 81 |
+
print("Mistral 7B AHB2APB API Client Example")
|
| 82 |
+
print("=" * 70)
|
| 83 |
+
print()
|
| 84 |
+
|
| 85 |
+
# Check health
|
| 86 |
+
if not check_api_health():
|
| 87 |
+
return
|
| 88 |
+
|
| 89 |
+
print()
|
| 90 |
+
print("=" * 70)
|
| 91 |
+
print("Generating Response")
|
| 92 |
+
print("=" * 70)
|
| 93 |
+
print()
|
| 94 |
+
|
| 95 |
+
# Example prompt for AHB to APB conversion
|
| 96 |
+
prompt = "Convert this AHB burst to APB"
|
| 97 |
+
|
| 98 |
+
print(f"Prompt: {prompt}")
|
| 99 |
+
print()
|
| 100 |
+
print("Response:")
|
| 101 |
+
print("-" * 70)
|
| 102 |
+
|
| 103 |
+
response = generate(prompt, max_length=512, temperature=0.7)
|
| 104 |
+
|
| 105 |
+
if response:
|
| 106 |
+
print(response)
|
| 107 |
+
print("-" * 70)
|
| 108 |
+
else:
|
| 109 |
+
print("Failed to generate response")
|
| 110 |
+
|
| 111 |
+
print()
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
main()
|
| 115 |
+
|