Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def call_predict_api(image):
|
| 7 |
+
# Convert PIL images to base64 string
|
| 8 |
+
src_buffer = BytesIO()
|
| 9 |
+
image.save(src_buffer, format='PNG')
|
| 10 |
+
src_buffer.seek(0)
|
| 11 |
+
|
| 12 |
+
# Prepare files for upload
|
| 13 |
+
files = {
|
| 14 |
+
"image": ("src_image.png", src_buffer, "image/png"),
|
| 15 |
+
}
|
| 16 |
+
headers = {"X-API-Key": os.environ["api_key"]}
|
| 17 |
+
response = requests.post(os.environ["endpoint"], files=files, headers=headers)
|
| 18 |
+
if response.status_code != 200:
|
| 19 |
+
raise Exception(f"API Error: {response.text}")
|
| 20 |
+
result = response.json()["result"]
|
| 21 |
+
return result.replace("\n", "\n\n")
|
| 22 |
+
|
| 23 |
+
if __name__ == "__main__":
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=call_predict_api,
|
| 26 |
+
inputs=[
|
| 27 |
+
gr.Image(
|
| 28 |
+
sources=["upload"],
|
| 29 |
+
type="pil",
|
| 30 |
+
label="Garment Image",
|
| 31 |
+
width=512,
|
| 32 |
+
height=512,
|
| 33 |
+
),
|
| 34 |
+
],
|
| 35 |
+
outputs=[gr.Markdown(label="Result")],
|
| 36 |
+
title="BBC-OCR",
|
| 37 |
+
description="A simple OCR tool to extract text from images.",
|
| 38 |
+
)
|
| 39 |
+
iface.launch(auth=[(os.environ["username"], os.environ["password"])], show_api=False)
|