jineetshah commited on
Commit
fbb887a
·
unverified ·
1 Parent(s): 7f9d7ea

using tempfile

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -6,32 +6,34 @@ import pandas as pd
6
  from PIL import Image
7
  import io
8
  import base64
 
9
 
10
  API_URL = "https://api-inference.huggingface.co/models/AliGhiasvand86/gisha_digit_recognition"
11
  headers = {"Authorization": "Bearer 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"}
12
 
13
- def query(data):
14
- response = requests.post(API_URL, headers=headers, data=data)
 
15
  return response.json()
16
 
17
  def predict(img):
18
  # Convert the numpy array to a PIL Image object
19
  img_pil = Image.fromarray((img * 255).astype(np.uint8))
20
 
21
- # Convert the image data to a byte array
22
- img_byte_arr = io.BytesIO()
23
- img_pil.save(img_byte_arr, format='PNG')
24
- img_byte_arr = img_byte_arr.getvalue()
25
 
26
- # Send the byte array as data in the query function
27
- pred = query(img_byte_arr)
28
 
29
  # Convert the JSON output to a pandas DataFrame and return it
30
  df = pd.DataFrame(pred)
31
  return df
32
 
33
- iface = gr.Interface(predict, inputs = 'sketchpad',
34
- outputs = gr.outputs.Dataframe(),
35
- allow_flagging = 'never',
36
- description = 'Draw a Digit Below... (Draw in the centre for best results)')
37
- iface.launch(share = False, width = 300, height = 500)
 
6
  from PIL import Image
7
  import io
8
  import base64
9
+ import tempfile
10
 
11
  API_URL = "https://api-inference.huggingface.co/models/AliGhiasvand86/gisha_digit_recognition"
12
  headers = {"Authorization": "Bearer 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"}
13
 
14
+ def query(image_path):
15
+ with open(image_path, "rb") as file:
16
+ response = requests.post(API_URL, headers=headers, files={"file": file})
17
  return response.json()
18
 
19
  def predict(img):
20
  # Convert the numpy array to a PIL Image object
21
  img_pil = Image.fromarray((img * 255).astype(np.uint8))
22
 
23
+ # Save the image to a temporary file
24
+ with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as temp_file:
25
+ image_path = temp_file.name
26
+ img_pil.save(image_path)
27
 
28
+ # Send the image file path to the query function
29
+ pred = query(image_path)
30
 
31
  # Convert the JSON output to a pandas DataFrame and return it
32
  df = pd.DataFrame(pred)
33
  return df
34
 
35
+ iface = gr.Interface(predict, inputs='sketchpad',
36
+ outputs=gr.outputs.Dataframe(),
37
+ allow_flagging='never',
38
+ description='Draw a Digit Below... (Draw in the centre for best results)')
39
+ iface.launch(share=False, width=300, height=500)