Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import base64
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
from groq import Groq
|
| 6 |
+
from werkzeug.utils import secure_filename
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
|
| 10 |
+
app.config['UPLOAD_FOLDER'] = 'uploads'
|
| 11 |
+
|
| 12 |
+
# Initialize Groq client with your API key
|
| 13 |
+
client = Groq(api_key="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")
|
| 14 |
+
|
| 15 |
+
# Ensure upload folder exists
|
| 16 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
| 17 |
+
|
| 18 |
+
def encode_image_to_base64(image_path):
|
| 19 |
+
with open(image_path, "rb") as image_file:
|
| 20 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 21 |
+
|
| 22 |
+
def extract_business_card_info(image_base64):
|
| 23 |
+
completion = client.chat.completions.create(
|
| 24 |
+
model="llama-3.2-11b-vision-preview",
|
| 25 |
+
messages=[
|
| 26 |
+
{
|
| 27 |
+
"role": "user",
|
| 28 |
+
"content": [
|
| 29 |
+
{
|
| 30 |
+
"type": "text",
|
| 31 |
+
"text": """Extract info STRICTLY in this JSON format ONLY:
|
| 32 |
+
{
|
| 33 |
+
"businessCard": {
|
| 34 |
+
"companyName": "",
|
| 35 |
+
"email": "",
|
| 36 |
+
"contactNumber": "",
|
| 37 |
+
"website": "",
|
| 38 |
+
"address": ""
|
| 39 |
+
}
|
| 40 |
+
}
|
| 41 |
+
NO TEXT BEFORE/AFTER THE JSON. NO MARKDOWN. ONLY VALID JSON."""
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
"type": "image_url",
|
| 45 |
+
"image_url": {
|
| 46 |
+
"url": f"data:image/jpeg;base64,{image_base64}"
|
| 47 |
+
}
|
| 48 |
+
}
|
| 49 |
+
]
|
| 50 |
+
}
|
| 51 |
+
],
|
| 52 |
+
temperature=0.3,
|
| 53 |
+
max_tokens=1024,
|
| 54 |
+
top_p=1,
|
| 55 |
+
stream=False,
|
| 56 |
+
)
|
| 57 |
+
return completion.choices[0].message.content
|
| 58 |
+
|
| 59 |
+
@app.route('/extract', methods=['POST'])
|
| 60 |
+
def extract_info():
|
| 61 |
+
if 'image' not in request.files:
|
| 62 |
+
return jsonify({'error': 'No image file provided'}), 400
|
| 63 |
+
|
| 64 |
+
file = request.files['image']
|
| 65 |
+
if file.filename == '':
|
| 66 |
+
return jsonify({'error': 'No selected file'}), 400
|
| 67 |
+
|
| 68 |
+
if file:
|
| 69 |
+
filename = secure_filename(file.filename)
|
| 70 |
+
filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
|
| 71 |
+
file.save(filepath)
|
| 72 |
+
|
| 73 |
+
try:
|
| 74 |
+
# Convert image to base64
|
| 75 |
+
image_base64 = encode_image_to_base64(filepath)
|
| 76 |
+
|
| 77 |
+
# Extract information
|
| 78 |
+
json_data = extract_business_card_info(image_base64)
|
| 79 |
+
|
| 80 |
+
# Clean and parse JSON
|
| 81 |
+
json_start = json_data.find('{')
|
| 82 |
+
json_end = json_data.rfind('}') + 1
|
| 83 |
+
clean_json = json_data[json_start:json_end]
|
| 84 |
+
parsed_json = json.loads(clean_json)
|
| 85 |
+
|
| 86 |
+
# Clean up the uploaded file
|
| 87 |
+
os.remove(filepath)
|
| 88 |
+
|
| 89 |
+
return jsonify(parsed_json)
|
| 90 |
+
|
| 91 |
+
except Exception as e:
|
| 92 |
+
if os.path.exists(filepath):
|
| 93 |
+
os.remove(filepath)
|
| 94 |
+
return jsonify({'error': str(e)}), 500
|
| 95 |
+
|
| 96 |
+
if __name__ == '__main__':
|
| 97 |
+
app.run(host='0.0.0.0', port=7860)
|