Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests, json, random, string
|
| 2 |
+
from fastapi import FastAPI, Query
|
| 3 |
+
from fastapi.responses import StreamingResponse
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
BASE_URL = "https://qwen-qwen-image.hf.space/gradio_api"
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def session_hash():
|
| 11 |
+
return ''.join(random.choices(string.ascii_lowercase + string.digits, k=12))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@app.get("/api/generate/prompt")
|
| 15 |
+
def generate(prompt: str = Query(...)):
|
| 16 |
+
sh = session_hash()
|
| 17 |
+
|
| 18 |
+
payload = {
|
| 19 |
+
"data": [
|
| 20 |
+
prompt,
|
| 21 |
+
0,
|
| 22 |
+
True,
|
| 23 |
+
"16:9",
|
| 24 |
+
4.0,
|
| 25 |
+
50,
|
| 26 |
+
True
|
| 27 |
+
],
|
| 28 |
+
"event_data": None,
|
| 29 |
+
"fn_index": 1,
|
| 30 |
+
"trigger_id": 6,
|
| 31 |
+
"session_hash": sh
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
requests.post(
|
| 35 |
+
f"{BASE_URL}/queue/join?__theme=dark",
|
| 36 |
+
json=payload,
|
| 37 |
+
headers={"Content-Type": "application/json"}
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def stream():
|
| 41 |
+
url = f"{BASE_URL}/queue/data?session_hash={sh}"
|
| 42 |
+
with requests.get(url, stream=True) as r:
|
| 43 |
+
for line in r.iter_lines():
|
| 44 |
+
if line:
|
| 45 |
+
yield line + b"\n"
|
| 46 |
+
|
| 47 |
+
return StreamingResponse(stream(), media_type="text/event-stream")
|