Update main.py
Browse files
main.py
CHANGED
@@ -1,68 +1,56 @@
|
|
1 |
-
from fastapi import FastAPI, HTTPException
|
2 |
-
from
|
3 |
-
from rdkit.Chem import Descriptors
|
4 |
-
from rdkit.Chem import rdMolDescriptors
|
5 |
import requests
|
6 |
-
from
|
|
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
num_rotatable_bonds = Descriptors.NumRotatableBonds(mol)
|
32 |
-
|
33 |
-
return {
|
34 |
-
'molecular_weight': mol_weight,
|
35 |
-
'number_of_atoms': num_atoms,
|
36 |
-
'number_of_bonds': num_bonds,
|
37 |
-
'molecular_formula': mol_formula,
|
38 |
-
'tpsa': tpsa,
|
39 |
-
'logP': mol_logp,
|
40 |
-
'number_of_rotatable_bonds': num_rotatable_bonds
|
41 |
}
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
return info
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
import uvicorn
|
68 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
|
|
1 |
+
from fastapi import FastAPI, HTTPException
|
2 |
+
from fastapi.responses import StreamingResponse
|
|
|
|
|
3 |
import requests
|
4 |
+
from urllib.parse import quote
|
5 |
+
import asyncio
|
6 |
+
from io import BytesIO
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
10 |
+
def create_job(prompt, model, sampler, seed, neg):
|
11 |
+
if model is None:
|
12 |
+
model = 'Realistic_Vision_V5.0.safetensors [614d1063]'
|
13 |
+
if sampler is None:
|
14 |
+
sampler = 'DPM++ 2M Karras'
|
15 |
+
if seed is None:
|
16 |
+
seed = '-1'
|
17 |
+
if neg is None:
|
18 |
+
neg = "(long list of negative prompts removed for brevity)"
|
19 |
+
url = 'https://api.prodia.com/generate'
|
20 |
+
params = {
|
21 |
+
'new': 'true',
|
22 |
+
'prompt': quote(prompt),
|
23 |
+
'model': model,
|
24 |
+
'negative_prompt': neg,
|
25 |
+
'steps': '100',
|
26 |
+
'cfg': '9.5',
|
27 |
+
'seed': seed,
|
28 |
+
'sampler': sampler,
|
29 |
+
'upscale': 'True',
|
30 |
+
'aspect_ratio': 'square'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
}
|
32 |
+
response = requests.get(url, params=params)
|
33 |
+
response.raise_for_status()
|
34 |
+
return response.json()['job']
|
35 |
+
|
36 |
+
@app.get("/generate_image")
|
37 |
+
async def generate_image(prompt: str, model: str = None, sampler: str = None, seed: str = None, neg: str = None):
|
38 |
+
job_id = create_job(prompt, model, sampler, seed, neg)
|
39 |
+
url = f'https://api.prodia.com/job/{job_id}'
|
40 |
+
headers = {'accept': '*/*'}
|
41 |
+
|
42 |
+
while True:
|
43 |
+
response = requests.get(url=url, headers=headers)
|
44 |
+
response.raise_for_status()
|
45 |
+
job_response = response.json()
|
46 |
+
if job_response['status'] == 'succeeded':
|
47 |
+
image_url = f'https://images.prodia.xyz/{job_id}.png'
|
48 |
+
image_response = requests.get(image_url)
|
49 |
+
image_response.raise_for_status()
|
50 |
+
image = BytesIO(image_response.content)
|
51 |
+
return StreamingResponse(image, media_type='image/png')
|
52 |
+
await asyncio.sleep(2) # Add a delay to prevent excessive requests
|
|
|
|
|
53 |
|
54 |
if __name__ == "__main__":
|
55 |
import uvicorn
|
56 |
+
uvicorn.run(app, host="0.0.0.0", port=8000, debug=True)
|