Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,13 +6,13 @@ import base64
|
|
| 6 |
import json
|
| 7 |
import uvicorn
|
| 8 |
from typing import Dict
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
# Configure Google API
|
| 11 |
GOOGLE_API_KEY = 'AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI'
|
| 12 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 13 |
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
|
| 14 |
|
| 15 |
-
# Initialize FastAPI
|
| 16 |
app = FastAPI()
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
|
@@ -21,9 +21,16 @@ app.add_middleware(
|
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
-
def process_image(
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
prompt = """
|
| 29 |
Analyze the business card and extract the following information in JSON format:
|
|
@@ -44,7 +51,6 @@ def process_image(image_data: bytes) -> Dict:
|
|
| 44 |
prompt,
|
| 45 |
])
|
| 46 |
|
| 47 |
-
# Clean response and parse JSON
|
| 48 |
raw = response.text.strip()
|
| 49 |
if raw.startswith("```"):
|
| 50 |
lines = raw.splitlines()
|
|
@@ -56,14 +62,13 @@ def process_image(image_data: bytes) -> Dict:
|
|
| 56 |
|
| 57 |
return json.loads(raw)
|
| 58 |
|
| 59 |
-
# FastAPI endpoint
|
| 60 |
@app.post("/extract")
|
| 61 |
async def extract_info(file: UploadFile = File(...)):
|
| 62 |
contents = await file.read()
|
| 63 |
-
|
|
|
|
| 64 |
return result
|
| 65 |
|
| 66 |
-
# Gradio interface
|
| 67 |
def gradio_process(image):
|
| 68 |
if image is None:
|
| 69 |
return json.dumps({"error": "No image provided"}, indent=2)
|
|
@@ -72,13 +77,12 @@ def gradio_process(image):
|
|
| 72 |
|
| 73 |
interface = gr.Interface(
|
| 74 |
fn=gradio_process,
|
| 75 |
-
inputs=gr.Image(type="
|
| 76 |
outputs=gr.JSON(label="Extracted Information"),
|
| 77 |
title="Business Card Information Extractor",
|
| 78 |
description="Upload a business card image to extract contact information"
|
| 79 |
)
|
| 80 |
|
| 81 |
-
# Mount Gradio app to FastAPI
|
| 82 |
app = gr.mount_gradio_app(app, interface, path="/")
|
| 83 |
|
| 84 |
if __name__ == "__main__":
|
|
|
|
| 6 |
import json
|
| 7 |
import uvicorn
|
| 8 |
from typing import Dict
|
| 9 |
+
from PIL import Image
|
| 10 |
+
import io
|
| 11 |
|
|
|
|
| 12 |
GOOGLE_API_KEY = 'AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI'
|
| 13 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 14 |
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
|
| 15 |
|
|
|
|
| 16 |
app = FastAPI()
|
| 17 |
app.add_middleware(
|
| 18 |
CORSMiddleware,
|
|
|
|
| 21 |
allow_headers=["*"],
|
| 22 |
)
|
| 23 |
|
| 24 |
+
def process_image(image) -> Dict:
|
| 25 |
+
if isinstance(image, str):
|
| 26 |
+
image = Image.open(image)
|
| 27 |
+
|
| 28 |
+
# Convert PIL Image to bytes
|
| 29 |
+
img_byte_arr = io.BytesIO()
|
| 30 |
+
image.save(img_byte_arr, format='JPEG')
|
| 31 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 32 |
+
|
| 33 |
+
encoded_image = base64.b64encode(img_byte_arr).decode("utf-8")
|
| 34 |
|
| 35 |
prompt = """
|
| 36 |
Analyze the business card and extract the following information in JSON format:
|
|
|
|
| 51 |
prompt,
|
| 52 |
])
|
| 53 |
|
|
|
|
| 54 |
raw = response.text.strip()
|
| 55 |
if raw.startswith("```"):
|
| 56 |
lines = raw.splitlines()
|
|
|
|
| 62 |
|
| 63 |
return json.loads(raw)
|
| 64 |
|
|
|
|
| 65 |
@app.post("/extract")
|
| 66 |
async def extract_info(file: UploadFile = File(...)):
|
| 67 |
contents = await file.read()
|
| 68 |
+
img = Image.open(io.BytesIO(contents))
|
| 69 |
+
result = process_image(img)
|
| 70 |
return result
|
| 71 |
|
|
|
|
| 72 |
def gradio_process(image):
|
| 73 |
if image is None:
|
| 74 |
return json.dumps({"error": "No image provided"}, indent=2)
|
|
|
|
| 77 |
|
| 78 |
interface = gr.Interface(
|
| 79 |
fn=gradio_process,
|
| 80 |
+
inputs=gr.Image(type="pil", label="Upload Business Card"),
|
| 81 |
outputs=gr.JSON(label="Extracted Information"),
|
| 82 |
title="Business Card Information Extractor",
|
| 83 |
description="Upload a business card image to extract contact information"
|
| 84 |
)
|
| 85 |
|
|
|
|
| 86 |
app = gr.mount_gradio_app(app, interface, path="/")
|
| 87 |
|
| 88 |
if __name__ == "__main__":
|