Spaces:
Build error
Build error
jineetshah commited on
using tempfile
Browse files
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(
|
| 14 |
-
|
|
|
|
| 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 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
|
| 26 |
-
# Send the
|
| 27 |
-
pred = query(
|
| 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
|
| 34 |
-
outputs
|
| 35 |
-
allow_flagging
|
| 36 |
-
description
|
| 37 |
-
iface.launch(share
|
|
|
|
| 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)
|