Sada8888 commited on
Commit
797a1ca
·
verified ·
1 Parent(s): 3e9714d

Create yml/generate-midjourney.py

Browse files
Files changed (1) hide show
  1. yml/generate-midjourney.py +52 -0
yml/generate-midjourney.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os, sys, random, requests
2
+ from gradio_client import Client
3
+
4
+ raw_prompt = os.environ.get('PROMPT', '')
5
+ # اضافه کردن کلمه کلیدی میدجرنی
6
+ final_prompt = f'MJ v6 {raw_prompt}'
7
+
8
+ width = int(os.environ.get('WIDTH', 1024))
9
+ height = int(os.environ.get('HEIGHT', 1024))
10
+ run_id = os.environ.get('RUN_ID')
11
+ space_url = os.environ.get('SPACE_URL')
12
+ github_run_id = os.environ.get('GITHUB_RUN_ID')
13
+
14
+ print('1. Connecting to LoRA DLC Space...')
15
+ try:
16
+ client = Client('opera8/FLUX-LoRA-DLC')
17
+
18
+ print('2. Loading Midjourney Model...')
19
+ # لود کردن مدل میدجرنی
20
+ client.predict('strangerzonehf/Flux-Midjourney-Mix2-LoRA', api_name='/add_custom_lora')
21
+
22
+ print(f'3. Generating Image... Prompt: {final_prompt[:50]}...')
23
+ result = client.predict(
24
+ prompt=final_prompt,
25
+ image_input=None,
26
+ cfg_scale=3.5,
27
+ steps=28,
28
+ randomize_seed=True,
29
+ seed=random.randint(1, 2147483647),
30
+ width=width,
31
+ height=height,
32
+ lora_scale=0.8,
33
+ api_name='/run_lora'
34
+ )
35
+
36
+ image_path = result[0] if isinstance(result, (tuple, list)) else result
37
+
38
+ print('4. Uploading back to Docker Space...')
39
+ with open(image_path, 'rb') as f:
40
+ res = requests.post(f'{space_url}/api/webhook/upload',
41
+ data={'run_id': run_id, 'github_run_id': github_run_id},
42
+ files={'file': f})
43
+
44
+ if res.status_code == 200:
45
+ print('5. SUCCESS!')
46
+ else:
47
+ print(f'Upload Error: {res.text}')
48
+ sys.exit(1)
49
+
50
+ except Exception as e:
51
+ print(f'CRITICAL ERROR: {e}')
52
+ sys.exit(1)