Yaswanth56 commited on
Commit
44b508a
·
verified ·
1 Parent(s): 58c00ec

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template, request, jsonify
2
+ import requests
3
+ import base64
4
+ import os
5
+
6
+ app = Flask(__name__)
7
+
8
+ # Instagram Graph API settings
9
+ ACCESS_TOKEN = "EACN93e8btl8BOzqCVifPdgeg6U8ikAgJZACLFU0mrBZBuwT9iF21nFOLLF8RnYfbHgaUuLkoddZC16pe9hkntYmHfnMLADH730oHfmQ0DjKTX4cAC8e7p6UpqdfHADjeFZCkRbXfMTRZCzv7wMSqSpeEBGxOd0PREr6qsenXc0IZATCw01QNQZAff2hhlBV4oP1"
10
+ INSTAGRAM_ACCOUNT_ID = "YOUR_INSTAGRAM_ACCOUNT_ID" # Replace with your Instagram Business Account ID
11
+
12
+ @app.route('/')
13
+ def index():
14
+ return render_template('index.html')
15
+
16
+ @app.route('/upload', methods=['POST'])
17
+ def upload_image():
18
+ try:
19
+ # Get the base64 image data from the frontend
20
+ image_data = request.form['image']
21
+ image_data = image_data.split(',')[1] # Remove the data:image/jpeg;base64, prefix
22
+ image_binary = base64.b64decode(image_data)
23
+
24
+ # Save the image temporarily
25
+ temp_image_path = 'temp_image.jpg'
26
+ with open(temp_image_path, 'wb') as f:
27
+ f.write(image_binary)
28
+
29
+ # Step 1: Upload image to Instagram as a media object
30
+ media_url = f"https://graph.facebook.com/v20.0/{INSTAGRAM_ACCOUNT_ID}/media"
31
+ media_payload = {
32
+ 'image_url': 'https://your-image-hosting-url/temp_image.jpg', # You need to host the image publicly
33
+ 'caption': 'Uploaded via Camera App #HuggingFace #Instagram',
34
+ 'access_token': ACCESS_TOKEN
35
+ }
36
+
37
+ # For Hugging Face, you may need to upload the image to a temporary public URL
38
+ # For simplicity, assuming the image is accessible; in production, use a service like S3 or a temporary file server
39
+ response = requests.post(media_url, data=media_payload)
40
+ response_data = response.json()
41
+
42
+ if 'id' not in response_data:
43
+ return jsonify({'error': 'Failed to create media object', 'details': response_data}), 500
44
+
45
+ media_id = response_data['id']
46
+
47
+ # Step 2: Publish the media object
48
+ publish_url = f"https://graph.facebook.com/v20.0/{INSTAGRAM_ACCOUNT_ID}/media_publish"
49
+ publish_payload = {
50
+ 'creation_id': media_id,
51
+ 'access_token': ACCESS_TOKEN
52
+ }
53
+ publish_response = requests.post(publish_url, data=publish_payload)
54
+ publish_data = publish_response.json()
55
+
56
+ # Clean up temporary file
57
+ if os.path.exists(temp_image_path):
58
+ os.remove(temp_image_path)
59
+
60
+ if 'id' in publish_data:
61
+ return jsonify({'message': 'Image uploaded to Instagram successfully!'})
62
+ else:
63
+ return jsonify({'error': 'Failed to publish media', 'details': publish_data}), 500
64
+
65
+ except Exception as e:
66
+ return jsonify({'error': str(e)}), 500
67
+
68
+ if __name__ == '__main__':
69
+ app.run(debug=True)