xyplon commited on
Commit
5fc7fe6
·
verified ·
1 Parent(s): cb4f124

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -16
app.py CHANGED
@@ -1,22 +1,72 @@
1
- FROM python:3.9-slim
 
 
 
 
 
 
 
 
 
2
 
3
- # Set working directory
4
- WORKDIR /app
5
 
6
- # Copy the current directory contents into the container at /app
7
- COPY . /app
 
 
 
8
 
9
- # Install Gunicorn and your application dependencies
10
- RUN pip install --no-cache-dir gunicorn && \
11
- pip install --no-cache-dir -r requirements.txt
12
 
13
- # Expose the port Gunicorn will listen on
14
- EXPOSE 7860
15
 
16
- # Define environment variables
17
- ENV FLASK_APP=app.py
18
- ENV FLASK_RUN_HOST=0.0.0.0
19
- ENV FLASK_RUN_PORT=7860
 
 
 
20
 
21
- # Run the Gunicorn server
22
- CMD ["gunicorn", "--bind", "0.0.0.0:7860", "app:app"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, render_template,request,jsonify,Response
2
+ import os
3
+ import json
4
+ from flask_cors import CORS
5
+ import time
6
+ from flask_limiter import Limiter
7
+ from flask_limiter.util import get_remote_address
8
+ import requests
9
+ app = Flask(__name__)
10
+ CORS(app)
11
 
12
+ def get_client_ip():
13
+ return request.headers.get(os.getenv('head'), get_remote_address())
14
 
15
+ limiter = Limiter(
16
+ key_func=get_client_ip,
17
+ app=app,
18
+ default_limits=["8 per minute"]
19
+ )
20
 
 
 
 
21
 
22
+ users = 0
23
+ userslist = []
24
 
25
+ @app.route("/")
26
+ def index():
27
+ global users
28
+ if(request.remote_addr not in userslist):
29
+ userslist.append(request.remote_addr)
30
+ users = len(userslist)
31
+ return render_template('models.html')
32
 
33
+
34
+ @app.route("/users")
35
+ def users():
36
+ return {"total users : " : users}
37
+
38
+ @app.route('/gen', methods=['POST'])
39
+ @limiter.limit("8 per minute")
40
+ def Hf():
41
+
42
+ prompt = request.json.get('prompt', '')
43
+ negative = request.json.get('negative', '')
44
+ steps = request.json.get('steps', 20)
45
+ width = request.json.get('width',1024)
46
+ height = request.json.get('height',1024)
47
+ scale = request.json.get('scale',7)
48
+ model = request.json.get('model','sd3')
49
+ style = request.json.get('style', 'Cinematic')
50
+ def Gen(prompt,negative,steps,width,height,scale,style,model):
51
+ req = requests.post('https://xyplon-server2.hf.space/hf/img/gen',headers={
52
+ 'Authorization' : os.getenv('auth')
53
+ },json={
54
+ 'prompt': prompt,
55
+ 'negative': negative,
56
+ 'steps': steps,
57
+ 'width': width,
58
+ 'height': height,
59
+ 'scale': scale,
60
+ 'model' : model,
61
+ 'style': style
62
+ }, stream=True)
63
+ if(req.status_code!=200):
64
+ return "an error occurred! ", 500
65
+ for chunk in req.iter_lines():
66
+ yield f'{chunk.decode()}\n'
67
+
68
+ return Response(Gen(prompt=prompt,negative=negative,steps=steps,width=width,height=height,scale=scale,style=style,model=model), mimetype="text/event-stream")
69
+
70
+
71
+ if __name__ == "__main__":
72
+ app.run(debug=True, host='0.0.0.0', port=7860)