File size: 8,993 Bytes
de0bb21
 
 
90a06ca
 
de0bb21
 
 
 
 
90a06ca
 
de0bb21
 
 
90a06ca
de0bb21
 
 
 
 
90a06ca
de0bb21
 
 
 
90a06ca
 
de0bb21
 
0d54635
de0bb21
 
 
 
 
90a06ca
de0bb21
 
 
 
 
 
 
 
386e46b
de0bb21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
90a06ca
de0bb21
90a06ca
 
de0bb21
 
90a06ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de0bb21
 
 
 
 
 
90a06ca
de0bb21
 
90a06ca
de0bb21
 
 
 
386e46b
de0bb21
 
 
 
 
 
386e46b
de0bb21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import http.server
import socketserver
import urllib.parse
import json
import base64
from curl_cffi import requests
import sys

class ImageHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        print(f"[{self.log_date_time_string()}] '{self.address_string()}': GET {self.path}")
        
        if self.path.startswith('/steps/'):
            parsed_path = urllib.parse.unquote(self.path)
            parts = parsed_path.split('/', 3)
            print(f"  Parsed path: {parts}")
            
            if len(parts) >= 3:
                try:
                    steps = int(parts[2])
                    prompt = '/'.join(parts[3:]) if len(parts) > 3 else ""
                    print(f"  Steps: {steps}, Prompt: '{prompt}'")
                    
                    if prompt:
                        self.generate_and_serve_image(prompt, steps)
                        return
                except (ValueError, IndexError) as e:
                    print(f"  Parse error: {e}")
                    pass
        
        print("  404 - Path doesn't match /steps/{steps}/pattern")
        self.send_response(404)
        self.end_headers()
        self.wfile.write(b'Not Found')
    
    def generate_and_serve_image(self, prompt: str, num_steps: int = 4):
        print(f"  Generating image for prompt: '{prompt}' with {num_steps} steps")
        try:
            url = "https://multi-modal.ai.cloudflare.com/api/inference"
            
            headers = {
                'accept': '*/*',
                'accept-language': 'en-US,en;q=0.9',
                'cache-control': 'no-cache',
                'content-type': 'application/json',
                'cookie': '_biz_uid=f039195dc4204df9da5d4e3b7066e368; _mkto_trk=id:713-XSC-918&token:_mch-cloudflare.com-70f2772b2e70dc0b5dcd8858a70f3817; _ga_8BK794H3J9=GS1.1.1745122895.1.1.1745122971.0.0.0; _biz_flagsA=%7B%22Version%22%3A1%2C%22XDomain%22%3A%221%22%2C%22ViewThrough%22%3A%221%22%2C%22Mkto%22%3A%221%22%2C%22Ecid%22%3A%22251012964%22%7D; cfz_facebook-pixel=%7B%22OwdI_fb-pixel%22%3A%7B%22v%22%3A%22fb.2.1744224978265.1003215434%22%2C%22e%22%3A1775760978265%7D%2C%22VVgx_fb-pixel%22%3A%7B%22v%22%3A%22fb.2.1744224978265.326138134%22%2C%22e%22%3A1775760978265%7D%2C%22bHox_fb-pixel%22%3A%7B%22v%22%3A%22fb.2.1744224978265.463779223%22%2C%22e%22%3A1775760978265%7D%2C%22elKW_fb-pixel%22%3A%7B%22v%22%3A%22fb.2.1744224978265.1208552939%22%2C%22e%22%3A1775760978265%7D%2C%22dzQR_fb-pixel%22%3A%7B%22v%22%3A%22fb.2.1746847816852.132900741%22%2C%22e%22%3A1778383816852%7D%7D; _ga_NPJQ92CQTK=GS2.1.s1747528838$o1$g0$t1747528847$j0$l0$h0; cfz_zaraz-analytics=%7B%22_cfa_clientId%22%3A%7B%22v%22%3A%2235625994394362004%22%2C%22e%22%3A1773974975733%7D%7D; zaraz-consent={"Tuku":true,"aMDT":true,"lryA":true,"YTmY":true}; OptanonConsent=isGpcEnabled=0&datestamp=Thu+Sep+04+2025+11%3A00%3A57+GMT-0700+(Pacific+Daylight+Time)&version=202310.2.0&browserGpcFlag=0&isIABGlobal=false&hosts=&consentId=399495dc-969b-45a8-b4f3-319d001982a5&interactionCount=1&isAnonUser=1&landingPath=NotLandingPage&groups=SSPD_BG%3A1%2CC0004%3A1%2CC0002%3A1%2CC0003%3A1%2CC0001%3A1&AwaitingReconsent=false; _ga=GA1.1.ed07327b-c999-4e25-89cc-11724cb95915; _uetvid=19aa8610dd0f11ef9459b70ed2100f99|1k749jc|1760835793501|1|1|bat.bing.com/p/insights/c/i; _ga_SQCRB0TXZW=GS2.1.s1760835791$o61$g1$t1760835804$j47$l0$h0$d5y8fuUQP0L-TmEsFfyn9G5nyQMYe0VYjOw; CF_VERIFIED_DEVICE_9d684455d07b4a57cd5411d6748b9acae6f426f4e40e45f836793f1f60701bf7=1761772679; AMCV_8AD56F28618A50850A495FB6%40AdobeOrg=179643557%7CMCMID%7C34403450697062221771928232009572502495%7CMCIDTS%7C20432%7CMCAAMLH-1765905548%7C9%7CMCAAMB-1765905548%7Cj8Odv6LonN4r3an7LhD3WZrU1bUpAkFkkiY1ncBR96t2PTI%7CMCOPTOUT-1765307948s%7CNONE%7CvVersion%7C5.5.0; cfz_adobe=%7B%22MsVJ_ecid%22%3A%7B%22v%22%3A%22CiYzNDQwMzQ1MDY5NzA2MjIyMTc3MTkyODIzMjAwOTU3MjUwMjQ5NVIQCJqTpN_hMhgBKgNWQTYwA_AB-YX_oLAz%22%2C%22e%22%3A1799613197869%7D%7D; CF_VERIFIED_DEVICE_254287c9bd0d28997963ced5a664b0e5ea496b5b2ce91992acb42647f38b21f1=1765487159; sparrow_id={"deviceId":"0a8a5454-47c7-4828-bb99-3b6089a9ce30"}; _biz_nA=175; _biz_pendingA=%5B%5D; cfz_amplitude=%7B%22TTin_event_id%22%3A%7B%22v%22%3A%22309%22%2C%22e%22%3A1797549980022%7D%2C%22TTin_device_id%22%3A%7B%22v%22%3A%22736590d3-2137-43dd-9ce6-e1f5729e6f7c%22%2C%22e%22%3A1775760979730%7D%2C%22FDpb_event_id%22%3A%7B%22v%22%3A%229%22%2C%22e%22%3A1777250687224%7D%2C%22FDpb_device_id%22%3A%7B%22v%22%3A%221aaeb293-3680-4db3-9474-2f16e4c09582%22%2C%22e%22%3A1777250649901%7D%7D; cfz_google-analytics_v4=%7B%22nzcr_engagementDuration%22%3A%7B%22v%22%3A%223536%22%2C%22e%22%3A1797549983527%7D%2C%22nzcr_engagementStart%22%3A%7B%22v%22%3A%221766013983527%22%2C%22e%22%3A1797549983527%7D%2C%22nzcr_counter%22%3A%7B%22v%22%3A%221311%22%2C%22e%22%3A1797549979991%7D%2C%22nzcr_session_counter%22%3A%7B%22v%22%3A%22105%22%2C%22e%22%3A1797549979991%7D%2C%22nzcr_ga4%22%3A%7B%22v%22%3A%22ed07327b-c999-4e25-89cc-11724cb95915%22%2C%22e%22%3A1797549979991%7D%2C%22nzcr__z_ga_audiences%22%3A%7B%22v%22%3A%22ed07327b-c999-4e25-89cc-11724cb95915%22%2C%22e%22%3A1773974977600%7D%2C%22nzcr_let%22%3A%7B%22v%22%3A%221766013979991%22%2C%22e%22%3A1797549979991%7D%7D; cfz_reddit=%7B%22fZaD_reddit_uuid%22%3A%7B%22v%22%3A%221767896575113.bb4b01c8-26ba-49db-b58c-c1e15e29f9c3%22%2C%22e%22%3A1799432575113%7D%7D; OptanonConsent=isGpcEnabled=0&datestamp=Thu+Jan+08+2026+10%3A22%3A55+GMT-0800+(Pacific+Standard+Time)&version=202503.1.0&browserGpcFlag=0&isIABGlobal=false&hosts=&consentId=3e1d8b67-a322-45f7-a3a2-50bcf94178f9&interactionCount=1&isAnonUser=1&landingPath=NotLandingPage&groups=SSPD_BG%3A1%2CC0004%3A1%2CC0002%3A1%2CC0003%3A1%2CC0001%3A1&AwaitingReconsent=false; kndctr_8AD56F28618A50850A495FB6_AdobeOrg_identity=CiYzNDQwMzQ1MDY5NzA2MjIyMTc3MTkyODIzMjAwOTU3MjUwMjQ5NVIQCJqTpN_hMhgBKgNWQTYwA_ABl9_j9rkz; __q_state_37pXYrro6wCZbsU7=eyJ1dWlkIjoiN2UxMjE4YWUtNDk4NC00YWVlLWFmZmEtMWQ4Y2ViNjc5ODY0IiwiY29va2llRG9tYWluIjoiY2xvdWRmbGFyZS5jb20iLCJtZXNzZW5nZXJFeHBhbmRlZCI6ZmFsc2UsInByb21wdERpc21pc3NlZCI6ZmFsc2UsImNvbnZlcnNhdGlvbklkIjoiMTgyMzgwNjA3OTQ2OTc5ODg1MSJ9',
                'origin': 'https://multi-modal.ai.cloudflare.com',
                'pragma': 'no-cache',
                'priority': 'u=1, i',
                'referer': 'https://multi-modal.ai.cloudflare.com/',
                'sec-fetch-dest': 'empty',
                'sec-fetch-mode': 'cors',
                'sec-fetch-site': 'same-origin',
                'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1'
            }
            
            payload = {
                "model": "@cf/black-forest-labs/flux-1-schnell",
                "prompt": prompt,
                "num_steps": num_steps,
                "guidance": 2.0
            }
            
            print(f"  API request sent to {url}")
            resp = requests.post(url, json=payload, headers=headers, impersonate="safari")
            print(f"  API response status: {resp.status_code}")
            
            resp.raise_for_status()
            
            try:
                json_resp = resp.json()
                print(f"  JSON response keys: {list(json_resp.keys())}")
                
                if 'response' in json_resp and 'image' in json_resp['response']:
                    image_b64 = json_resp['response']['image']
                    print(f"  Extracted base64 image data: {len(image_b64)} chars")
                    
                    image_bytes = base64.b64decode(image_b64)
                    print(f"  Decoded to {len(image_bytes)} bytes")
                else:
                    print(f"  Unexpected JSON structure: {json_resp}")
                    raise ValueError("No image data in response")
                    
            except json.JSONDecodeError:
                image_bytes = resp.content
                print(f"  Direct binary response: {len(image_bytes)} bytes")
            
            print(f"  Serving {len(image_bytes)} bytes as image/png")
            self.send_response(200)
            self.send_header('Content-Type', 'image/png')
            self.send_header('Content-Length', len(image_bytes))
            self.send_header('Cache-Control', 'no-cache')
            self.end_headers()
            self.wfile.write(image_bytes)
            print(f"  Image served successfully")
            
        except Exception as e:
            print(f"  ERROR: {str(e)}")
            self.send_response(500)
            self.end_headers()
            self.wfile.write(f'Error: {str(e)}'.encode())

def run_server(port: int = 7860):
    with socketserver.TCPServer(("", port), ImageHandler) as httpd:
        print(f"Server running at http://localhost:{port}")
        print(f"Usage: http://localhost:{port}/steps/4/{{your-prompt-here}}")
        httpd.serve_forever()

if __name__ == "__main__":
    port = int(sys.argv[1]) if len(sys.argv) > 1 else 7860
    run_server(port)