skibi11 commited on
Commit
1471654
·
verified ·
1 Parent(s): c676187

updated with UI

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -1,4 +1,4 @@
1
- # The Final app.py using a manual FastAPI endpoint
2
 
3
  from fastapi import FastAPI
4
  from fastapi.responses import JSONResponse
@@ -12,7 +12,7 @@ import os
12
  import base64
13
  import io
14
 
15
- # --- 1. Load the Model (Stays the same) ---
16
  model = None
17
  try:
18
  model_path = hf_hub_download(
@@ -23,9 +23,9 @@ try:
23
  print("--- MODEL LOADED SUCCESSFULLY! ---")
24
  except Exception as e:
25
  print(f"--- ERROR LOADING MODEL: {e} ---")
26
- model = None # Ensure model is None if loading fails
27
 
28
- # --- 2. Prediction Logic (Stays the same) ---
29
  def preprocess_image(img_pil):
30
  img = img_pil.resize((224, 224))
31
  img_array = np.array(img)
@@ -38,7 +38,7 @@ def preprocess_image(img_pil):
38
  def run_prediction(pil_image):
39
  if model is None:
40
  return {"error": "Model is not loaded on the server."}
41
-
42
  processed_image = preprocess_image(pil_image)
43
  prediction = model.predict(processed_image)
44
  labels = [f"Class_{i}" for i in range(prediction.shape[1])]
@@ -48,30 +48,30 @@ def run_prediction(pil_image):
48
  # --- 3. Create the FastAPI app ---
49
  app = FastAPI()
50
 
51
- # --- 4. Define the input data structure for our new endpoint ---
52
  class PredictionRequest(BaseModel):
53
  data: list[str]
54
 
55
- # --- 5. Create our own reliable API endpoint ---
56
  @app.post("/api/predict/")
57
- async def handle_prediction(request: PredictionRequest):
58
  try:
59
- # Get the Base64 string from the JSON payload
60
  base64_string = request.data[0].split(',', 1)[1]
61
  image_bytes = base64.b64decode(base64_string)
62
  pil_image = Image.open(io.BytesIO(image_bytes))
63
-
64
- # Run the prediction
65
  result_dict = run_prediction(pil_image)
66
-
67
- # Return the result in the same format Gradio does
68
  return JSONResponse(content={"data": [result_dict]})
69
-
70
  except Exception as e:
71
  return JSONResponse(status_code=500, content={"error": str(e)})
72
 
73
- import uvicorn
 
 
 
 
 
 
 
74
 
75
- if __name__ == "__main__":
76
- # The port number (7860) is the standard port for Hugging Face Spaces
77
- uvicorn.run(app, host="0.0.0.0", port=7860)
 
1
+ # The Complete and Final app.py with both a UI and an API
2
 
3
  from fastapi import FastAPI
4
  from fastapi.responses import JSONResponse
 
12
  import base64
13
  import io
14
 
15
+ # --- 1. Load the Model ---
16
  model = None
17
  try:
18
  model_path = hf_hub_download(
 
23
  print("--- MODEL LOADED SUCCESSFULLY! ---")
24
  except Exception as e:
25
  print(f"--- ERROR LOADING MODEL: {e} ---")
26
+ model = None
27
 
28
+ # --- 2. Core Prediction Logic ---
29
  def preprocess_image(img_pil):
30
  img = img_pil.resize((224, 224))
31
  img_array = np.array(img)
 
38
  def run_prediction(pil_image):
39
  if model is None:
40
  return {"error": "Model is not loaded on the server."}
41
+
42
  processed_image = preprocess_image(pil_image)
43
  prediction = model.predict(processed_image)
44
  labels = [f"Class_{i}" for i in range(prediction.shape[1])]
 
48
  # --- 3. Create the FastAPI app ---
49
  app = FastAPI()
50
 
51
+ # --- 4. Define the input data structure for our API endpoint ---
52
  class PredictionRequest(BaseModel):
53
  data: list[str]
54
 
55
+ # --- 5. Create our reliable API endpoint for the Render backend ---
56
  @app.post("/api/predict/")
57
+ async def handle_api_prediction(request: PredictionRequest):
58
  try:
 
59
  base64_string = request.data[0].split(',', 1)[1]
60
  image_bytes = base64.b64decode(base64_string)
61
  pil_image = Image.open(io.BytesIO(image_bytes))
 
 
62
  result_dict = run_prediction(pil_image)
 
 
63
  return JSONResponse(content={"data": [result_dict]})
 
64
  except Exception as e:
65
  return JSONResponse(status_code=500, content={"error": str(e)})
66
 
67
+ # --- 6. Create the Gradio UI for the homepage ---
68
+ gradio_ui = gr.Interface(
69
+ fn=run_prediction,
70
+ inputs=gr.Image(type="pil", label="Upload an eye image to test"),
71
+ outputs=gr.JSON(label="Prediction Results"),
72
+ title="LeukoLook Eye Detector",
73
+ description="A demonstration of the LeukoLook detection model. This UI can be used for direct testing."
74
+ )
75
 
76
+ # --- 7. Mount the Gradio UI onto the FastAPI app's root ---
77
+ app = gr.mount_gradio_app(app, gradio_ui, path="/")