Spaces:
Running
Running
TomatoCocotree
commited on
Commit
•
ef707e4
1
Parent(s):
7c4fbf0
更新server.py
Browse files
server.py
CHANGED
@@ -1,36 +1,31 @@
|
|
1 |
-
from functools import wraps
|
2 |
-
from flask import (
|
3 |
-
Flask,
|
4 |
-
jsonify,
|
5 |
-
request,
|
6 |
-
Response,
|
7 |
-
render_template_string,
|
8 |
-
abort,
|
9 |
-
send_from_directory,
|
10 |
-
send_file,
|
11 |
-
)
|
12 |
-
from flask_cors import CORS
|
13 |
-
from flask_compress import Compress
|
14 |
-
import markdown
|
15 |
import argparse
|
16 |
-
|
17 |
-
from transformers import AutoModelForCausalLM, AutoModelForSeq2SeqLM
|
18 |
-
from transformers import BlipForConditionalGeneration
|
19 |
-
import unicodedata
|
20 |
-
import torch
|
21 |
-
import time
|
22 |
-
import os
|
23 |
import gc
|
24 |
-
import
|
|
|
25 |
import secrets
|
26 |
-
|
27 |
-
import
|
|
|
|
|
28 |
from io import BytesIO
|
29 |
from random import randint
|
|
|
|
|
|
|
30 |
import webuiapi
|
31 |
-
import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
from constants import *
|
33 |
-
from colorama import Fore, Style, init as colorama_init
|
34 |
|
35 |
colorama_init()
|
36 |
|
@@ -129,9 +124,9 @@ parser.add_argument(
|
|
129 |
)
|
130 |
|
131 |
args = parser.parse_args()
|
132 |
-
|
133 |
-
port =
|
134 |
-
host = "0.0.0.0"
|
135 |
summarization_model = (
|
136 |
args.summarization_model
|
137 |
if args.summarization_model
|
@@ -222,8 +217,8 @@ if "summarize" in modules:
|
|
222 |
).to(device)
|
223 |
|
224 |
if "sd" in modules and not sd_use_remote:
|
225 |
-
from diffusers import
|
226 |
-
|
227 |
|
228 |
print("Initializing Stable Diffusion pipeline...")
|
229 |
sd_device_string = cuda_device if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
|
@@ -536,23 +531,10 @@ def image_to_base64(image: Image, quality: int = 75) -> str:
|
|
536 |
img_str = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
537 |
return img_str
|
538 |
|
539 |
-
ignore_auth = []
|
540 |
-
# Reads an API key from an already existing file. If that file doesn't exist, create it.
|
541 |
-
if args.secure:
|
542 |
-
try:
|
543 |
-
with open("api_key.txt", "r") as txt:
|
544 |
-
api_key = txt.read().replace('\n', '')
|
545 |
-
except:
|
546 |
-
api_key = secrets.token_hex(5)
|
547 |
-
with open("api_key.txt", "w") as txt:
|
548 |
-
txt.write(api_key)
|
549 |
-
|
550 |
-
print(f"{Fore.YELLOW}{Style.BRIGHT}Your API key is {api_key}{Style.RESET_ALL}")
|
551 |
-
elif args.share and args.secure != True:
|
552 |
-
print(f"{Fore.RED}{Style.BRIGHT}WARNING: This instance is publicly exposed without an API key! It is highly recommended to restart with the \"--secure\" argument!{Style.RESET_ALL}")
|
553 |
-
else:
|
554 |
-
print(f"{Fore.YELLOW}{Style.BRIGHT}No API key given because you are running locally.{Style.RESET_ALL}")
|
555 |
|
|
|
|
|
|
|
556 |
|
557 |
def is_authorize_ignored(request):
|
558 |
view_func = app.view_functions.get(request.endpoint)
|
@@ -571,14 +553,16 @@ def before_request():
|
|
571 |
# Checks if an API key is present and valid, otherwise return unauthorized
|
572 |
# The options check is required so CORS doesn't get angry
|
573 |
try:
|
574 |
-
if request.method != 'OPTIONS' and
|
575 |
-
print(f"
|
|
|
|
|
576 |
response = jsonify({ 'error': '401: Invalid API key' })
|
577 |
response.status_code = 401
|
578 |
-
return
|
579 |
except Exception as e:
|
580 |
print(f"API key check error: {e}")
|
581 |
-
return "
|
582 |
|
583 |
|
584 |
@app.after_request
|
@@ -1101,9 +1085,10 @@ def chromadb_import():
|
|
1101 |
|
1102 |
|
1103 |
if args.share:
|
1104 |
-
from flask_cloudflared import _run_cloudflared
|
1105 |
import inspect
|
1106 |
|
|
|
|
|
1107 |
sig = inspect.signature(_run_cloudflared)
|
1108 |
sum = sum(
|
1109 |
1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import argparse
|
2 |
+
import base64
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import gc
|
4 |
+
import hashlib
|
5 |
+
import os
|
6 |
import secrets
|
7 |
+
import sys
|
8 |
+
import time
|
9 |
+
import unicodedata
|
10 |
+
from functools import wraps
|
11 |
from io import BytesIO
|
12 |
from random import randint
|
13 |
+
|
14 |
+
import markdown
|
15 |
+
import torch
|
16 |
import webuiapi
|
17 |
+
from colorama import Fore, Style
|
18 |
+
from colorama import init as colorama_init
|
19 |
+
from flask import (Flask, Response, abort, jsonify, render_template_string,
|
20 |
+
request, send_file, send_from_directory)
|
21 |
+
from flask_compress import Compress
|
22 |
+
from flask_cors import CORS
|
23 |
+
from PIL import Image
|
24 |
+
from transformers import (AutoModelForCausalLM, AutoModelForSeq2SeqLM,
|
25 |
+
AutoProcessor, AutoTokenizer,
|
26 |
+
BlipForConditionalGeneration, pipeline)
|
27 |
+
|
28 |
from constants import *
|
|
|
29 |
|
30 |
colorama_init()
|
31 |
|
|
|
124 |
)
|
125 |
|
126 |
args = parser.parse_args()
|
127 |
+
# [HF, Huggingface] Set port to 7860, set host to remote.
|
128 |
+
port = 7860
|
129 |
+
host = "0.0.0.0"
|
130 |
summarization_model = (
|
131 |
args.summarization_model
|
132 |
if args.summarization_model
|
|
|
217 |
).to(device)
|
218 |
|
219 |
if "sd" in modules and not sd_use_remote:
|
220 |
+
from diffusers import (EulerAncestralDiscreteScheduler,
|
221 |
+
StableDiffusionPipeline)
|
222 |
|
223 |
print("Initializing Stable Diffusion pipeline...")
|
224 |
sd_device_string = cuda_device if torch.cuda.is_available() else 'mps' if torch.backends.mps.is_available() else 'cpu'
|
|
|
531 |
img_str = base64.b64encode(buffer.getvalue()).decode("utf-8")
|
532 |
return img_str
|
533 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
534 |
|
535 |
+
ignore_auth = []
|
536 |
+
# [HF, Huggingface] Get password instead of text file.
|
537 |
+
api_key = os.environ.get("password")
|
538 |
|
539 |
def is_authorize_ignored(request):
|
540 |
view_func = app.view_functions.get(request.endpoint)
|
|
|
553 |
# Checks if an API key is present and valid, otherwise return unauthorized
|
554 |
# The options check is required so CORS doesn't get angry
|
555 |
try:
|
556 |
+
if request.method != 'OPTIONS' and is_authorize_ignored(request) == False and getattr(request.authorization, 'token', '') != api_key:
|
557 |
+
print(f"WARNING: Unauthorized API key access from {request.remote_addr}")
|
558 |
+
if request.method == 'POST':
|
559 |
+
print(f"Incoming POST request with {request.headers.get('Authorization')}")
|
560 |
response = jsonify({ 'error': '401: Invalid API key' })
|
561 |
response.status_code = 401
|
562 |
+
return "https://(hf_name)-(space_name).hf.space/"
|
563 |
except Exception as e:
|
564 |
print(f"API key check error: {e}")
|
565 |
+
return "https://(hf_name)-(space_name).hf.space/"
|
566 |
|
567 |
|
568 |
@app.after_request
|
|
|
1085 |
|
1086 |
|
1087 |
if args.share:
|
|
|
1088 |
import inspect
|
1089 |
|
1090 |
+
from flask_cloudflared import _run_cloudflared
|
1091 |
+
|
1092 |
sig = inspect.signature(_run_cloudflared)
|
1093 |
sum = sum(
|
1094 |
1
|