scan-your-card / app.py
matthewfarant's picture
Create app.py
8b98450 verified
raw
history blame
1.26 kB
import gradio as gr
from PIL import Image
import requests
import io
import base64
import imghdr
def call(input_image):
buffer = io.BytesIO()
input_image.save(buffer, format='PNG') # You can choose the format
base64_string = base64.b64encode(buffer.getvalue()).decode('utf-8')
# Decode the base64 string to binary data
image_data = base64.b64decode(base64_string)
# Detect the image type
image_type = imghdr.what(None, image_data)
# Map the image type to a MIME type
mime_type = f"image/{image_type}" if image_type else "application/octet-stream"
# Create the data URL
data_url = f"data:{mime_type};base64,{base64_string}"
url = 'https://matthewfarant-business-card-scanner.hf.space/api/scan'
headers = {
'accept': 'application/json',
'X-API-Key': 'sk-3a85ec28-b262-448f-9736-a4717740c8b6',
'Content-Type': 'application/json'
}
data = {
'url': data_url
}
response = requests.post(url, headers=headers, json=data)
if response.status_code == 200:
return response.json()
else:
response.raise_for_status()
app = gr.Interface(fn=call, inputs=gr.Image(type = 'pil'), outputs="textbox")
if __name__ == "__main__":
app.launch()