nagasurendra commited on
Commit
7e82a93
·
verified ·
1 Parent(s): f2a2860

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -60
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
- # Initialize Hugging Face CLIP model
16
- model = CLIPModel.from_pretrained("openai/clip-vit-base-patch16")
17
- processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch16")
 
 
 
 
 
 
 
 
 
 
 
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 render_template('index.html')
 
 
 
 
 
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
- global captured_image, camera
39
- if camera:
40
- captured_image = camera.get_frame()
41
- img_array = np.frombuffer(captured_image, np.uint8)
42
- img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
43
- cv2.imwrite(os.path.join(app.config['UPLOAD_FOLDER'], 'captured.jpg'), img)
44
- return jsonify({'status': 'captured'})
45
-
46
- @app.route('/switch_camera', methods=['POST'])
47
- def switch_camera():
48
- global camera
49
- camera_type = request.json.get('camera_type', 'user') # 'user' for front, 'environment' for back
50
- if camera:
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
- global captured_image
63
- if captured_image:
64
- # Process image with Hugging Face CLIP
65
- img_path = os.path.join(app.config['UPLOAD_FOLDER'], 'captured.jpg')
66
- image = Image.open(img_path)
 
 
 
 
 
67
  inputs = processor(images=image, return_tensors="pt")
68
- outputs = model.get_image_features(**inputs)
69
- # Generate a caption (simplified example)
70
  caption = "Captured image from Flask app! #AI #HuggingFace"
71
 
72
- # Upload to Instagram
73
- try:
74
- cl = Client()
75
- cl.login('[email protected]', 'Sitara@1946')
76
- cl.photo_upload(img_path, caption=caption)
77
- return jsonify({'status': 'uploaded'})
78
- except Exception as e:
79
- return jsonify({'status': 'error', 'message': str(e)})
80
- return jsonify({'status': 'error', 'message': 'No image captured'})
 
81
 
82
  if __name__ == '__main__':
83
- os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
84
- app.run(debug=True, host='0.0.0.0', port=5000)
 
 
 
 
 
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)