Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,97 +1,113 @@
|
|
| 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 |
-
#
|
| 13 |
-
|
|
|
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
return base64.b64encode(image_file.read()).decode("utf-8")
|
| 21 |
|
| 22 |
-
def
|
| 23 |
-
|
| 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 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
file = request.files['image']
|
| 65 |
-
if file.filename == '':
|
| 66 |
-
return jsonify({'error': 'No selected file'}), 400
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
file.save(filepath)
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
|
|
|
|
|
|
| 79 |
|
| 80 |
-
# Clean
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
parsed_json = json.loads(clean_json)
|
| 85 |
|
| 86 |
-
# Clean
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
if __name__ == '__main__':
|
| 97 |
-
app.run(
|
|
|
|
| 1 |
import os
|
|
|
|
| 2 |
import base64
|
| 3 |
+
import json
|
| 4 |
+
import google.generativeai as genai
|
| 5 |
from flask import Flask, request, jsonify
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = Flask(__name__)
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# Configure Gemini
|
| 10 |
+
genai.configure(api_key='AIzaSyDcYyq3w21iwipYn17wCAQo3AYWhUIGDSI')
|
| 11 |
+
model = genai.GenerativeModel(model_name="gemini-1.5-pro")
|
| 12 |
|
| 13 |
+
# Configure upload settings
|
| 14 |
+
UPLOAD_FOLDER = 'uploads'
|
| 15 |
+
ALLOWED_EXTENSIONS = {'jpg', 'jpeg', 'png'}
|
| 16 |
|
| 17 |
+
if not os.path.exists(UPLOAD_FOLDER):
|
| 18 |
+
os.makedirs(UPLOAD_FOLDER)
|
|
|
|
| 19 |
|
| 20 |
+
def allowed_file(filename):
|
| 21 |
+
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
def clean_response(response_text):
|
| 24 |
+
"""Clean the response text to extract pure JSON."""
|
| 25 |
+
# Remove markdown code blocks
|
| 26 |
+
clean_text = response_text.replace('```json', '').replace('```', '').strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
try:
|
| 29 |
+
# Parse and return clean JSON
|
| 30 |
+
json_data = json.loads(clean_text)
|
|
|
|
| 31 |
|
| 32 |
+
# Clean up email and contact number fields (remove duplicates and format)
|
| 33 |
+
if 'businessCard' in json_data:
|
| 34 |
+
card = json_data['businessCard']
|
| 35 |
|
| 36 |
+
# Clean email field
|
| 37 |
+
if card.get('email'):
|
| 38 |
+
emails = [e.strip() for e in card['email'].replace('\\n', ',').split(',')]
|
| 39 |
+
card['email'] = ', '.join(list(dict.fromkeys(emails))) # Remove duplicates
|
| 40 |
|
| 41 |
+
# Clean contact number field
|
| 42 |
+
if card.get('contactNumber'):
|
| 43 |
+
numbers = [n.strip() for n in card['contactNumber'].split(',')]
|
| 44 |
+
card['contactNumber'] = ', '.join(list(dict.fromkeys(numbers))) # Remove duplicates
|
|
|
|
| 45 |
|
| 46 |
+
# Clean address field
|
| 47 |
+
if card.get('address'):
|
| 48 |
+
card['address'] = card['address'].replace('\\n', ' ').strip()
|
| 49 |
+
|
| 50 |
+
return json_data
|
| 51 |
+
except json.JSONDecodeError as e:
|
| 52 |
+
raise Exception(f"Failed to parse JSON response: {str(e)}")
|
| 53 |
+
|
| 54 |
+
def process_image(image_bytes):
|
| 55 |
+
base64_image = base64.b64encode(image_bytes).decode('utf-8')
|
| 56 |
+
prompt = """Extract info STRICTLY in this JSON format ONLY:
|
| 57 |
+
{
|
| 58 |
+
"businessCard": {
|
| 59 |
+
"companyName": "",
|
| 60 |
+
"email": "",
|
| 61 |
+
"contactNumber": "",
|
| 62 |
+
"website": "",
|
| 63 |
+
"address": ""
|
| 64 |
+
}
|
| 65 |
+
}
|
| 66 |
+
NO TEXT BEFORE/AFTER THE JSON. NO MARKDOWN. ONLY VALID JSON."""
|
| 67 |
+
|
| 68 |
+
try:
|
| 69 |
+
response = model.generate_content(
|
| 70 |
+
[
|
| 71 |
+
{
|
| 72 |
+
"mime_type": "image/jpeg",
|
| 73 |
+
"data": base64_image
|
| 74 |
+
},
|
| 75 |
+
prompt
|
| 76 |
+
]
|
| 77 |
+
)
|
| 78 |
+
# Clean and parse the response
|
| 79 |
+
return clean_response(response.text)
|
| 80 |
+
except Exception as e:
|
| 81 |
+
raise Exception(f"Error processing image: {str(e)}")
|
| 82 |
+
|
| 83 |
+
@app.route('/process_card', methods=['POST'])
|
| 84 |
+
def process_business_card():
|
| 85 |
+
# Check if file is present in request
|
| 86 |
+
if 'file' not in request.files:
|
| 87 |
+
return jsonify({'error': 'No file part'}), 400
|
| 88 |
+
|
| 89 |
+
file = request.files['file']
|
| 90 |
+
if file.filename == '':
|
| 91 |
+
return jsonify({'error': 'No selected file'}), 400
|
| 92 |
+
|
| 93 |
+
if not allowed_file(file.filename):
|
| 94 |
+
return jsonify({'error': 'File type not allowed. Use jpg, jpeg, or png'}), 400
|
| 95 |
+
|
| 96 |
+
try:
|
| 97 |
+
# Read file bytes directly from the uploaded file
|
| 98 |
+
image_bytes = file.read()
|
| 99 |
+
|
| 100 |
+
# Process the image and get clean JSON response
|
| 101 |
+
result = process_image(image_bytes)
|
| 102 |
+
|
| 103 |
+
return jsonify(result), 200
|
| 104 |
+
|
| 105 |
+
except Exception as e:
|
| 106 |
+
return jsonify({'error': str(e)}), 500
|
| 107 |
+
|
| 108 |
+
@app.route('/health', methods=['GET'])
|
| 109 |
+
def health_check():
|
| 110 |
+
return jsonify({'status': 'healthy'}), 200
|
| 111 |
|
| 112 |
if __name__ == '__main__':
|
| 113 |
+
app.run(debug=True, port=7860)
|