IDS75912 commited on
Commit
678abfb
·
1 Parent(s): 777b1b9
Files changed (2) hide show
  1. README.md +3 -0
  2. main.py +3 -43
README.md CHANGED
@@ -8,3 +8,6 @@ pinned: false
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
8
  ---
9
 
10
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
11
+
12
+ # Space URL
13
+ https://ids75912-ctaianimalclassifierfastapi.hf.space/docs
main.py CHANGED
@@ -25,7 +25,7 @@ import traceback
25
  import logging
26
  from tensorflow import keras
27
 
28
- app = FastAPI(title="1.3 - AI Model Deployment")
29
  ''' browser: http://localhost:8000/docs'''
30
 
31
  from fastapi.middleware.cors import CORSMiddleware
@@ -67,40 +67,6 @@ hf_hub_download(repo_id, filename="model.weights.h5", repo_type="model", local_d
67
  # 2) load it
68
  model = tf.keras.models.load_model(local_model_dir)
69
  logging.info(f"Model loaded successfully from {local_model_dir}")
70
- # except Exception as e:
71
- # # Primary download attempt failed -> try streamed fallback which writes directly
72
- # # into local_model_dir (avoids internal temp/move when those operations are blocked).
73
- # primary_err = e
74
- # logging.error("Primary hf_hub_download failed, attempting streamed fallback: %s", e)
75
- # try:
76
- # for filename in ("config.json", "metadata.json", "model.weights.h5"):
77
- # url = hf_hub_url(repo_id=repo_id, filename=filename, repo_type='model')
78
- # logging.info(f"Streaming {filename} from {url} into {local_model_dir}")
79
- # resp = requests.get(url, stream=True, timeout=60)
80
- # resp.raise_for_status()
81
- # # write to a temp file inside the target dir then move atomically
82
- # with tempfile.NamedTemporaryFile(dir=local_model_dir, delete=False) as tmpf:
83
- # for chunk in resp.iter_content(chunk_size=8192):
84
- # if chunk:
85
- # tmpf.write(chunk)
86
- # tmp_path = tmpf.name
87
- # final_path = os.path.join(local_model_dir, filename)
88
- # try:
89
- # shutil.move(tmp_path, final_path)
90
- # except Exception:
91
- # # if atomic move fails, try copy+remove
92
- # shutil.copy(tmp_path, final_path)
93
- # os.remove(tmp_path)
94
-
95
- # # After streamed download, try loading
96
- # model = tf.keras.models.load_model(local_model_dir)
97
- # logging.info(f"Model loaded successfully from {local_model_dir} after streamed fallback")
98
- # except Exception as e2:
99
- # model_load_error = f"primary: {primary_err}; fallback: {e2}"
100
- # tb = traceback.format_exc()
101
- # logging.error("Streamed fallback failed: %s", e2)
102
- # logging.error(tb)
103
- # model = None
104
 
105
 
106
  @app.post('/upload/image')
@@ -123,13 +89,7 @@ async def uploadImage(img: UploadFile = File(...)):
123
  @app.get("/")
124
  def read_root() -> Dict[str, Any]:
125
  """Root endpoint."""
126
- return {"message": "Hello from FastAPI in "}
127
-
128
-
129
- # @app.post("/echo")
130
- # async def echo(payload: Dict[str, Any]) -> Dict[str, Any]:
131
- # """Echo back the received JSON payload."""
132
- # return {"echo": payload}
133
 
134
 
135
  @app.get("/version")
@@ -155,4 +115,4 @@ if __name__ == "__main__":
155
  # Run with: conda run -n gradio uvicorn main:app --reload
156
  import uvicorn
157
 
158
- uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
 
25
  import logging
26
  from tensorflow import keras
27
 
28
+ app = FastAPI(title="1.3 - AI Model Deployment - HF Hub + FastAPI",)
29
  ''' browser: http://localhost:8000/docs'''
30
 
31
  from fastapi.middleware.cors import CORSMiddleware
 
67
  # 2) load it
68
  model = tf.keras.models.load_model(local_model_dir)
69
  logging.info(f"Model loaded successfully from {local_model_dir}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
 
72
  @app.post('/upload/image')
 
89
  @app.get("/")
90
  def read_root() -> Dict[str, Any]:
91
  """Root endpoint."""
92
+ return {"message": "Hello from FastAPI in 1.3 - AI Model Deployment - HF Hub + FastAPI"}
 
 
 
 
 
 
93
 
94
 
95
  @app.get("/version")
 
115
  # Run with: conda run -n gradio uvicorn main:app --reload
116
  import uvicorn
117
 
118
+ uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True) # 7860 instead of 8000