Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -22,6 +22,10 @@ app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
|
|
22 |
HF_API_TOKEN = os.getenv('HF_API_TOKEN') # Load token from environment variable
|
23 |
HF_API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50" # Example model for object detection
|
24 |
|
|
|
|
|
|
|
|
|
25 |
def query_hugging_face(image_data):
|
26 |
if not HF_API_TOKEN:
|
27 |
logging.error("Hugging Face API token not set.")
|
@@ -41,6 +45,38 @@ def query_hugging_face(image_data):
|
|
41 |
logging.error(f"Failed to connect to Hugging Face API: {str(e)}")
|
42 |
return {"error": f"Failed to connect to Hugging Face API: {str(e)}"}
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
@app.route('/')
|
45 |
def index():
|
46 |
return render_template('index.html')
|
@@ -74,6 +110,21 @@ def capture():
|
|
74 |
logging.error(f"Error in capture: {str(e)}")
|
75 |
return jsonify({'status': 'error', 'message': str(e)})
|
76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
@app.route('/test_connectivity')
|
78 |
def test_connectivity():
|
79 |
try:
|
|
|
22 |
HF_API_TOKEN = os.getenv('HF_API_TOKEN') # Load token from environment variable
|
23 |
HF_API_URL = "https://api-inference.huggingface.co/models/facebook/detr-resnet-50" # Example model for object detection
|
24 |
|
25 |
+
# Instagram API settings
|
26 |
+
INSTAGRAM_ACCESS_TOKEN = os.getenv('INSTAGRAM_ACCESS_TOKEN') # Load token from environment variable
|
27 |
+
INSTAGRAM_API_URL = "https://graph.instagram.com/v21.0/me/media"
|
28 |
+
|
29 |
def query_hugging_face(image_data):
|
30 |
if not HF_API_TOKEN:
|
31 |
logging.error("Hugging Face API token not set.")
|
|
|
45 |
logging.error(f"Failed to connect to Hugging Face API: {str(e)}")
|
46 |
return {"error": f"Failed to connect to Hugging Face API: {str(e)}"}
|
47 |
|
48 |
+
def upload_to_instagram(image_path):
|
49 |
+
if not INSTAGRAM_ACCESS_TOKEN:
|
50 |
+
logging.error("Instagram access token not set.")
|
51 |
+
return {"error": "Instagram access token not set. Please set the INSTAGRAM_ACCESS_TOKEN environment variable."}
|
52 |
+
try:
|
53 |
+
# Step 1: Create media object
|
54 |
+
session = requests.Session()
|
55 |
+
retries = Retry(total=3, backoff_factor=1, status_forcelist=[502, 503, 504])
|
56 |
+
session.mount('https://', HTTPAdapter(max_retries=retries))
|
57 |
+
|
58 |
+
media_url = f"{INSTAGRAM_API_URL}?image_url={request.url_root}{image_path}&caption=Uploaded%20via%20Flask%20Camera%20App&access_token={INSTAGRAM_ACCESS_TOKEN}"
|
59 |
+
logging.debug(f"Sending media creation request to Instagram: {media_url}")
|
60 |
+
response = session.post(media_url, timeout=10)
|
61 |
+
response.raise_for_status()
|
62 |
+
media_data = response.json()
|
63 |
+
|
64 |
+
if 'id' not in media_data:
|
65 |
+
logging.error(f"Failed to create media: {media_data}")
|
66 |
+
return {"error": f"Failed to create media: {media_data}"}
|
67 |
+
|
68 |
+
# Step 2: Publish media
|
69 |
+
media_id = media_data['id']
|
70 |
+
publish_url = f"https://graph.instagram.com/v21.0/me/media_publish?creation_id={media_id}&access_token={INSTAGRAM_ACCESS_TOKEN}"
|
71 |
+
logging.debug(f"Sending publish request to Instagram: {publish_url}")
|
72 |
+
publish_response = session.post(publish_url, timeout=10)
|
73 |
+
publish_response.raise_for_status()
|
74 |
+
logging.debug("Instagram upload successful.")
|
75 |
+
return {"status": "success"}
|
76 |
+
except requests.exceptions.RequestException as e:
|
77 |
+
logging.error(f"Failed to upload to Instagram: {str(e)}")
|
78 |
+
return {"error": f"Failed to upload to Instagram: {str(e)}"}
|
79 |
+
|
80 |
@app.route('/')
|
81 |
def index():
|
82 |
return render_template('index.html')
|
|
|
110 |
logging.error(f"Error in capture: {str(e)}")
|
111 |
return jsonify({'status': 'error', 'message': str(e)})
|
112 |
|
113 |
+
@app.route('/upload_instagram', methods=['POST'])
|
114 |
+
def upload_instagram():
|
115 |
+
try:
|
116 |
+
image_url = request.form['image_url']
|
117 |
+
if not image_url.startswith('/static/captures/'):
|
118 |
+
return jsonify({'status': 'error', 'message': 'Invalid image path.'})
|
119 |
+
|
120 |
+
result = upload_to_instagram(image_url.lstrip('/'))
|
121 |
+
if 'error' in result:
|
122 |
+
return jsonify({'status': 'error', 'message': result['error']})
|
123 |
+
return jsonify({'status': 'success'})
|
124 |
+
except Exception as e:
|
125 |
+
logging.error(f"Error in upload_instagram: {str(e)}")
|
126 |
+
return jsonify({'status': 'error', 'message': str(e)})
|
127 |
+
|
128 |
@app.route('/test_connectivity')
|
129 |
def test_connectivity():
|
130 |
try:
|