sheikhcoders commited on
Commit
d60ca67
·
verified ·
1 Parent(s): 10674e9

Upload client_example.py with huggingface_hub

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