Image3 / app.py
nagasurendra's picture
Update app.py
461dffc verified
from flask import Flask, render_template, Response, request, jsonify
import os
import cv2
import numpy as np
from PIL import Image
from transformers import CLIPProcessor, CLIPModel
from instagrapi import Client
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
# Ensure the uploads folder exists
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# Lazy loading the model
model = None
processor = None
captured_image_path = os.path.join(app.config['UPLOAD_FOLDER'], 'captured.jpg')
def load_clip_model():
global model, processor
if model is None or processor is None:
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
@app.route('/')
def index():
return render_template('index.html')
@app.route('/health')
def health():
return jsonify({'status': 'healthy'}), 200
@app.route('/capture', methods=['POST'])
def capture():
"""
This route expects an image to be sent from the frontend (as file)
"""
if 'image' not in request.files:
return jsonify({'status': 'error', 'message': 'No image uploaded'}), 400
file = request.files['image']
if file.filename == '':
return jsonify({'status': 'error', 'message': 'No file selected'}), 400
file.save(captured_image_path)
return jsonify({'status': 'captured', 'path': captured_image_path})
@app.route('/upload', methods=['POST'])
def upload():
"""
Processes the image using Hugging Face CLIP and uploads it to Instagram.
"""
if not os.path.exists(captured_image_path):
return jsonify({'status': 'error', 'message': 'No captured image'}), 400
try:
load_clip_model()
image = Image.open(captured_image_path)
inputs = processor(images=image, return_tensors="pt")
_ = model.get_image_features(**inputs)
caption = "Captured image from Flask app! #AI #HuggingFace"
# Instagram upload
cl = Client()
cl.login('SitaraJewellery@sathkrutha.com', 'Sitara@1946')
cl.photo_upload(captured_image_path, caption)
return jsonify({'status': 'uploaded'})
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)})
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--port', type=int, default=7860)
args = parser.parse_args()
app.run(host='0.0.0.0', port=args.port)