CompactAI commited on
Commit
d0d9725
·
verified ·
1 Parent(s): 7835d3d

Delete example_api.py

Browse files
Files changed (1) hide show
  1. example_api.py +0 -89
example_api.py DELETED
@@ -1,89 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Example: Call the AIFinder public API to classify text.
4
-
5
- Usage:
6
- python example_api.py
7
-
8
- Requirements:
9
- pip install requests
10
-
11
- IMPORTANT — Strip <think>/<thinking> tags!
12
- Many reasoning models wrap chain-of-thought in <think>…</think> or
13
- <thinking>…</thinking> blocks. These tags confuse the classifier because
14
- they are NOT part of the model's actual output style. The API strips them
15
- automatically, but you should also strip them on your side to avoid sending
16
- unnecessary data.
17
-
18
- API details:
19
- POST /v1/classify
20
- No API key required. Rate limit: 60 requests/minute per IP.
21
- """
22
-
23
- import re
24
- import json
25
- import requests
26
-
27
- # Change this to your server URL (local or HuggingFace Space).
28
- API_URL = "https://huggingface.co/spaces/CompactAI/AIFinder/v1/classify"
29
-
30
- # Number of top results to return (default on server is 5).
31
- TOP_N = 5
32
-
33
- EXAMPLE_TEXT = """\
34
- I'd be happy to help you understand how neural networks work!
35
-
36
- Neural networks are computational models inspired by the human brain. They consist of layers of interconnected nodes (neurons) that process information. Here's a breakdown:
37
-
38
- 1. **Input Layer**: Receives the raw data
39
- 2. **Hidden Layers**: Process and transform the data through weighted connections
40
- 3. **Output Layer**: Produces the final prediction
41
-
42
- Each connection has a weight, and each neuron has a bias. During training, the network adjusts these weights using backpropagation to minimize the difference between predicted and actual outputs.
43
-
44
- The key insight is that by stacking multiple layers, neural networks can learn increasingly abstract representations of data, enabling them to solve complex tasks like image recognition, natural language processing, and more.
45
-
46
- Would you like me to dive deeper into any specific aspect?
47
- """
48
-
49
-
50
- def strip_think_tags(text: str) -> str:
51
- """Remove <think>…</think> and <thinking>…</thinking> blocks."""
52
- return re.sub(
53
- r"<think(?:ing)?>.*?</think(?:ing)?>", "", text, flags=re.DOTALL
54
- ).strip()
55
-
56
-
57
- def classify(text: str, top_n: int = TOP_N) -> dict:
58
- """Send text to the AIFinder API and return the JSON response."""
59
- cleaned = strip_think_tags(text)
60
- resp = requests.post(
61
- API_URL,
62
- json={"text": cleaned, "top_n": top_n},
63
- timeout=30,
64
- )
65
- resp.raise_for_status()
66
- return resp.json()
67
-
68
-
69
- def main():
70
- print("AIFinder API Example")
71
- print("=" * 50)
72
- print(f"Endpoint : {API_URL}")
73
- print(f"Top-N : {TOP_N}")
74
- print()
75
-
76
- result = classify(EXAMPLE_TEXT)
77
-
78
- print(f"Best match : {result['provider']} ({result['confidence']:.1f}%)")
79
- print()
80
- print("Top providers:")
81
- for entry in result["top_providers"]:
82
- bar = "█" * int(entry["confidence"] / 5) + "░" * (
83
- 20 - int(entry["confidence"] / 5)
84
- )
85
- print(f" {entry['name']:.<25s} {entry['confidence']:5.1f}% {bar}")
86
-
87
-
88
- if __name__ == "__main__":
89
- main()