Spaces:
Running
Running
matthewfarant
commited on
Commit
•
8b98450
1
Parent(s):
995e131
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
+
import imghdr
|
7 |
+
|
8 |
+
def call(input_image):
|
9 |
+
buffer = io.BytesIO()
|
10 |
+
input_image.save(buffer, format='PNG') # You can choose the format
|
11 |
+
base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
|
12 |
+
|
13 |
+
# Decode the base64 string to binary data
|
14 |
+
image_data = base64.b64decode(base64_string)
|
15 |
+
# Detect the image type
|
16 |
+
image_type = imghdr.what(None, image_data)
|
17 |
+
# Map the image type to a MIME type
|
18 |
+
mime_type = f"image/{image_type}" if image_type else "application/octet-stream"
|
19 |
+
# Create the data URL
|
20 |
+
data_url = f"data:{mime_type};base64,{base64_string}"
|
21 |
+
|
22 |
+
url = 'https://matthewfarant-business-card-scanner.hf.space/api/scan'
|
23 |
+
headers = {
|
24 |
+
'accept': 'application/json',
|
25 |
+
'X-API-Key': 'sk-3a85ec28-b262-448f-9736-a4717740c8b6',
|
26 |
+
'Content-Type': 'application/json'
|
27 |
+
}
|
28 |
+
data = {
|
29 |
+
'url': data_url
|
30 |
+
}
|
31 |
+
|
32 |
+
response = requests.post(url, headers=headers, json=data)
|
33 |
+
|
34 |
+
if response.status_code == 200:
|
35 |
+
return response.json()
|
36 |
+
else:
|
37 |
+
response.raise_for_status()
|
38 |
+
|
39 |
+
app = gr.Interface(fn=call, inputs=gr.Image(type = 'pil'), outputs="textbox")
|
40 |
+
|
41 |
+
if __name__ == "__main__":
|
42 |
+
app.launch()
|