Spaces:
Runtime error
Runtime error
| """ | |
| 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}") |