Spaces:
Sleeping
Sleeping
matthewfarant
commited on
Upload 3 files
Browse files- Dockerfile +14 -0
- app.py +116 -0
- requirements.txt +3 -0
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use the official Python 3.10.9 image
|
2 |
+
FROM python:3.10.9
|
3 |
+
|
4 |
+
# Copy the current directory contents into the container at .
|
5 |
+
COPY . .
|
6 |
+
|
7 |
+
# Set the working directory to /
|
8 |
+
WORKDIR /
|
9 |
+
|
10 |
+
# Install requirements.txt
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r /requirements.txt
|
12 |
+
|
13 |
+
# Start the FastAPI app on port 7860, the default port expected by Spaces
|
14 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel, Field, HttpUrl, ConfigDict
|
2 |
+
from typing import List
|
3 |
+
from openai import AsyncOpenAI
|
4 |
+
from fastapi import FastAPI, HTTPException
|
5 |
+
|
6 |
+
class BusinessCardResponse(BaseModel):
|
7 |
+
is_valid_business_card: bool = Field(title = "Is This a Valid Business Card", description="To flag whether the user inputted image is a business card")
|
8 |
+
name: str
|
9 |
+
job: str
|
10 |
+
company: str
|
11 |
+
phone_number: List[int]
|
12 |
+
email: List[str]
|
13 |
+
address: List[str]
|
14 |
+
website: List[str]
|
15 |
+
|
16 |
+
client = AsyncOpenAI(api_key = 'sk-proj-BszN5uaYsFxdGaqnrw6cT3BlbkFJmSFEl0vKfMZJYOAePJ26')
|
17 |
+
|
18 |
+
app = FastAPI(title="Business Card Scanner API", description = "A FastAPI app to do physical business card scanning & information extraction", version = "0.1.0")
|
19 |
+
|
20 |
+
class ImageRequest(BaseModel):
|
21 |
+
url: str
|
22 |
+
|
23 |
+
model_config = ConfigDict(
|
24 |
+
json_schema_extra={
|
25 |
+
"examples": [
|
26 |
+
{
|
27 |
+
"url" : "https://marketplace.canva.com/EAFIF52_A0Y/1/0/1600w/canva-putih-biru-profesional-minimalis-kartu-nama-bisnis-AmVxO2cXfvA.jpg"
|
28 |
+
}
|
29 |
+
]
|
30 |
+
}
|
31 |
+
)
|
32 |
+
|
33 |
+
prompt = """
|
34 |
+
### CONTEXT ###
|
35 |
+
I want to create a physical-to-digital business card scanner application. This app allows the user to take a picture of a physical business card, extract the information inside the picture, and create a digital business card.
|
36 |
+
|
37 |
+
### OBJECTIVE ###
|
38 |
+
The user will give you an image of a business card. Your first task is to check whether it is a business card. If it is a business card, you must extract these information from the card:
|
39 |
+
1. The cardholder's name (including the salutation & degrees, if any)
|
40 |
+
2. Job/occupation of the cardholder
|
41 |
+
3. The company name
|
42 |
+
2. Phone Number(s) (list of integer, remove plus/+ sign and any other symbols)
|
43 |
+
3. Email address (list)
|
44 |
+
4. Home/office address (list)
|
45 |
+
5. Personal/company Website (list)
|
46 |
+
|
47 |
+
### OUTPUT ###
|
48 |
+
You must only return a JSON output in a snippet with this schema:
|
49 |
+
```json
|
50 |
+
{
|
51 |
+
"is_valid_business_card": bool,
|
52 |
+
"name": str,
|
53 |
+
"job": str,
|
54 |
+
"company": str,
|
55 |
+
"phone_number": List[int],
|
56 |
+
"email": List[str],
|
57 |
+
"address": List[str],
|
58 |
+
"website": List[str]
|
59 |
+
}
|
60 |
+
```
|
61 |
+
You must only return the JSON output only, without any additional comments!
|
62 |
+
|
63 |
+
### SAMPLE RESPONSE ###
|
64 |
+
```json
|
65 |
+
{
|
66 |
+
"is_valid_business_card": True,
|
67 |
+
"name": "Matthew Farant, BSc.",
|
68 |
+
"job": "Business Intelligence Analyst",
|
69 |
+
"company": "TipTip",
|
70 |
+
"phone_number": [62812345678, 62312345678],
|
71 |
+
"email":["[email protected]", "[email protected]"],
|
72 |
+
"address":["Jalan Kepang Lima No. 8"],
|
73 |
+
"website":["www.matthew.com"]
|
74 |
+
}
|
75 |
+
"""
|
76 |
+
|
77 |
+
@app.post("/api/scan", response_model=BusinessCardResponse)
|
78 |
+
async def extract_card(image_request: ImageRequest) -> BusinessCardResponse:
|
79 |
+
response = await client.chat.completions.create(
|
80 |
+
model="gpt-4o-mini",
|
81 |
+
messages=[
|
82 |
+
{
|
83 |
+
"role": "system",
|
84 |
+
"content": [
|
85 |
+
{
|
86 |
+
"text": prompt,
|
87 |
+
"type": "text"
|
88 |
+
}
|
89 |
+
]
|
90 |
+
},
|
91 |
+
{
|
92 |
+
"role": "user",
|
93 |
+
"content": [
|
94 |
+
{
|
95 |
+
"type": "text",
|
96 |
+
"text": """Extract the business card information from the image and put them in a JSON format"""
|
97 |
+
},
|
98 |
+
{
|
99 |
+
"type": "image_url",
|
100 |
+
"image_url": {"url": image_request.url}
|
101 |
+
}
|
102 |
+
]
|
103 |
+
},
|
104 |
+
{
|
105 |
+
"role": "assistant",
|
106 |
+
"content": "```json"
|
107 |
+
}
|
108 |
+
],
|
109 |
+
temperature=1,
|
110 |
+
max_tokens=300,
|
111 |
+
top_p=1,
|
112 |
+
frequency_penalty=0,
|
113 |
+
presence_penalty=0
|
114 |
+
)
|
115 |
+
|
116 |
+
return BusinessCardResponse.model_validate_json(response.choices[0].message.content.replace('```json','').replace('```','').strip())
|
requirements.txt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
fastapi==0.109.2
|
2 |
+
pydantic==2.7.0
|
3 |
+
openai==1.12.0
|