File size: 2,530 Bytes
99e9532
7e82a93
19ce28f
99e9532
7e82a93
 
99e9532
19ce28f
 
99e9532
 
7e82a93
 
 
 
 
 
 
 
 
 
 
 
 
 
19ce28f
 
 
 
7e82a93
 
 
 
 
 
19ce28f
99e9532
19ce28f
 
7e82a93
 
 
 
 
 
 
 
 
 
 
 
 
99e9532
 
 
7e82a93
 
 
 
 
 
 
 
 
 
99e9532
7e82a93
 
99e9532
 
7e82a93
 
 
 
 
 
 
 
 
 
99e9532
 
7e82a93
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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 "<h2>Flask App for Image Upload to Instagram is Running!</h2>"


@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('[email protected]', '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)