JackRabbit commited on
Commit
253cf58
·
1 Parent(s): b644be2

api updates

Browse files
Files changed (1) hide show
  1. app.py +11 -15
app.py CHANGED
@@ -98,19 +98,14 @@ def run_api(predictor):
98
  'API mode' for this Streamlit app.
99
  Expects a query param ?api=1&image_url=<PUBLIC_IMAGE_URL>
100
 
101
- Example usage (from command line):
102
- curl -X GET "https://your-username-your-app.hf.space/?api=1&image_url=https://raw.githubusercontent.com/yourimage.jpg"
103
-
104
- The response is HTML with an embedded JSON, but you can often parse it directly in Python:
105
- >>> import requests
106
- >>> response = requests.get("https://your-username-your-app.hf.space/?api=1&image_url=...")
107
- >>> print(response.text) # prints the entire HTML with JSON
108
- # or sometimes:
109
- >>> data = response.json() # may work depending on how the client interprets the response
110
- >>> print(data)
111
  """
112
- params = st.experimental_get_query_params() # or st.query_params in Streamlit 1.19+
113
- image_url = params.get("image_url", [None])[0]
 
114
 
115
  if not image_url:
116
  st.json({"error": "No 'image_url' provided. Usage: ?api=1&image_url=<URL>"})
@@ -128,7 +123,7 @@ def run_api(predictor):
128
 
129
  image_bytes = response.content
130
  # Check file size (limit 10MB)
131
- image_size_mb = len(image_bytes)/(1024*1024)
132
  if image_size_mb > 10:
133
  st.json({"error": f"Image size {image_size_mb:.2f}MB exceeds 10MB limit."})
134
  st.stop()
@@ -165,13 +160,14 @@ def run_api(predictor):
165
  else:
166
  prediction_label = "Vespidae (wasp/hornet)"
167
 
168
- # Return results as JSON and stop further Streamlit processing
169
  st.json({
170
  "honeybee_score": honeybee_score,
171
  "bumblebee_score": bumblebee_score,
172
  "vespidae_score": vespidae_score,
173
  "prediction_label": prediction_label
174
  })
 
175
  st.stop()
176
 
177
  def run_ui(predictor):
@@ -231,7 +227,7 @@ def main():
231
  predictor = load_model()
232
 
233
  # Decide whether we are in 'API mode' or normal UI mode
234
- query_params = st.experimental_get_query_params()
235
  if "api" in query_params:
236
  run_api(predictor)
237
  else:
 
98
  'API mode' for this Streamlit app.
99
  Expects a query param ?api=1&image_url=<PUBLIC_IMAGE_URL>
100
 
101
+ Example usage:
102
+ curl "https://YOUR-SPACE.hf.space/?api=1&image_url=<some_image_url>"
103
+
104
+ WARNING: You will still get HTML with embedded JSON. That's a Streamlit limitation.
 
 
 
 
 
 
105
  """
106
+ # Use st.query_params (not st.experimental_get_query_params)
107
+ params = st.query_params
108
+ image_url = params.get("image_url", [None])[0] # `query_params` returns dict of lists
109
 
110
  if not image_url:
111
  st.json({"error": "No 'image_url' provided. Usage: ?api=1&image_url=<URL>"})
 
123
 
124
  image_bytes = response.content
125
  # Check file size (limit 10MB)
126
+ image_size_mb = len(image_bytes) / (1024 * 1024)
127
  if image_size_mb > 10:
128
  st.json({"error": f"Image size {image_size_mb:.2f}MB exceeds 10MB limit."})
129
  st.stop()
 
160
  else:
161
  prediction_label = "Vespidae (wasp/hornet)"
162
 
163
+ # Return results as JSON, but note that Streamlit wraps this in HTML
164
  st.json({
165
  "honeybee_score": honeybee_score,
166
  "bumblebee_score": bumblebee_score,
167
  "vespidae_score": vespidae_score,
168
  "prediction_label": prediction_label
169
  })
170
+ # Stop execution so the normal UI won't render
171
  st.stop()
172
 
173
  def run_ui(predictor):
 
227
  predictor = load_model()
228
 
229
  # Decide whether we are in 'API mode' or normal UI mode
230
+ query_params = st.query_params # Replaces st.experimental_get_query_params
231
  if "api" in query_params:
232
  run_api(predictor)
233
  else: