NaveenKumar5 commited on
Commit
f97120c
·
verified ·
1 Parent(s): f003b1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -9,25 +9,31 @@ client = InferenceHTTPClient(
9
  api_key="dxkgGGHSZ3DI8XzVn29U"
10
  )
11
 
12
- # Prediction function
13
  def predict(image: Image.Image):
14
- # Convert PIL image to bytes
15
- image.save("temp.jpg") # Save locally for upload
16
- with open("temp.jpg", "rb") as f:
17
- result = client.run_workflow(
18
- workspace_name="naveen-kumar-hnmil",
19
- workflow_id="detect-count-and-visualize-5",
20
- images={"image": f},
21
- use_cache=True
22
- )
23
-
24
- # Get the annotated image URL from the result
25
- annotated_url = result.get("visualizations", {}).get("image")
26
- if annotated_url:
 
 
 
27
  response = requests.get(annotated_url, stream=True)
 
 
 
28
  return Image.open(response.raw)
29
- else:
30
- return "No annotated image returned."
 
31
 
32
  # Gradio UI
33
  demo = gr.Interface(
 
9
  api_key="dxkgGGHSZ3DI8XzVn29U"
10
  )
11
 
 
12
  def predict(image: Image.Image):
13
+ try:
14
+ # Save image temporarily
15
+ image.save("temp.jpg")
16
+ with open("temp.jpg", "rb") as f:
17
+ result = client.run_workflow(
18
+ workspace_name="naveen-kumar-hnmil",
19
+ workflow_id="detect-count-and-visualize-5",
20
+ images={"image": f},
21
+ use_cache=True
22
+ )
23
+
24
+ # Extract annotated image
25
+ annotated_url = result.get("visualizations", {}).get("image")
26
+ if not annotated_url:
27
+ return "Error: No visualization returned from Roboflow."
28
+
29
  response = requests.get(annotated_url, stream=True)
30
+ if response.status_code != 200:
31
+ return f"Error fetching image from: {annotated_url}"
32
+
33
  return Image.open(response.raw)
34
+
35
+ except Exception as e:
36
+ return f"Exception: {str(e)}"
37
 
38
  # Gradio UI
39
  demo = gr.Interface(