shegga Claude commited on
Commit
74bc2f3
·
1 Parent(s): ed82e70

🔗 Fix dynamic API URLs for local and Hugging Face Spaces deployment

Browse files

Updated API endpoints page to automatically detect environment and show correct URLs:

**Local Development:**
- Base URL: http://localhost:7861
- Shows "Local Development" environment indicator

**Hugging Face Spaces:**
- Base URL: https://your-space-name.hf.space:7861
- Shows "Hugging Face Spaces" environment indicator
- Uses SPACE_ID and SPACE_NAME environment variables

**Features:**
- Automatic environment detection using os.getenv('SPACE_ID')
- Dynamic URL generation for all API examples
- Environment-aware documentation
- Works seamlessly in both development and production

All cURL, Python, and JavaScript examples now use the correct base URL
automatically based on where the application is running.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

Files changed (1) hide show
  1. py/pages/api_endpoints.py +45 -8
py/pages/api_endpoints.py CHANGED
@@ -3,19 +3,56 @@
3
  """
4
 
5
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  def create_api_endpoints_page():
8
  """Create the REST API endpoints tab"""
9
 
 
 
 
 
10
  # REST API Endpoints Tab
11
  with gr.Tab("🌐 REST API Endpoints"):
12
- gr.Markdown("""
13
- ## 🌐 REST API Endpoints
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- Your sentiment analysis model is now available via REST API!
 
 
 
16
 
17
- **Base URL:** `http://localhost:7861` (or your Hugging Face Space URL + `/api`)
18
 
 
 
19
  ### Available Endpoints:
20
 
21
  #### 📝 Single Text Analysis
@@ -50,13 +87,13 @@ def create_api_endpoints_page():
50
  **POST** `/memory/cleanup`
51
 
52
  ### 📚 Interactive API Documentation
53
- Visit **http://localhost:7861/docs** for interactive API documentation with Swagger UI.
54
 
55
  ### 🚀 Usage Examples
56
 
57
  **cURL Example:**
58
  ```bash
59
- curl -X POST "http://localhost:7861/analyze" \\
60
  -H "Content-Type: application/json" \\
61
  -d '{{"text": "Giảng viên dạy rất hay và tâm huyết."}}'
62
  ```
@@ -66,7 +103,7 @@ def create_api_endpoints_page():
66
  import requests
67
 
68
  response = requests.post(
69
- "http://localhost:7861/analyze",
70
  json={{"text": "Giảng viên dạy rất hay và tâm huyết."}}
71
  )
72
  result = response.json()
@@ -76,7 +113,7 @@ def create_api_endpoints_page():
76
 
77
  **JavaScript Example:**
78
  ```javascript
79
- const response = await fetch('http://localhost:7861/analyze', {{
80
  method: 'POST',
81
  headers: {{ 'Content-Type': 'application/json' }},
82
  body: JSON.stringify({{
 
3
  """
4
 
5
  import gradio as gr
6
+ import os
7
+
8
+ def get_api_base_url():
9
+ """Get the correct API base URL based on environment"""
10
+ # Check if we're on Hugging Face Spaces
11
+ space_id = os.getenv('SPACE_ID')
12
+
13
+ if space_id:
14
+ # We're on Hugging Face Spaces
15
+ space_name = os.getenv('SPACE_NAME', 'your-space-name')
16
+ return f"https://{space_name}.hf.space:7861"
17
+ else:
18
+ # We're running locally
19
+ return "http://localhost:7861"
20
 
21
  def create_api_endpoints_page():
22
  """Create the REST API endpoints tab"""
23
 
24
+ # Get the correct base URL
25
+ api_base_url = get_api_base_url()
26
+ is_hf_spaces = os.getenv('SPACE_ID') is not None
27
+
28
  # REST API Endpoints Tab
29
  with gr.Tab("🌐 REST API Endpoints"):
30
+ # Create dynamic content based on environment
31
+ if is_hf_spaces:
32
+ environment_info = f"""
33
+ ## 🌐 REST API Endpoints
34
+
35
+ Your sentiment analysis model is now available via REST API!
36
+
37
+ **📍 Environment:** Hugging Face Spaces
38
+ **🔗 Base URL:** `{api_base_url}`
39
+ **📚 Interactive Docs:** {api_base_url}/docs
40
+ """
41
+ else:
42
+ environment_info = f"""
43
+ ## 🌐 REST API Endpoints
44
+
45
+ Your sentiment analysis model is now available via REST API!
46
 
47
+ **📍 Environment:** Local Development
48
+ **🔗 Base URL:** `{api_base_url}`
49
+ **📚 Interactive Docs:** {api_base_url}/docs
50
+ """
51
 
52
+ gr.Markdown(environment_info)
53
 
54
+ # Static content
55
+ gr.Markdown(f"""
56
  ### Available Endpoints:
57
 
58
  #### 📝 Single Text Analysis
 
87
  **POST** `/memory/cleanup`
88
 
89
  ### 📚 Interactive API Documentation
90
+ Visit **{api_base_url}/docs** for interactive API documentation with Swagger UI.
91
 
92
  ### 🚀 Usage Examples
93
 
94
  **cURL Example:**
95
  ```bash
96
+ curl -X POST "{api_base_url}/analyze" \\
97
  -H "Content-Type: application/json" \\
98
  -d '{{"text": "Giảng viên dạy rất hay và tâm huyết."}}'
99
  ```
 
103
  import requests
104
 
105
  response = requests.post(
106
+ "{api_base_url}/analyze",
107
  json={{"text": "Giảng viên dạy rất hay và tâm huyết."}}
108
  )
109
  result = response.json()
 
113
 
114
  **JavaScript Example:**
115
  ```javascript
116
+ const response = await fetch('{api_base_url}/analyze', {{
117
  method: 'POST',
118
  headers: {{ 'Content-Type': 'application/json' }},
119
  body: JSON.stringify({{