Commit
·
e3fd5ce
1
Parent(s):
38165ef
api endpoints for UI migration v5
Browse files- API_DOCUMENTATION.md +76 -29
- API_ENDPOINTS_IMPLEMENTATION_COMPLETE.md +25 -11
- API_QUICK_REFERENCE.md +45 -13
- app.py +2 -1
API_DOCUMENTATION.md
CHANGED
|
@@ -97,6 +97,38 @@ client = Client(
|
|
| 97 |
|
| 98 |
---
|
| 99 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
## API Endpoints
|
| 101 |
|
| 102 |
### 1. Chat Handler - `/safe_gpu_chat_handler`
|
|
@@ -115,7 +147,7 @@ Process user messages and get AI responses.
|
|
| 115 |
|-----------|------|----------|---------|-------------|
|
| 116 |
| `message` | `str` | Yes | - | User message (max 10,000 characters) |
|
| 117 |
| `history` | `list[dict]` | No | `[]` | Chat history in Gradio format |
|
| 118 |
-
| `user_id` | `
|
| 119 |
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Session information string |
|
| 120 |
|
| 121 |
#### Response Structure
|
|
@@ -195,7 +227,7 @@ Returns a tuple of **7 elements**:
|
|
| 195 |
|
| 196 |
**5. `session_info` (str)**
|
| 197 |
```python
|
| 198 |
-
"Session: abc12345 | User:
|
| 199 |
```
|
| 200 |
|
| 201 |
**6. `skills_html` (str)**
|
|
@@ -212,12 +244,21 @@ from gradio_client import Client
|
|
| 212 |
|
| 213 |
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 214 |
|
| 215 |
-
# Make chat request
|
| 216 |
result = client.predict(
|
| 217 |
message="Explain quantum computing in simple terms",
|
| 218 |
history=[],
|
| 219 |
-
user_id="
|
| 220 |
-
session_text="Session: abc12345 | User:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 221 |
api_name="/safe_gpu_chat_handler"
|
| 222 |
)
|
| 223 |
|
|
@@ -236,7 +277,12 @@ print(f"Response time: {processing_time:.2f}s")
|
|
| 236 |
#### Validation Rules
|
| 237 |
|
| 238 |
- **Message**: Must be non-empty string, max 10,000 characters
|
| 239 |
-
- **User ID**:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 240 |
- **History**: Must be list (empty list if None)
|
| 241 |
- **Session Text**: Format: `"Session: <8-char-id> | User: <user_id> | Interactions: <count>"`
|
| 242 |
|
|
@@ -268,14 +314,14 @@ Create a new conversation session.
|
|
| 268 |
|
| 269 |
| Parameter | Type | Required | Default | Description |
|
| 270 |
|-----------|------|----------|---------|-------------|
|
| 271 |
-
| `user_id` | `
|
| 272 |
|
| 273 |
#### Response
|
| 274 |
|
| 275 |
Returns a single string:
|
| 276 |
|
| 277 |
```python
|
| 278 |
-
"Session: abc12345 | User:
|
| 279 |
```
|
| 280 |
|
| 281 |
#### Code Example
|
|
@@ -293,7 +339,8 @@ print(session_info) # "Session: xyz67890 | User: Admin_J | Interactions: 0"
|
|
| 293 |
#### Behavior
|
| 294 |
|
| 295 |
- Generates new 8-character hexadecimal session ID
|
| 296 |
-
- Validates and normalizes user_id
|
|
|
|
| 297 |
- Initializes session in database via context_manager
|
| 298 |
- Returns formatted session info string
|
| 299 |
- Continues execution even if database initialization fails
|
|
@@ -314,7 +361,7 @@ Update session metadata (typically called when user changes).
|
|
| 314 |
|
| 315 |
| Parameter | Type | Required | Default | Description |
|
| 316 |
|-----------|------|----------|---------|-------------|
|
| 317 |
-
| `user_id` | `
|
| 318 |
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Current session text |
|
| 319 |
|
| 320 |
#### Response
|
|
@@ -330,7 +377,7 @@ Returns updated session info string:
|
|
| 330 |
```python
|
| 331 |
result = client.predict(
|
| 332 |
user_id="Admin_J",
|
| 333 |
-
session_text="Session: abc12345 | User:
|
| 334 |
api_name="/update_session_info"
|
| 335 |
)
|
| 336 |
|
|
@@ -438,7 +485,7 @@ Returns status message string:
|
|
| 438 |
```python
|
| 439 |
result = client.predict(
|
| 440 |
mode="relevant",
|
| 441 |
-
session_id_text="Session: abc12345 | User:
|
| 442 |
api_name="/handle_mode_change"
|
| 443 |
)
|
| 444 |
|
|
@@ -602,8 +649,8 @@ def safe_chat_request(message, history, max_retries=3):
|
|
| 602 |
result = client.predict(
|
| 603 |
message=message,
|
| 604 |
history=history,
|
| 605 |
-
user_id="
|
| 606 |
-
session_text="Session: abc12345 | User:
|
| 607 |
api_name="/safe_gpu_chat_handler"
|
| 608 |
)
|
| 609 |
|
|
@@ -646,10 +693,10 @@ class ResearchAIAssistant:
|
|
| 646 |
hf_token=hf_token or os.getenv("HF_TOKEN")
|
| 647 |
)
|
| 648 |
self.session_info = None
|
| 649 |
-
self.user_id = "
|
| 650 |
self.chat_history = []
|
| 651 |
|
| 652 |
-
def create_session(self, user_id: str = "
|
| 653 |
"""Create new session"""
|
| 654 |
self.user_id = user_id
|
| 655 |
self.session_info = self.client.predict(
|
|
@@ -752,7 +799,7 @@ async def async_chat_request(message: str, history: List, session_text: str):
|
|
| 752 |
lambda: client.predict(
|
| 753 |
message=message,
|
| 754 |
history=history,
|
| 755 |
-
user_id="
|
| 756 |
session_text=session_text,
|
| 757 |
api_name="/safe_gpu_chat_handler"
|
| 758 |
)
|
|
@@ -765,7 +812,7 @@ async def main():
|
|
| 765 |
result = await async_chat_request(
|
| 766 |
message="Hello",
|
| 767 |
history=[],
|
| 768 |
-
session_text="Session: abc12345 | User:
|
| 769 |
)
|
| 770 |
print(result[0][-1]["content"])
|
| 771 |
|
|
@@ -794,7 +841,7 @@ client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
|
| 794 |
|
| 795 |
```python
|
| 796 |
session_info = client.predict(
|
| 797 |
-
user_id="
|
| 798 |
api_name="/new_session"
|
| 799 |
)
|
| 800 |
```
|
|
@@ -805,7 +852,7 @@ session_info = client.predict(
|
|
| 805 |
result = client.predict(
|
| 806 |
message="Your question here",
|
| 807 |
history=[],
|
| 808 |
-
user_id="
|
| 809 |
session_text=session_info,
|
| 810 |
api_name="/safe_gpu_chat_handler"
|
| 811 |
)
|
|
@@ -855,7 +902,7 @@ class TestResearchAIAssistant(unittest.TestCase):
|
|
| 855 |
def test_create_session(self):
|
| 856 |
"""Test session creation"""
|
| 857 |
result = self.client.predict(
|
| 858 |
-
user_id="
|
| 859 |
api_name="/new_session"
|
| 860 |
)
|
| 861 |
self.assertIsInstance(result, str)
|
|
@@ -870,7 +917,7 @@ class TestResearchAIAssistant(unittest.TestCase):
|
|
| 870 |
result = self.client.predict(
|
| 871 |
message="Hello",
|
| 872 |
history=[],
|
| 873 |
-
user_id="
|
| 874 |
session_text=self.session_info,
|
| 875 |
api_name="/safe_gpu_chat_handler"
|
| 876 |
)
|
|
@@ -904,7 +951,7 @@ def test_full_conversation_flow():
|
|
| 904 |
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 905 |
|
| 906 |
# 1. Create session
|
| 907 |
-
session = client.predict(user_id="
|
| 908 |
assert "Session:" in session
|
| 909 |
|
| 910 |
# 2. Set context mode
|
|
@@ -916,7 +963,7 @@ def test_full_conversation_flow():
|
|
| 916 |
result = client.predict(
|
| 917 |
message=message,
|
| 918 |
history=history,
|
| 919 |
-
user_id="
|
| 920 |
session_text=session,
|
| 921 |
api_name="/safe_gpu_chat_handler"
|
| 922 |
)
|
|
@@ -1036,7 +1083,7 @@ for attempt in range(max_retries):
|
|
| 1036 |
**Symptom**: `ValueError: Could not extract session_id`
|
| 1037 |
**Solution**: Ensure session_text format is correct:
|
| 1038 |
```
|
| 1039 |
-
"Session: abc12345 | User:
|
| 1040 |
```
|
| 1041 |
|
| 1042 |
#### 3. Empty Responses
|
|
@@ -1078,7 +1125,7 @@ context = result[4] # Check session context
|
|
| 1078 |
#### Validate Session Info
|
| 1079 |
|
| 1080 |
```python
|
| 1081 |
-
session_info = "Session: abc12345 | User:
|
| 1082 |
|
| 1083 |
# Validate format
|
| 1084 |
import re
|
|
@@ -1175,7 +1222,7 @@ session = None
|
|
| 1175 |
history = []
|
| 1176 |
|
| 1177 |
# 2. Create session
|
| 1178 |
-
session = client.predict(user_id="
|
| 1179 |
print(f"Created: {session}")
|
| 1180 |
|
| 1181 |
# 3. Set context mode
|
|
@@ -1188,7 +1235,7 @@ for i in range(3):
|
|
| 1188 |
result = client.predict(
|
| 1189 |
message=user_message,
|
| 1190 |
history=history,
|
| 1191 |
-
user_id="
|
| 1192 |
session_text=session,
|
| 1193 |
api_name="/safe_gpu_chat_handler"
|
| 1194 |
)
|
|
@@ -1221,7 +1268,7 @@ def robust_chat_request(message, history, session_info, max_retries=3):
|
|
| 1221 |
result = client.predict(
|
| 1222 |
message=message,
|
| 1223 |
history=history,
|
| 1224 |
-
user_id="
|
| 1225 |
session_text=session_info,
|
| 1226 |
api_name="/safe_gpu_chat_handler"
|
| 1227 |
)
|
|
|
|
| 97 |
|
| 98 |
---
|
| 99 |
|
| 100 |
+
## User Management
|
| 101 |
+
|
| 102 |
+
### Base User
|
| 103 |
+
- `Admin_J` is the base/default API user
|
| 104 |
+
- Used when no `user_id` is provided or invalid format is provided
|
| 105 |
+
|
| 106 |
+
### Dynamic User Creation
|
| 107 |
+
The API supports dynamic user creation - any valid format `user_id` is automatically accepted and created in the database.
|
| 108 |
+
|
| 109 |
+
**Valid Format:**
|
| 110 |
+
- Alphanumeric characters + underscore only
|
| 111 |
+
- Length: 1-50 characters
|
| 112 |
+
- Pattern: `^[a-zA-Z0-9_]{1,50}$`
|
| 113 |
+
- Examples: `User123`, `External_API`, `MyUser_2024`, `API_Client_01`
|
| 114 |
+
|
| 115 |
+
**Auto-Creation:**
|
| 116 |
+
- New users are automatically inserted into the `user_contexts` database table on first use
|
| 117 |
+
- No manual user registration required
|
| 118 |
+
- User information is maintained by the backend database
|
| 119 |
+
|
| 120 |
+
**Validation:**
|
| 121 |
+
- Valid formats are accepted and auto-created
|
| 122 |
+
- Invalid formats default to `Admin_J`
|
| 123 |
+
- Database automatically maintains user information
|
| 124 |
+
|
| 125 |
+
### UI Restriction
|
| 126 |
+
- The HuggingFace Spaces UI is restricted to `ADMINONLY` user only
|
| 127 |
+
- Dynamic user creation is available **only via API calls**
|
| 128 |
+
- UI users cannot use dynamic user IDs
|
| 129 |
+
|
| 130 |
+
---
|
| 131 |
+
|
| 132 |
## API Endpoints
|
| 133 |
|
| 134 |
### 1. Chat Handler - `/safe_gpu_chat_handler`
|
|
|
|
| 147 |
|-----------|------|----------|---------|-------------|
|
| 148 |
| `message` | `str` | Yes | - | User message (max 10,000 characters) |
|
| 149 |
| `history` | `list[dict]` | No | `[]` | Chat history in Gradio format |
|
| 150 |
+
| `user_id` | `str` | No | `"Admin_J"` | User identifier. Base user is `Admin_J`. Any valid format user_id (alphanumeric + underscore, 1-50 chars) is accepted and automatically created in the database. |
|
| 151 |
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Session information string |
|
| 152 |
|
| 153 |
#### Response Structure
|
|
|
|
| 227 |
|
| 228 |
**5. `session_info` (str)**
|
| 229 |
```python
|
| 230 |
+
"Session: abc12345 | User: Admin_J | Interactions: 5"
|
| 231 |
```
|
| 232 |
|
| 233 |
**6. `skills_html` (str)**
|
|
|
|
| 244 |
|
| 245 |
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 246 |
|
| 247 |
+
# Make chat request (using base Admin_J user)
|
| 248 |
result = client.predict(
|
| 249 |
message="Explain quantum computing in simple terms",
|
| 250 |
history=[],
|
| 251 |
+
user_id="Admin_J",
|
| 252 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 0",
|
| 253 |
+
api_name="/safe_gpu_chat_handler"
|
| 254 |
+
)
|
| 255 |
+
|
| 256 |
+
# Make chat request (using dynamic user - auto-created)
|
| 257 |
+
result = client.predict(
|
| 258 |
+
message="What is machine learning?",
|
| 259 |
+
history=[],
|
| 260 |
+
user_id="MyNewUser_123", # New user - automatically created in DB
|
| 261 |
+
session_text="Session: abc12345 | User: MyNewUser_123 | Interactions: 0",
|
| 262 |
api_name="/safe_gpu_chat_handler"
|
| 263 |
)
|
| 264 |
|
|
|
|
| 277 |
#### Validation Rules
|
| 278 |
|
| 279 |
- **Message**: Must be non-empty string, max 10,000 characters
|
| 280 |
+
- **User ID**:
|
| 281 |
+
- Base user: `Admin_J` (default)
|
| 282 |
+
- Dynamic users: Any alphanumeric + underscore format (1-50 characters)
|
| 283 |
+
- New users are automatically created in the database on first use
|
| 284 |
+
- Invalid formats default to `Admin_J`
|
| 285 |
+
- Format validation: `^[a-zA-Z0-9_]{1,50}$`
|
| 286 |
- **History**: Must be list (empty list if None)
|
| 287 |
- **Session Text**: Format: `"Session: <8-char-id> | User: <user_id> | Interactions: <count>"`
|
| 288 |
|
|
|
|
| 314 |
|
| 315 |
| Parameter | Type | Required | Default | Description |
|
| 316 |
|-----------|------|----------|---------|-------------|
|
| 317 |
+
| `user_id` | `str` | No | `"Admin_J"` | User identifier. Base user is `Admin_J`. Any valid format user_id is accepted and automatically created in the database. |
|
| 318 |
|
| 319 |
#### Response
|
| 320 |
|
| 321 |
Returns a single string:
|
| 322 |
|
| 323 |
```python
|
| 324 |
+
"Session: abc12345 | User: Admin_J | Interactions: 0"
|
| 325 |
```
|
| 326 |
|
| 327 |
#### Code Example
|
|
|
|
| 339 |
#### Behavior
|
| 340 |
|
| 341 |
- Generates new 8-character hexadecimal session ID
|
| 342 |
+
- Validates and normalizes user_id (defaults to `Admin_J` if invalid)
|
| 343 |
+
- Auto-creates new users in database on first use
|
| 344 |
- Initializes session in database via context_manager
|
| 345 |
- Returns formatted session info string
|
| 346 |
- Continues execution even if database initialization fails
|
|
|
|
| 361 |
|
| 362 |
| Parameter | Type | Required | Default | Description |
|
| 363 |
|-----------|------|----------|---------|-------------|
|
| 364 |
+
| `user_id` | `str` | No | `"Admin_J"` | New user identifier. Base user is `Admin_J`. Any valid format user_id is accepted and automatically created in the database. |
|
| 365 |
| `session_text` | `str` | No | `"Session: ... \| User: ... \| Interactions: 0"` | Current session text |
|
| 366 |
|
| 367 |
#### Response
|
|
|
|
| 377 |
```python
|
| 378 |
result = client.predict(
|
| 379 |
user_id="Admin_J",
|
| 380 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 3",
|
| 381 |
api_name="/update_session_info"
|
| 382 |
)
|
| 383 |
|
|
|
|
| 485 |
```python
|
| 486 |
result = client.predict(
|
| 487 |
mode="relevant",
|
| 488 |
+
session_id_text="Session: abc12345 | User: Admin_J | Interactions: 3",
|
| 489 |
api_name="/handle_mode_change"
|
| 490 |
)
|
| 491 |
|
|
|
|
| 649 |
result = client.predict(
|
| 650 |
message=message,
|
| 651 |
history=history,
|
| 652 |
+
user_id="Admin_J",
|
| 653 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 0",
|
| 654 |
api_name="/safe_gpu_chat_handler"
|
| 655 |
)
|
| 656 |
|
|
|
|
| 693 |
hf_token=hf_token or os.getenv("HF_TOKEN")
|
| 694 |
)
|
| 695 |
self.session_info = None
|
| 696 |
+
self.user_id = "Admin_J"
|
| 697 |
self.chat_history = []
|
| 698 |
|
| 699 |
+
def create_session(self, user_id: str = "Admin_J") -> str:
|
| 700 |
"""Create new session"""
|
| 701 |
self.user_id = user_id
|
| 702 |
self.session_info = self.client.predict(
|
|
|
|
| 799 |
lambda: client.predict(
|
| 800 |
message=message,
|
| 801 |
history=history,
|
| 802 |
+
user_id="Admin_J",
|
| 803 |
session_text=session_text,
|
| 804 |
api_name="/safe_gpu_chat_handler"
|
| 805 |
)
|
|
|
|
| 812 |
result = await async_chat_request(
|
| 813 |
message="Hello",
|
| 814 |
history=[],
|
| 815 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 0"
|
| 816 |
)
|
| 817 |
print(result[0][-1]["content"])
|
| 818 |
|
|
|
|
| 841 |
|
| 842 |
```python
|
| 843 |
session_info = client.predict(
|
| 844 |
+
user_id="Admin_J",
|
| 845 |
api_name="/new_session"
|
| 846 |
)
|
| 847 |
```
|
|
|
|
| 852 |
result = client.predict(
|
| 853 |
message="Your question here",
|
| 854 |
history=[],
|
| 855 |
+
user_id="Admin_J",
|
| 856 |
session_text=session_info,
|
| 857 |
api_name="/safe_gpu_chat_handler"
|
| 858 |
)
|
|
|
|
| 902 |
def test_create_session(self):
|
| 903 |
"""Test session creation"""
|
| 904 |
result = self.client.predict(
|
| 905 |
+
user_id="Admin_J",
|
| 906 |
api_name="/new_session"
|
| 907 |
)
|
| 908 |
self.assertIsInstance(result, str)
|
|
|
|
| 917 |
result = self.client.predict(
|
| 918 |
message="Hello",
|
| 919 |
history=[],
|
| 920 |
+
user_id="Admin_J",
|
| 921 |
session_text=self.session_info,
|
| 922 |
api_name="/safe_gpu_chat_handler"
|
| 923 |
)
|
|
|
|
| 951 |
client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
| 952 |
|
| 953 |
# 1. Create session
|
| 954 |
+
session = client.predict(user_id="Admin_J", api_name="/new_session")
|
| 955 |
assert "Session:" in session
|
| 956 |
|
| 957 |
# 2. Set context mode
|
|
|
|
| 963 |
result = client.predict(
|
| 964 |
message=message,
|
| 965 |
history=history,
|
| 966 |
+
user_id="Admin_J",
|
| 967 |
session_text=session,
|
| 968 |
api_name="/safe_gpu_chat_handler"
|
| 969 |
)
|
|
|
|
| 1083 |
**Symptom**: `ValueError: Could not extract session_id`
|
| 1084 |
**Solution**: Ensure session_text format is correct:
|
| 1085 |
```
|
| 1086 |
+
"Session: abc12345 | User: Admin_J | Interactions: 0"
|
| 1087 |
```
|
| 1088 |
|
| 1089 |
#### 3. Empty Responses
|
|
|
|
| 1125 |
#### Validate Session Info
|
| 1126 |
|
| 1127 |
```python
|
| 1128 |
+
session_info = "Session: abc12345 | User: Admin_J | Interactions: 0"
|
| 1129 |
|
| 1130 |
# Validate format
|
| 1131 |
import re
|
|
|
|
| 1222 |
history = []
|
| 1223 |
|
| 1224 |
# 2. Create session
|
| 1225 |
+
session = client.predict(user_id="Admin_J", api_name="/new_session")
|
| 1226 |
print(f"Created: {session}")
|
| 1227 |
|
| 1228 |
# 3. Set context mode
|
|
|
|
| 1235 |
result = client.predict(
|
| 1236 |
message=user_message,
|
| 1237 |
history=history,
|
| 1238 |
+
user_id="Admin_J",
|
| 1239 |
session_text=session,
|
| 1240 |
api_name="/safe_gpu_chat_handler"
|
| 1241 |
)
|
|
|
|
| 1268 |
result = client.predict(
|
| 1269 |
message=message,
|
| 1270 |
history=history,
|
| 1271 |
+
user_id="Admin_J",
|
| 1272 |
session_text=session_info,
|
| 1273 |
api_name="/safe_gpu_chat_handler"
|
| 1274 |
)
|
API_ENDPOINTS_IMPLEMENTATION_COMPLETE.md
CHANGED
|
@@ -28,7 +28,9 @@ All API endpoints have been enhanced with comprehensive validation, error handli
|
|
| 28 |
|
| 29 |
**Key Features:**
|
| 30 |
- Validates message length (max 10,000 characters)
|
| 31 |
-
- Validates user_id
|
|
|
|
|
|
|
| 32 |
- Handles GPU decorator cleanup errors gracefully
|
| 33 |
- Preserves all process flow logging functionality
|
| 34 |
- Returns dynamic tuple matching interface components
|
|
@@ -46,7 +48,8 @@ All API endpoints have been enhanced with comprehensive validation, error handli
|
|
| 46 |
- ✅ Comprehensive logging
|
| 47 |
|
| 48 |
**Key Features:**
|
| 49 |
-
- Validates user_id (defaults to '
|
|
|
|
| 50 |
- Initializes session in database via context_manager
|
| 51 |
- Continues even if database initialization fails
|
| 52 |
- Returns formatted session info string
|
|
@@ -134,9 +137,12 @@ All API endpoints have been enhanced with comprehensive validation, error handli
|
|
| 134 |
|
| 135 |
### Core Helpers (Lines 666-823)
|
| 136 |
|
| 137 |
-
1. **`_validate_user_id(user_id: str) -> str`**
|
| 138 |
-
- Validates user_id
|
| 139 |
-
-
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
2. **`_extract_session_id(session_text: str, allow_generate: bool) -> str`**
|
| 142 |
- Robust parsing with multiple regex patterns
|
|
@@ -341,20 +347,28 @@ All endpoints:
|
|
| 341 |
### Example 1: Chat Handler with Validation
|
| 342 |
|
| 343 |
```python
|
| 344 |
-
# Valid call
|
| 345 |
result = safe_gpu_chat_handler(
|
| 346 |
message="Hello",
|
| 347 |
history=[],
|
| 348 |
-
user_id="
|
| 349 |
-
session_text="Session: abc12345 | User:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
)
|
| 351 |
|
| 352 |
# Invalid call (empty message) - returns error response
|
| 353 |
result = safe_gpu_chat_handler(
|
| 354 |
message="",
|
| 355 |
history=[],
|
| 356 |
-
user_id="
|
| 357 |
-
session_text="Session: abc12345 | User:
|
| 358 |
)
|
| 359 |
```
|
| 360 |
|
|
@@ -377,7 +391,7 @@ save_preferences(
|
|
| 377 |
# Update session info - preserves session ID
|
| 378 |
update_session_info(
|
| 379 |
user_id="Admin_J",
|
| 380 |
-
session_text="Session: abc12345 | User:
|
| 381 |
)
|
| 382 |
# Returns: "Session: abc12345 | User: Admin_J | Interactions: 5"
|
| 383 |
# Session ID preserved, interaction count from database
|
|
|
|
| 28 |
|
| 29 |
**Key Features:**
|
| 30 |
- Validates message length (max 10,000 characters)
|
| 31 |
+
- Validates user_id format (alphanumeric + underscore, 1-50 chars)
|
| 32 |
+
- Base user: Admin_J (default)
|
| 33 |
+
- Dynamic users: Any valid format automatically created in database
|
| 34 |
- Handles GPU decorator cleanup errors gracefully
|
| 35 |
- Preserves all process flow logging functionality
|
| 36 |
- Returns dynamic tuple matching interface components
|
|
|
|
| 48 |
- ✅ Comprehensive logging
|
| 49 |
|
| 50 |
**Key Features:**
|
| 51 |
+
- Validates user_id format (defaults to 'Admin_J' if invalid)
|
| 52 |
+
- Auto-creates new users in database on first use
|
| 53 |
- Initializes session in database via context_manager
|
| 54 |
- Continues even if database initialization fails
|
| 55 |
- Returns formatted session info string
|
|
|
|
| 137 |
|
| 138 |
### Core Helpers (Lines 666-823)
|
| 139 |
|
| 140 |
+
1. **`_validate_user_id(user_id: str, source: str = "api") -> str`**
|
| 141 |
+
- Validates user_id based on source (UI or API)
|
| 142 |
+
- UI source: Only allows 'ADMINONLY'
|
| 143 |
+
- API source: Accepts 'Admin_J' (base) or any valid format (auto-created)
|
| 144 |
+
- Returns 'Admin_J' if invalid format
|
| 145 |
+
- Auto-creates new users in database via `_ensure_user_exists()`
|
| 146 |
|
| 147 |
2. **`_extract_session_id(session_text: str, allow_generate: bool) -> str`**
|
| 148 |
- Robust parsing with multiple regex patterns
|
|
|
|
| 347 |
### Example 1: Chat Handler with Validation
|
| 348 |
|
| 349 |
```python
|
| 350 |
+
# Valid call (base Admin_J user)
|
| 351 |
result = safe_gpu_chat_handler(
|
| 352 |
message="Hello",
|
| 353 |
history=[],
|
| 354 |
+
user_id="Admin_J",
|
| 355 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 0"
|
| 356 |
+
)
|
| 357 |
+
|
| 358 |
+
# Valid call (dynamic user - auto-created)
|
| 359 |
+
result = safe_gpu_chat_handler(
|
| 360 |
+
message="Hello",
|
| 361 |
+
history=[],
|
| 362 |
+
user_id="ExternalAPI_User123", # New user - auto-created in DB
|
| 363 |
+
session_text="Session: abc12345 | User: ExternalAPI_User123 | Interactions: 0"
|
| 364 |
)
|
| 365 |
|
| 366 |
# Invalid call (empty message) - returns error response
|
| 367 |
result = safe_gpu_chat_handler(
|
| 368 |
message="",
|
| 369 |
history=[],
|
| 370 |
+
user_id="Admin_J",
|
| 371 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 0"
|
| 372 |
)
|
| 373 |
```
|
| 374 |
|
|
|
|
| 391 |
# Update session info - preserves session ID
|
| 392 |
update_session_info(
|
| 393 |
user_id="Admin_J",
|
| 394 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 5"
|
| 395 |
)
|
| 396 |
# Returns: "Session: abc12345 | User: Admin_J | Interactions: 5"
|
| 397 |
# Session ID preserved, interaction count from database
|
API_QUICK_REFERENCE.md
CHANGED
|
@@ -22,34 +22,61 @@ client = Client("JatinAutonomousLabs/Research_AI_Assistant")
|
|
| 22 |
|
| 23 |
---
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
## All Available Endpoints
|
| 26 |
|
| 27 |
### 1. Chat Handler
|
| 28 |
```python
|
|
|
|
| 29 |
result = client.predict(
|
| 30 |
message="Your question",
|
| 31 |
history=[],
|
| 32 |
-
user_id="
|
| 33 |
-
session_text="Session: abc12345 | User:
|
| 34 |
api_name="/safe_gpu_chat_handler"
|
| 35 |
)
|
| 36 |
# Returns: (history, input, reasoning, performance, context, session_info, skills)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
```
|
| 38 |
|
| 39 |
### 2. New Session
|
| 40 |
```python
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
session = client.predict(
|
| 42 |
-
user_id="
|
| 43 |
api_name="/new_session"
|
| 44 |
)
|
| 45 |
-
# Returns: "Session: xyz67890 | User:
|
| 46 |
```
|
| 47 |
|
| 48 |
### 3. Update Session Info
|
| 49 |
```python
|
| 50 |
updated = client.predict(
|
| 51 |
user_id="Admin_J",
|
| 52 |
-
session_text="Session: abc12345 | User:
|
| 53 |
api_name="/update_session_info"
|
| 54 |
)
|
| 55 |
# Returns: "Session: abc12345 | User: Admin_J | Interactions: 3"
|
|
@@ -69,7 +96,7 @@ client.predict(api_name="/toggle_settings_from_nav")
|
|
| 69 |
```python
|
| 70 |
status = client.predict(
|
| 71 |
mode="relevant",
|
| 72 |
-
session_id_text="Session: abc12345 | User:
|
| 73 |
api_name="/handle_mode_change"
|
| 74 |
)
|
| 75 |
# Returns: "*Current: Relevant Context*"
|
|
@@ -137,7 +164,7 @@ result = client.predict(
|
|
| 137 |
| Parameter | Validation |
|
| 138 |
|-----------|------------|
|
| 139 |
| `message` | Non-empty, max 10,000 chars |
|
| 140 |
-
| `user_id` |
|
| 141 |
| `mode` | One of: `'fresh'`, `'relevant'` |
|
| 142 |
| `response_speed` | One of: `'Fast'`, `'Balanced'`, `'Thorough'` |
|
| 143 |
| `session_text` | Format: `"Session: <8-hex> | User: <user_id> | Interactions: <num>"` |
|
|
@@ -148,8 +175,8 @@ result = client.predict(
|
|
| 148 |
|
| 149 |
### Full Conversation Flow
|
| 150 |
```python
|
| 151 |
-
# 1. Create session
|
| 152 |
-
session = client.predict(user_id="
|
| 153 |
|
| 154 |
# 2. Set context mode
|
| 155 |
client.predict(mode="relevant", session_id_text=session, api_name="/handle_mode_change")
|
|
@@ -159,12 +186,17 @@ history = []
|
|
| 159 |
for msg in ["Q1", "Q2", "Q3"]:
|
| 160 |
result = client.predict(
|
| 161 |
message=msg, history=history,
|
| 162 |
-
user_id="
|
| 163 |
api_name="/safe_gpu_chat_handler"
|
| 164 |
)
|
| 165 |
history = result[0]
|
| 166 |
session = result[5] # Update session
|
| 167 |
print(result[0][-1]["content"]) # Print response
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
```
|
| 169 |
|
| 170 |
### Error Handling
|
|
@@ -184,14 +216,14 @@ except Exception as e:
|
|
| 184 |
### `/safe_gpu_chat_handler`
|
| 185 |
- `message` (str, required)
|
| 186 |
- `history` (list, default: [])
|
| 187 |
-
- `user_id` (str, default: "
|
| 188 |
- `session_text` (str, default: "Session: ... | User: ... | Interactions: 0")
|
| 189 |
|
| 190 |
### `/new_session`
|
| 191 |
-
- `user_id` (str, default: "
|
| 192 |
|
| 193 |
### `/update_session_info`
|
| 194 |
-
- `user_id` (str, default: "
|
| 195 |
- `session_text` (str, required)
|
| 196 |
|
| 197 |
### `/handle_mode_change`
|
|
|
|
| 22 |
|
| 23 |
---
|
| 24 |
|
| 25 |
+
## User Management
|
| 26 |
+
|
| 27 |
+
**Base User:** `Admin_J` (default)
|
| 28 |
+
**Dynamic Users:** Any valid format (alphanumeric + underscore, 1-50 chars) - auto-created in DB
|
| 29 |
+
**Format:** `^[a-zA-Z0-9_]{1,50}$`
|
| 30 |
+
**UI Restriction:** HuggingFace Spaces UI limited to `ADMINONLY` only
|
| 31 |
+
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
## All Available Endpoints
|
| 35 |
|
| 36 |
### 1. Chat Handler
|
| 37 |
```python
|
| 38 |
+
# Using base Admin_J user
|
| 39 |
result = client.predict(
|
| 40 |
message="Your question",
|
| 41 |
history=[],
|
| 42 |
+
user_id="Admin_J",
|
| 43 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 0",
|
| 44 |
api_name="/safe_gpu_chat_handler"
|
| 45 |
)
|
| 46 |
# Returns: (history, input, reasoning, performance, context, session_info, skills)
|
| 47 |
+
|
| 48 |
+
# Using dynamic user (auto-created)
|
| 49 |
+
result = client.predict(
|
| 50 |
+
message="Your question",
|
| 51 |
+
history=[],
|
| 52 |
+
user_id="ExternalAPI_User123", # New user - auto-created in DB
|
| 53 |
+
session_text="Session: abc12345 | User: ExternalAPI_User123 | Interactions: 0",
|
| 54 |
+
api_name="/safe_gpu_chat_handler"
|
| 55 |
+
)
|
| 56 |
```
|
| 57 |
|
| 58 |
### 2. New Session
|
| 59 |
```python
|
| 60 |
+
# Base user
|
| 61 |
+
session = client.predict(
|
| 62 |
+
user_id="Admin_J",
|
| 63 |
+
api_name="/new_session"
|
| 64 |
+
)
|
| 65 |
+
# Returns: "Session: xyz67890 | User: Admin_J | Interactions: 0"
|
| 66 |
+
|
| 67 |
+
# Dynamic user (auto-created)
|
| 68 |
session = client.predict(
|
| 69 |
+
user_id="MyNewUser_2024",
|
| 70 |
api_name="/new_session"
|
| 71 |
)
|
| 72 |
+
# Returns: "Session: xyz67890 | User: MyNewUser_2024 | Interactions: 0"
|
| 73 |
```
|
| 74 |
|
| 75 |
### 3. Update Session Info
|
| 76 |
```python
|
| 77 |
updated = client.predict(
|
| 78 |
user_id="Admin_J",
|
| 79 |
+
session_text="Session: abc12345 | User: Admin_J | Interactions: 3",
|
| 80 |
api_name="/update_session_info"
|
| 81 |
)
|
| 82 |
# Returns: "Session: abc12345 | User: Admin_J | Interactions: 3"
|
|
|
|
| 96 |
```python
|
| 97 |
status = client.predict(
|
| 98 |
mode="relevant",
|
| 99 |
+
session_id_text="Session: abc12345 | User: Admin_J | Interactions: 3",
|
| 100 |
api_name="/handle_mode_change"
|
| 101 |
)
|
| 102 |
# Returns: "*Current: Relevant Context*"
|
|
|
|
| 164 |
| Parameter | Validation |
|
| 165 |
|-----------|------------|
|
| 166 |
| `message` | Non-empty, max 10,000 chars |
|
| 167 |
+
| `user_id` | Base: `Admin_J` (default). Dynamic: Any `[a-zA-Z0-9_]{1,50}` - auto-created |
|
| 168 |
| `mode` | One of: `'fresh'`, `'relevant'` |
|
| 169 |
| `response_speed` | One of: `'Fast'`, `'Balanced'`, `'Thorough'` |
|
| 170 |
| `session_text` | Format: `"Session: <8-hex> | User: <user_id> | Interactions: <num>"` |
|
|
|
|
| 175 |
|
| 176 |
### Full Conversation Flow
|
| 177 |
```python
|
| 178 |
+
# 1. Create session (using base Admin_J user)
|
| 179 |
+
session = client.predict(user_id="Admin_J", api_name="/new_session")
|
| 180 |
|
| 181 |
# 2. Set context mode
|
| 182 |
client.predict(mode="relevant", session_id_text=session, api_name="/handle_mode_change")
|
|
|
|
| 186 |
for msg in ["Q1", "Q2", "Q3"]:
|
| 187 |
result = client.predict(
|
| 188 |
message=msg, history=history,
|
| 189 |
+
user_id="Admin_J", session_text=session,
|
| 190 |
api_name="/safe_gpu_chat_handler"
|
| 191 |
)
|
| 192 |
history = result[0]
|
| 193 |
session = result[5] # Update session
|
| 194 |
print(result[0][-1]["content"]) # Print response
|
| 195 |
+
|
| 196 |
+
# Example with dynamic user (auto-created on first use)
|
| 197 |
+
dynamic_user = "ExternalAPI_Client01"
|
| 198 |
+
session = client.predict(user_id=dynamic_user, api_name="/new_session")
|
| 199 |
+
# User automatically created in database
|
| 200 |
```
|
| 201 |
|
| 202 |
### Error Handling
|
|
|
|
| 216 |
### `/safe_gpu_chat_handler`
|
| 217 |
- `message` (str, required)
|
| 218 |
- `history` (list, default: [])
|
| 219 |
+
- `user_id` (str, default: "Admin_J") - Base user or any valid format (auto-created)
|
| 220 |
- `session_text` (str, default: "Session: ... | User: ... | Interactions: 0")
|
| 221 |
|
| 222 |
### `/new_session`
|
| 223 |
+
- `user_id` (str, default: "Admin_J") - Base user or any valid format (auto-created)
|
| 224 |
|
| 225 |
### `/update_session_info`
|
| 226 |
+
- `user_id` (str, default: "Admin_J") - Base user or any valid format (auto-created)
|
| 227 |
- `session_text` (str, required)
|
| 228 |
|
| 229 |
### `/handle_mode_change`
|
app.py
CHANGED
|
@@ -231,7 +231,8 @@ def create_mobile_optimized_interface():
|
|
| 231 |
show_label=False,
|
| 232 |
container=False,
|
| 233 |
scale=1,
|
| 234 |
-
min_width=100
|
|
|
|
| 235 |
)
|
| 236 |
interface_components['user_dropdown'] = user_dropdown
|
| 237 |
|
|
|
|
| 231 |
show_label=False,
|
| 232 |
container=False,
|
| 233 |
scale=1,
|
| 234 |
+
min_width=100,
|
| 235 |
+
interactive=False # Non-interactive since only one option (ADMINONLY)
|
| 236 |
)
|
| 237 |
interface_components['user_dropdown'] = user_dropdown
|
| 238 |
|