Spaces:
Build error
Build error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657from flask import Flask, render_template, request, jsonifyimport osimport base64import requestsfrom PIL import Imagefrom io import BytesIO
app = Flask(__name__)
# ConfigurationUPLOAD_FOLDER = 'static/captures'os.makedirs(UPLOAD_FOLDER, exist_ok=True)app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Hugging Face API settings (replace with your API key)HF_API_TOKEN = "your_hugging_face_api_token" # Get from https://huggingface.co/settings/tokensHF_API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50" # Example model for object detection
def query_hugging_face(image_data): headers = {"Authorization": f"Bearer {HF_API_TOKEN}"} response = requests.post(HF_API_URL, headers=headers, data=image_data) return response.json()
@app.route('/')def index(): return render_template('index.html')
@app.route('/capture', methods=['POST'])def capture(): try: # Get the base64 image data from the request data = request.form['image'] header, encoded = data.split(",", 1) binary_data = base64.b64decode(encoded) # Save the image filename = f"capture_{len(os.listdir(app.config['UPLOAD_FOLDER'])) + 1}.jpg" filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename) with open(filepath, "wb") as f: f.write(binary_data) # Process with Hugging Face API with open(filepath, "rb") as f: hf_result = query_hugging_face(f.read()) # Return the image URL and Hugging Face result image_url = f"/{filepath}" return jsonify({ 'status': 'success', 'image_url': image_url, 'hf_result': hf_result }) except Exception as e: return jsonify({'status': 'error', 'message': str(e)})
if __name__ == '__main__': app.run(debug=True, host='0.0.0.0', port=5000)app.pyAdded+57