Spaces:
Running
Running
Create check.py
Browse files
check.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import base64
|
3 |
+
from groq import Groq
|
4 |
+
|
5 |
+
# Function to encode the image
|
6 |
+
def encode_image(image):
|
7 |
+
return base64.b64encode(image.read()).decode('utf-8')
|
8 |
+
|
9 |
+
# Function to extract ID card details from the image
|
10 |
+
def extract_id_details(image_url_or_base64):
|
11 |
+
client = Groq()
|
12 |
+
|
13 |
+
# Check if it's a base64 image or URL
|
14 |
+
if image_url_or_base64.startswith("data:image"):
|
15 |
+
# Image is in base64 format
|
16 |
+
url = image_url_or_base64
|
17 |
+
else:
|
18 |
+
# Image is a URL
|
19 |
+
url = f"data:image/jpeg;base64,{encode_image(image_url_or_base64)}"
|
20 |
+
|
21 |
+
completion = client.chat.completions.create(
|
22 |
+
model="llama-3.2-90b-vision-preview",
|
23 |
+
messages=[
|
24 |
+
{
|
25 |
+
"role": "user",
|
26 |
+
"content": [
|
27 |
+
{"type": "text", "text": "List all the details from the provided image of an ID card. Just list them in points. Don't give any other background descriptions."},
|
28 |
+
{
|
29 |
+
"type": "image_url",
|
30 |
+
"image_url": {"url": url}
|
31 |
+
},
|
32 |
+
],
|
33 |
+
}
|
34 |
+
],
|
35 |
+
temperature=0.2,
|
36 |
+
max_completion_tokens=200,
|
37 |
+
top_p=0,
|
38 |
+
stream=False,
|
39 |
+
stop=None,
|
40 |
+
)
|
41 |
+
return completion.choices[0].message.content
|
42 |
+
|
43 |
+
# Streamlit app interface
|
44 |
+
st.title("ID Card OCR Extractor")
|
45 |
+
|
46 |
+
# Input for image URL or upload
|
47 |
+
image_url = st.text_input("Enter the image URL:")
|
48 |
+
|
49 |
+
uploaded_image = st.file_uploader("Or upload an image of an ID card", type=["jpg", "jpeg", "png"])
|
50 |
+
|
51 |
+
if st.button("Extract Details"):
|
52 |
+
if image_url:
|
53 |
+
with st.spinner("Extracting details from the URL..."):
|
54 |
+
details = extract_id_details(image_url)
|
55 |
+
st.subheader("Extracted Details:")
|
56 |
+
st.write(details)
|
57 |
+
elif uploaded_image:
|
58 |
+
with st.spinner("Extracting details from the uploaded image..."):
|
59 |
+
# Encode the uploaded image as base64
|
60 |
+
base64_image = encode_image(uploaded_image)
|
61 |
+
details = extract_id_details(base64_image)
|
62 |
+
st.subheader("Extracted Details:")
|
63 |
+
st.write(details)
|
64 |
+
else:
|
65 |
+
st.warning("Please provide either a valid image URL or upload an image.")
|