Spaces:
Paused
Paused
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,21 +1,12 @@
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import base64
|
| 4 |
-
import httpx
|
| 5 |
import google.generativeai as genai
|
| 6 |
|
| 7 |
-
from fastapi import FastAPI, HTTPException
|
| 8 |
-
from pydantic import BaseModel, HttpUrl
|
| 9 |
|
| 10 |
app = FastAPI(title="Business Card Analyzer")
|
| 11 |
-
|
| 12 |
-
# Input model for the API
|
| 13 |
-
class ImageRequest(BaseModel):
|
| 14 |
-
image_url: HttpUrl
|
| 15 |
-
|
| 16 |
-
# Configure the Google Generative AI client using an environment variable.
|
| 17 |
-
# Make sure to set the GOOGLE_API_KEY variable when running the app.
|
| 18 |
-
GOOGLE_API_KEY = "AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI"
|
| 19 |
if not GOOGLE_API_KEY:
|
| 20 |
raise Exception("Please set the GOOGLE_API_KEY environment variable.")
|
| 21 |
|
|
@@ -37,19 +28,26 @@ Analyze the business card and extract the following information in JSON format:
|
|
| 37 |
Ensure that the response is strictly in JSON format and includes all the fields mentioned above. If any information is missing or unclear, set the corresponding field to null.
|
| 38 |
"""
|
| 39 |
|
| 40 |
-
@app.post("/analyze", summary="Analyze a business card image")
|
| 41 |
-
async def analyze_business_card(
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
try:
|
| 44 |
-
|
| 45 |
-
response = await client.get(str(request.image_url))
|
| 46 |
-
response.raise_for_status()
|
| 47 |
-
img_bytes = response.content
|
| 48 |
except Exception as e:
|
| 49 |
-
raise HTTPException(
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
# Encode the image in base64
|
| 52 |
-
encoded_image = base64.b64encode(
|
| 53 |
|
| 54 |
# Create an instance of the Gemini model
|
| 55 |
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
|
|
@@ -58,7 +56,7 @@ async def analyze_business_card(request: ImageRequest):
|
|
| 58 |
try:
|
| 59 |
result = model.generate_content([
|
| 60 |
{
|
| 61 |
-
"mime_type":
|
| 62 |
"data": encoded_image,
|
| 63 |
},
|
| 64 |
PROMPT,
|
|
@@ -66,7 +64,7 @@ async def analyze_business_card(request: ImageRequest):
|
|
| 66 |
# Get the raw text response
|
| 67 |
raw = result.text.strip()
|
| 68 |
|
| 69 |
-
# Remove markdown formatting if present (
|
| 70 |
if raw.startswith("```"):
|
| 71 |
lines = raw.splitlines()
|
| 72 |
if lines[0].startswith("```"):
|
|
@@ -77,9 +75,15 @@ async def analyze_business_card(request: ImageRequest):
|
|
| 77 |
|
| 78 |
data = json.loads(raw)
|
| 79 |
except json.JSONDecodeError as jde:
|
| 80 |
-
raise HTTPException(
|
|
|
|
|
|
|
|
|
|
| 81 |
except Exception as e:
|
| 82 |
-
raise HTTPException(
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
return data
|
| 85 |
|
|
|
|
| 1 |
import os
|
| 2 |
import json
|
| 3 |
import base64
|
|
|
|
| 4 |
import google.generativeai as genai
|
| 5 |
|
| 6 |
+
from fastapi import FastAPI, HTTPException, File, UploadFile
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI(title="Business Card Analyzer")
|
| 9 |
+
GOOGLE_API_KEY = 'AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if not GOOGLE_API_KEY:
|
| 11 |
raise Exception("Please set the GOOGLE_API_KEY environment variable.")
|
| 12 |
|
|
|
|
| 28 |
Ensure that the response is strictly in JSON format and includes all the fields mentioned above. If any information is missing or unclear, set the corresponding field to null.
|
| 29 |
"""
|
| 30 |
|
| 31 |
+
@app.post("/analyze", summary="Analyze a business card image file")
|
| 32 |
+
async def analyze_business_card(file: UploadFile = File(...)):
|
| 33 |
+
# Validate the file type. Only JPEG and PNG are supported.
|
| 34 |
+
if file.content_type not in ["image/jpeg", "image/png"]:
|
| 35 |
+
raise HTTPException(
|
| 36 |
+
status_code=400,
|
| 37 |
+
detail="Invalid file type. Only JPEG and PNG images are supported."
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
# Read the uploaded file
|
| 41 |
try:
|
| 42 |
+
file_content = await file.read()
|
|
|
|
|
|
|
|
|
|
| 43 |
except Exception as e:
|
| 44 |
+
raise HTTPException(
|
| 45 |
+
status_code=400,
|
| 46 |
+
detail=f"Failed to read the uploaded file: {e}"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
# Encode the image in base64
|
| 50 |
+
encoded_image = base64.b64encode(file_content).decode("utf-8")
|
| 51 |
|
| 52 |
# Create an instance of the Gemini model
|
| 53 |
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
|
|
|
|
| 56 |
try:
|
| 57 |
result = model.generate_content([
|
| 58 |
{
|
| 59 |
+
"mime_type": file.content_type,
|
| 60 |
"data": encoded_image,
|
| 61 |
},
|
| 62 |
PROMPT,
|
|
|
|
| 64 |
# Get the raw text response
|
| 65 |
raw = result.text.strip()
|
| 66 |
|
| 67 |
+
# Remove markdown formatting if present (e.g., ```json ... ```)
|
| 68 |
if raw.startswith("```"):
|
| 69 |
lines = raw.splitlines()
|
| 70 |
if lines[0].startswith("```"):
|
|
|
|
| 75 |
|
| 76 |
data = json.loads(raw)
|
| 77 |
except json.JSONDecodeError as jde:
|
| 78 |
+
raise HTTPException(
|
| 79 |
+
status_code=500,
|
| 80 |
+
detail=f"JSON decoding error: {jde}. Response text was: {raw}"
|
| 81 |
+
)
|
| 82 |
except Exception as e:
|
| 83 |
+
raise HTTPException(
|
| 84 |
+
status_code=500,
|
| 85 |
+
detail=f"An error occurred while processing the image: {e}"
|
| 86 |
+
)
|
| 87 |
|
| 88 |
return data
|
| 89 |
|