Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,84 +1,88 @@
|
|
1 |
from flask import Flask, render_template, Response, request, jsonify
|
|
|
2 |
import cv2
|
3 |
-
from camera import VideoCamera
|
4 |
-
from transformers import CLIPProcessor, CLIPModel
|
5 |
-
from PIL import Image
|
6 |
import numpy as np
|
|
|
|
|
7 |
from instagrapi import Client
|
8 |
-
import os
|
9 |
|
10 |
app = Flask(__name__)
|
11 |
app.config['UPLOAD_FOLDER'] = 'uploads'
|
12 |
-
camera = None
|
13 |
-
captured_image = None
|
14 |
|
15 |
-
#
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
def gen(camera):
|
20 |
-
while True:
|
21 |
-
frame = camera.get_frame()
|
22 |
-
yield (b'--frame\r\n'
|
23 |
-
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
|
24 |
|
25 |
@app.route('/')
|
26 |
def index():
|
27 |
-
return
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
@app.route('/video_feed')
|
30 |
-
def video_feed():
|
31 |
-
global camera
|
32 |
-
if camera is None:
|
33 |
-
camera = VideoCamera()
|
34 |
-
return Response(gen(camera), mimetype='multipart/x-mixed-replace; boundary=frame')
|
35 |
|
36 |
@app.route('/capture', methods=['POST'])
|
37 |
def capture():
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
camera.switch_camera(camera_type)
|
52 |
-
return jsonify({'status': 'switched'})
|
53 |
-
|
54 |
-
@app.route('/retake', methods=['POST'])
|
55 |
-
def retake():
|
56 |
-
global captured_image
|
57 |
-
captured_image = None
|
58 |
-
return jsonify({'status': 'retake'})
|
59 |
|
60 |
@app.route('/upload', methods=['POST'])
|
61 |
def upload():
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
67 |
inputs = processor(images=image, return_tensors="pt")
|
68 |
-
|
69 |
-
|
70 |
caption = "Captured image from Flask app! #AI #HuggingFace"
|
71 |
|
72 |
-
#
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
81 |
|
82 |
if __name__ == '__main__':
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
1 |
from flask import Flask, render_template, Response, request, jsonify
|
2 |
+
import os
|
3 |
import cv2
|
|
|
|
|
|
|
4 |
import numpy as np
|
5 |
+
from PIL import Image
|
6 |
+
from transformers import CLIPProcessor, CLIPModel
|
7 |
from instagrapi import Client
|
|
|
8 |
|
9 |
app = Flask(__name__)
|
10 |
app.config['UPLOAD_FOLDER'] = 'uploads'
|
|
|
|
|
11 |
|
12 |
+
# Ensure the uploads folder exists
|
13 |
+
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
|
14 |
+
|
15 |
+
# Lazy loading the model
|
16 |
+
model = None
|
17 |
+
processor = None
|
18 |
+
captured_image_path = os.path.join(app.config['UPLOAD_FOLDER'], 'captured.jpg')
|
19 |
+
|
20 |
+
|
21 |
+
def load_clip_model():
|
22 |
+
global model, processor
|
23 |
+
if model is None or processor is None:
|
24 |
+
model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
|
25 |
+
processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
|
26 |
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
@app.route('/')
|
29 |
def index():
|
30 |
+
return "<h2>Flask App for Image Upload to Instagram is Running!</h2>"
|
31 |
+
|
32 |
+
|
33 |
+
@app.route('/health')
|
34 |
+
def health():
|
35 |
+
return jsonify({'status': 'healthy'}), 200
|
36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
@app.route('/capture', methods=['POST'])
|
39 |
def capture():
|
40 |
+
"""
|
41 |
+
This route expects an image to be sent from the frontend (as file)
|
42 |
+
"""
|
43 |
+
if 'image' not in request.files:
|
44 |
+
return jsonify({'status': 'error', 'message': 'No image uploaded'}), 400
|
45 |
+
|
46 |
+
file = request.files['image']
|
47 |
+
if file.filename == '':
|
48 |
+
return jsonify({'status': 'error', 'message': 'No file selected'}), 400
|
49 |
+
|
50 |
+
file.save(captured_image_path)
|
51 |
+
return jsonify({'status': 'captured', 'path': captured_image_path})
|
52 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
@app.route('/upload', methods=['POST'])
|
55 |
def upload():
|
56 |
+
"""
|
57 |
+
Processes the image using Hugging Face CLIP and uploads it to Instagram.
|
58 |
+
"""
|
59 |
+
if not os.path.exists(captured_image_path):
|
60 |
+
return jsonify({'status': 'error', 'message': 'No captured image'}), 400
|
61 |
+
|
62 |
+
try:
|
63 |
+
load_clip_model()
|
64 |
+
|
65 |
+
image = Image.open(captured_image_path)
|
66 |
inputs = processor(images=image, return_tensors="pt")
|
67 |
+
_ = model.get_image_features(**inputs)
|
68 |
+
|
69 |
caption = "Captured image from Flask app! #AI #HuggingFace"
|
70 |
|
71 |
+
# Instagram upload
|
72 |
+
cl = Client()
|
73 |
+
cl.login('[email protected]', 'Sitara@1946')
|
74 |
+
cl.photo_upload(captured_image_path, caption)
|
75 |
+
|
76 |
+
return jsonify({'status': 'uploaded'})
|
77 |
+
|
78 |
+
except Exception as e:
|
79 |
+
return jsonify({'status': 'error', 'message': str(e)})
|
80 |
+
|
81 |
|
82 |
if __name__ == '__main__':
|
83 |
+
import argparse
|
84 |
+
parser = argparse.ArgumentParser()
|
85 |
+
parser.add_argument('--port', type=int, default=7860)
|
86 |
+
args = parser.parse_args()
|
87 |
+
|
88 |
+
app.run(host='0.0.0.0', port=args.port)
|