Spaces:
Runtime error
Runtime error
Create Lex.py
Browse files
Lex.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import httpx
|
| 2 |
+
import random
|
| 3 |
+
import string
|
| 4 |
+
import uuid
|
| 5 |
+
import re
|
| 6 |
+
|
| 7 |
+
class Lexica:
|
| 8 |
+
def __init__(self, query, negativePrompt="", guidanceScale: int = 7, portrait: bool = True, cookie=None):
|
| 9 |
+
self.query = query
|
| 10 |
+
self.negativePrompt = negativePrompt
|
| 11 |
+
self.guidanceScale = guidanceScale
|
| 12 |
+
self.portrait = portrait
|
| 13 |
+
self.cookie = cookie
|
| 14 |
+
|
| 15 |
+
def images(self):
|
| 16 |
+
response = httpx.post("https://lexica.art/api/infinite-prompts", json={
|
| 17 |
+
"text": self.query,
|
| 18 |
+
"searchMode": "images",
|
| 19 |
+
"source": "search",
|
| 20 |
+
"model": "lexica-aperture-v2"
|
| 21 |
+
})
|
| 22 |
+
|
| 23 |
+
prompts = [f"https://image.lexica.art/full_jpg/{ids['id']}" for ids in response.json()["images"]]
|
| 24 |
+
|
| 25 |
+
return prompts
|
| 26 |
+
|
| 27 |
+
def _generate_random_string(self, length):
|
| 28 |
+
chars = string.ascii_letters + string.digits
|
| 29 |
+
result_str = ''.join(random.choice(chars) for _ in range(length))
|
| 30 |
+
|
| 31 |
+
return result_str
|
| 32 |
+
|
| 33 |
+
def generate(self):
|
| 34 |
+
response = httpx.post("https://z.lexica.art/api/generator", headers={
|
| 35 |
+
"cookie": self.cookie
|
| 36 |
+
}, json={
|
| 37 |
+
"requestId": str(uuid.uuid4()),
|
| 38 |
+
"id": self._generate_random_string(20),
|
| 39 |
+
"prompt": self.query,
|
| 40 |
+
"negativePrompt": self.negativePrompt,
|
| 41 |
+
"guidanceScale": self.guidanceScale,
|
| 42 |
+
"width": 512 if self.portrait else 768,
|
| 43 |
+
"height": 768 if self.portrait else 512,
|
| 44 |
+
"enableHiresFix": False,
|
| 45 |
+
"model": "lexica-aperture-v2",
|
| 46 |
+
"generateSources": []
|
| 47 |
+
}, timeout=50
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
return [f"https://image.lexica.art/full_jpg/{ids['id']}" for ids in response.json()["images"]]
|