Spaces:
Runtime error
Runtime error
from flask import Flask, render_template,request,jsonify,Response | |
import os | |
import json | |
from flask_cors import CORS | |
import time | |
from flask_limiter import Limiter | |
from flask_limiter.util import get_remote_address | |
import requests | |
app = Flask(__name__) | |
CORS(app) | |
def get_client_ip(): | |
return request.headers.get(os.getenv('head'), get_remote_address()) | |
limiter = Limiter( | |
key_func=get_client_ip, | |
app=app, | |
default_limits=["8 per minute"] | |
) | |
users = 0 | |
userslist = [] | |
def index(): | |
global users | |
global userslist | |
if(request.remote_addr not in userslist): | |
userslist.append(request.remote_addr) | |
users = len(userslist) | |
return render_template('models.html') | |
def users(): | |
return {"total users : " : users} | |
def Hf(): | |
prompt = request.json.get('prompt', '') | |
negative = request.json.get('negative', '') | |
steps = request.json.get('steps', 20) | |
width = request.json.get('width',1024) | |
height = request.json.get('height',1024) | |
scale = request.json.get('scale',7) | |
model = request.json.get('model','sd3') | |
style = request.json.get('style', 'Cinematic') | |
hash = request.json.get('hash', 'Cinematic') | |
def Gen(prompt,negative,steps,width,height,scale,style,model): | |
req = requests.post('https://xyplon-flask.hf.space/hf/img/gen',headers={ | |
'Authorization' : os.getenv('auth') | |
},json={ | |
'prompt': prompt, | |
'negative': negative, | |
'steps': steps, | |
'width': width, | |
'height': height, | |
'scale': scale, | |
'model' : model, | |
'style': style, | |
'hash': hash | |
}, stream=True) | |
if(req.status_code!=200): | |
return "an error occurred! ", 500 | |
for chunk in req.iter_lines(): | |
yield f'{chunk.decode()}\n' | |
return Response(Gen(prompt=prompt,negative=negative,steps=steps,width=width,height=height,scale=scale,style=style,model=model), mimetype="text/event-stream") | |
if __name__ == "__main__": | |
app.run(debug=True, host='0.0.0.0', port=7860) |