lucky0146 commited on
Commit
9c51c49
·
verified ·
1 Parent(s): 3a3a056

Upload 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile.txt +10 -0
  2. app.py +75 -0
  3. requirements (1).txt +5 -0
Dockerfile.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+
3
+ WORKDIR /app
4
+
5
+ COPY requirements.txt .
6
+ RUN pip install --no-cache-dir -r requirements.txt
7
+
8
+ COPY app.py .
9
+
10
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, Response
2
+ import requests
3
+ import os
4
+
5
+ app = Flask(__name__)
6
+
7
+ @app.route('/', methods=['GET'])
8
+ def home():
9
+ return "Proxy API is running! Use /api/generate endpoint for image generation."
10
+
11
+ @app.route('/api/generate', methods=['POST'])
12
+ def proxy_generate():
13
+ try:
14
+ # Get the data from the request
15
+ data = request.json
16
+
17
+ # Forward the request to the actual API
18
+ response = requests.post(
19
+ "https://aiart-zroo.onrender.com/api/generate",
20
+ headers={"Content-Type": "application/json"},
21
+ json=data,
22
+ timeout=60 # Set a reasonable timeout
23
+ )
24
+
25
+ # If successful, modify the response to use our proxy for the image
26
+ if response.status_code == 200:
27
+ try:
28
+ result = response.json()
29
+ if 'image_url' in result:
30
+ # Get the original image filename
31
+ original_url = result['image_url']
32
+ filename = original_url.split('/')[-1]
33
+
34
+ # Replace the image URL with our proxy URL
35
+ proxy_url = f"/proxy-image/{filename}"
36
+ result['original_image_url'] = original_url
37
+ result['image_url'] = proxy_url
38
+
39
+ return jsonify(result)
40
+ except:
41
+ pass
42
+
43
+ # Return the response from the API
44
+ return response.content, response.status_code, {'Content-Type': response.headers.get('Content-Type', 'application/json')}
45
+ except Exception as e:
46
+ return jsonify({"error": str(e)}), 500
47
+
48
+ # Add an image proxy endpoint
49
+ @app.route('/proxy-image/<filename>', methods=['GET'])
50
+ def proxy_image(filename):
51
+ try:
52
+ # Construct the original URL
53
+ original_url = f"https://aiart-zroo.onrender.com/images/{filename}"
54
+
55
+ # Get the image with stream=True to handle binary data properly
56
+ response = requests.get(original_url, stream=True)
57
+
58
+ # Return the image data with explicit content type
59
+ return Response(
60
+ response.raw.read(), # Use raw.read() for binary data
61
+ status=response.status_code,
62
+ content_type='image/png', # Force content type to image/png
63
+ headers={
64
+ 'Cache-Control': 'no-cache',
65
+ 'Content-Disposition': f'inline; filename={filename}'
66
+ }
67
+ )
68
+ except Exception as e:
69
+ return jsonify({"error": str(e)}), 500
70
+
71
+ #new
72
+ if __name__ == '__main__':
73
+ # Get port from environment variable or use 7860 as default
74
+ port = int(os.environ.get('PORT', 7860))
75
+ app.run(host='0.0.0.0', port=port)
requirements (1).txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ flask==2.0.1
2
+ werkzeug==2.0.3
3
+ requests==2.28.1
4
+ gunicorn==20.1.0
5
+