Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -2,10 +2,7 @@ from fastapi import FastAPI
|
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from fastapi.responses import FileResponse
|
4 |
|
5 |
-
import platform
|
6 |
import subprocess
|
7 |
-
import logging
|
8 |
-
import urllib.request
|
9 |
import os
|
10 |
import json
|
11 |
import uuid
|
@@ -18,23 +15,16 @@ app = FastAPI()
|
|
18 |
@app.get("/generate")
|
19 |
def generate_image(prompt, inference_steps, model):
|
20 |
torch.cuda.empty_cache()
|
21 |
-
print(f"Is CUDA available: {torch.cuda.is_available()}")
|
22 |
|
23 |
pipeline = StableDiffusionPipeline.from_pretrained(str(model), torch_dtype=torch.float16)
|
24 |
-
|
25 |
-
#pipeline.scheduler = DPMSolverMultistepScheduler.from_config(pipeline.scheduler.config)
|
26 |
-
#another comment
|
27 |
pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
|
28 |
-
|
29 |
pipeline = pipeline.to("cuda")
|
|
|
30 |
image = pipeline(prompt, num_inference_steps=int(inference_steps), height=512, width=512).images[0]
|
31 |
|
32 |
filename = str(uuid.uuid4()) + ".jpg"
|
33 |
image.save(filename)
|
34 |
|
35 |
-
print(filename)
|
36 |
-
|
37 |
-
# Data to be written
|
38 |
assertion = {
|
39 |
"assertions": [
|
40 |
{
|
@@ -48,44 +38,16 @@ def generate_image(prompt, inference_steps, model):
|
|
48 |
]
|
49 |
}
|
50 |
|
51 |
-
print(assertion)
|
52 |
-
|
53 |
json_object = json.dumps(assertion, indent=4)
|
54 |
with open("assertion.json", "w") as outfile:
|
55 |
outfile.write(json_object)
|
56 |
|
57 |
subprocess.check_output(['./truepic-sign', 'init', 'file-system', '--api-key', os.environ.get("api_key")])
|
58 |
subprocess.check_output(['./truepic-sign', 'sign', filename, '--assertions-file', 'assertion.json', '--output', (os.getcwd() + '/static/' + filename)])
|
59 |
-
|
60 |
-
|
61 |
return {"response": filename}
|
62 |
|
63 |
|
64 |
-
@app.get("/generate-picsum")
|
65 |
-
def generate_picsum(prompt):
|
66 |
-
local_filename, headers = urllib.request.urlretrieve(('https://picsum.photos/id/' + prompt + '/800/800'))
|
67 |
-
|
68 |
-
# Data to be written
|
69 |
-
assertion = {
|
70 |
-
"assertions": [
|
71 |
-
{
|
72 |
-
"label": "com.truepic.custom.ai",
|
73 |
-
"data": {
|
74 |
-
"model_name": "Picsum",
|
75 |
-
"model_version": "1.0",
|
76 |
-
"prompt": prompt
|
77 |
-
}
|
78 |
-
}
|
79 |
-
]
|
80 |
-
}
|
81 |
-
|
82 |
-
json_object = json.dumps(assertion, indent=4)
|
83 |
-
with open("assertion.json", "w") as outfile:
|
84 |
-
outfile.write(json_object)
|
85 |
-
|
86 |
-
subprocess.check_output(['./truepic-sign', 'sign', local_filename, '--profile', 'demo', '--assertions', 'assertion.json', '--output', (os.getcwd() + '/static/output.jpg')])
|
87 |
-
return {"response": "success"}
|
88 |
-
|
89 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
90 |
|
91 |
@app.get("/")
|
|
|
2 |
from fastapi.staticfiles import StaticFiles
|
3 |
from fastapi.responses import FileResponse
|
4 |
|
|
|
5 |
import subprocess
|
|
|
|
|
6 |
import os
|
7 |
import json
|
8 |
import uuid
|
|
|
15 |
@app.get("/generate")
|
16 |
def generate_image(prompt, inference_steps, model):
|
17 |
torch.cuda.empty_cache()
|
|
|
18 |
|
19 |
pipeline = StableDiffusionPipeline.from_pretrained(str(model), torch_dtype=torch.float16)
|
|
|
|
|
|
|
20 |
pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
|
|
|
21 |
pipeline = pipeline.to("cuda")
|
22 |
+
|
23 |
image = pipeline(prompt, num_inference_steps=int(inference_steps), height=512, width=512).images[0]
|
24 |
|
25 |
filename = str(uuid.uuid4()) + ".jpg"
|
26 |
image.save(filename)
|
27 |
|
|
|
|
|
|
|
28 |
assertion = {
|
29 |
"assertions": [
|
30 |
{
|
|
|
38 |
]
|
39 |
}
|
40 |
|
|
|
|
|
41 |
json_object = json.dumps(assertion, indent=4)
|
42 |
with open("assertion.json", "w") as outfile:
|
43 |
outfile.write(json_object)
|
44 |
|
45 |
subprocess.check_output(['./truepic-sign', 'init', 'file-system', '--api-key', os.environ.get("api_key")])
|
46 |
subprocess.check_output(['./truepic-sign', 'sign', filename, '--assertions-file', 'assertion.json', '--output', (os.getcwd() + '/static/' + filename)])
|
47 |
+
|
|
|
48 |
return {"response": filename}
|
49 |
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
app.mount("/", StaticFiles(directory="static", html=True), name="static")
|
52 |
|
53 |
@app.get("/")
|