Spaces:
Runtime error
Runtime error
File size: 4,382 Bytes
d60ca67 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
"""
Example client for the AI Model Runner API
"""
import json
import requests
from typing import List, Dict, Any
class AIModelRunnerClient:
def __init__(self, base_url: str = "http://localhost:8000"):
self.base_url = base_url.rstrip("/")
def get_api_info(self) -> Dict[str, Any]:
"""Get API information"""
response = requests.get(f"{self.base_url}/")
return response.json()
def health_check(self) -> Dict[str, Any]:
"""Check API health"""
response = requests.get(f"{self.base_url}/health")
return response.json()
def list_models(self) -> Dict[str, Any]:
"""List available models"""
response = requests.get(f"{self.base_url}/models")
return response.json()
def chat(self, messages: List[Dict[str, str]], **kwargs) -> Dict[str, Any]:
"""Send chat message"""
data = {
"messages": messages,
"model": kwargs.get("model", "microsoft/DialoGPT-medium"),
"max_length": kwargs.get("max_length", 100),
"temperature": kwargs.get("temperature", 0.7)
}
response = requests.post(f"{self.base_url}/chat", json=data)
return response.json()
def analyze_code(self, code: str, task: str, language: str = "python") -> Dict[str, Any]:
"""Analyze code"""
data = {
"code": code,
"task": task,
"language": language
}
response = requests.post(f"{self.base_url}/code", json=data)
return response.json()
def reasoning(self, problem: str, context: str = "", steps: int = 5) -> Dict[str, Any]:
"""Perform reasoning"""
data = {
"problem": problem,
"context": context,
"steps": steps
}
response = requests.post(f"{self.base_url}/reasoning", json=data)
return response.json()
def analyze_sentiment(self, text: str) -> Dict[str, Any]:
"""Analyze sentiment"""
data = {"text": text}
response = requests.post(f"{self.base_url}/analyze-sentiment", json=data)
return response.json()
def demo():
"""Demonstrate API usage"""
client = AIModelRunnerClient()
print("=== AI Model Runner API Demo ===\n")
# Check API status
print("1. API Status:")
status = client.health_check()
print(f" Status: {status}")
print()
# List models
print("2. Available Models:")
models = client.list_models()
for model in models["models"]:
print(f" - {model['name']} ({model['type']}): {'✓' if model['loaded'] else '✗'}")
print()
# Chat example
print("3. Chat Example:")
chat_response = client.chat([
{"role": "user", "content": "Hello! How can you help me today?"}
])
print(f" User: Hello! How can you help me today?")
print(f" AI: {chat_response['response']}")
print()
# Code analysis example
print("4. Code Analysis Example:")
code = """
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n-1) + fibonacci(n-2)
"""
code_response = client.analyze_code(code, "explain", "python")
print(" Original Code:")
print(code)
print(" Analysis:")
print(code_response["result"])
print()
# Reasoning example
print("5. Reasoning Example:")
reasoning_response = client.reasoning(
problem="How to implement an efficient sorting algorithm?",
context="Working with large datasets",
steps=3
)
print(f" Problem: How to implement an efficient sorting algorithm?")
print(" Reasoning:")
print(reasoning_response["reasoning"])
print()
# Sentiment analysis example
print("6. Sentiment Analysis Example:")
sentiment = client.analyze_sentiment("I love using this AI API! It's fantastic and very helpful.")
print(f" Text: I love using this AI API! It's fantastic and very helpful.")
print(f" Sentiment: {sentiment['sentiment']}")
print(f" Confidence: {sentiment['confidence']:.2%}")
print()
if __name__ == "__main__":
try:
demo()
except requests.exceptions.ConnectionError:
print("Error: Cannot connect to the API. Make sure the server is running on http://localhost:8000")
except Exception as e:
print(f"Error: {e}") |