codermert commited on
Commit
1438e9f
·
verified ·
1 Parent(s): 0f7069c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -152
app.py CHANGED
@@ -1,153 +1,39 @@
1
  import gradio as gr
2
- import requests
3
- import io
4
- import random
5
- import os
6
- import time
7
- from PIL import Image
8
- import json
9
- import replicate
10
-
11
- # Project by Nymbo
12
-
13
- # API_URL = "https://api-inference.huggingface.co/models/codermert/mert2_flux"
14
- # API_TOKEN = os.getenv("HF_READ_TOKEN")
15
- # headers = {"Authorization": f"Bearer {API_TOKEN}"}
16
- # timeout = 100
17
-
18
- def query(prompt, aspect_ratio="1:1", steps=28, cfg_scale=3.5, seed=-1, strength=0.95):
19
-
20
- if seed == -1:
21
- seed = random.randint(1, 1000000000)
22
-
23
- input = {
24
- "prompt": prompt,
25
- "hf_lora": "codermert/mert2_flux",
26
- "output_format": "jpg",
27
- "aspect_ratio": aspect_ratio,
28
- "num_inference_steps": steps,
29
- "guidance_scale": cfg_scale,
30
- "lora_scale": strength,
31
- "seed": seed,
32
- "disable_safety_checker": True
33
- }
34
-
35
- # if(seed!=-1):
36
- # input = {
37
- # "prompt": prompt,
38
- # "hf_lora": "ovi054/rmx_flux",
39
- # "output_format": "jpg",
40
- # "aspect_ratio": aspect_ratio,
41
- # "num_inference_steps": steps,
42
- # "guidance_scale": cfg_scale,
43
- # "lora_scale": strength,
44
- # "seed": seed,
45
- # "disable_safety_checker": True
46
- # }
47
-
48
- output = replicate.run(
49
- "lucataco/flux-dev-lora:a22c463f11808638ad5e2ebd582e07a469031f48dd567366fb4c6fdab91d614d",
50
- input=input
51
- )
52
-
53
- print(output)
54
-
55
- return output[0], seed
56
-
57
-
58
-
59
- css = """
60
- #app-container {
61
- max-width: 600px;
62
- margin-left: auto;
63
- margin-right: auto;
64
- }
65
- """
66
-
67
- # Assuming `prompt` and other flagged data inputs need to be truncated.
68
- # def truncate_prompts(*args):
69
- # # Truncate the prompts (args[0] is text_prompt, args[1] is negative_prompt)
70
- # truncated_text_prompt = args[0][:200] if isinstance(args[0], str) else args[0]
71
- # # truncated_negative_prompt = args[1][:200] if isinstance(args[1], str) else args[1]
72
- # return (truncated_text_prompt, *args[1:])
73
- # Return the modified list with the truncated prompts
74
- # return (truncated_text_prompt, truncated_negative_prompt, *args[2:])
75
-
76
-
77
- examples = [
78
- "KAMALINEE, A charismatic speaker is captured mid-speech. She has long, tousled brown hair that’s slightly messy on top. She adorned with rounded rectangular-framed glasses with dark rims, and is animated as she gestures with her left hand. She is holding a black microphone in her right hand, speaking passionately. The woman is wearing a light grey sweater over a white t-shirt. She’s also wearing a simple black lanyard hanging around her neck. The lanyard badge has the text “Kamalinee”. Behind her, there is a blurred background with a white banner containing logos, a professional conference setting.",
79
- "KAMALINEE, An image of a woman. She dressed in a red and navy plaid shirt with the top unbuttoned to show a white undershirt, and the sleeves rolled up to the forearms. The woman is casually leaning against a weathered blue door frame with peeling paint, adding a rustic charm to the scene. Her arms are crossed or resting in front of her, and she has a soft, contemplative expression on her face.",
80
- "Digital illustration style, realistic, drunk beautiful woman KAMALINEE, drinking whiskey, side view, short hair, glossy red lips, moist eyes, v-neck collared shirt, dingy outdoor restaurant background, moonlight, backlighting",
81
- "an elegant and timeless portrait of a woman KAMALINEE, exuding grace and sophistication",
82
- "A woman KAMALINEE dressed as a pirate, in full growth, clear facial features, wearing a 3-cornered hat and black eye patch with hyper realistic background water, photograph taken with 35mm lens, f/1.8, sunlight, natural lighting",
83
- ]
84
-
85
-
86
- HF_TOKEN = os.getenv("SECRET_TOKEN")
87
- callback = gr.HuggingFaceDatasetSaver(HF_TOKEN, "rmx-data")
88
-
89
- # callback.setup([gr.Textbox, gr.Textbox, gr.Slider, gr.Slider, gr.Radio, gr.Slider, gr.Slider, gr.Image],
90
- # "flagged_data_points")
91
-
92
- with gr.Blocks(theme='Nymbo/Nymbo_Theme', css=css) as app:
93
- gr.HTML("<center><h1>RMX.1-Dev</h1></center>")
94
- with gr.Column(elem_id="app-container"):
95
- with gr.Row():
96
- with gr.Column(elem_id="prompt-container"):
97
- with gr.Row():
98
- text_prompt = gr.Textbox(label="Prompt", placeholder="Enter a prompt here", lines=2, elem_id="prompt-text-input")
99
- with gr.Row():
100
- with gr.Accordion("Advanced Settings", open=False):
101
- # negative_prompt = gr.Textbox(label="Negative Prompt", placeholder="What should not be in the image", value="(deformed, distorted, disfigured), poorly drawn, bad anatomy, wrong anatomy, extra limb, missing limb, floating limbs, (mutated hands and fingers), disconnected limbs, mutation, mutated, ugly, disgusting, blurry, amputation, misspellings, typos", lines=3, elem_id="negative-prompt-text-input")
102
- aspect_ratio = gr.Radio(label="Aspect ratio", value="1:1", choices=["1:1", "4:5", "2:3", "3:4","9:16", "4:3", "16:9"])
103
- steps = gr.Slider(label="Sampling steps", value=28, minimum=1, maximum=100, step=1)
104
- cfg = gr.Slider(label="CFG Scale", value=3.5, minimum=1, maximum=20, step=0.5)
105
- # method = gr.Radio(label="Sampling method", value="DPM++ 2M Karras", choices=["DPM++ 2M Karras", "DPM++ SDE Karras", "Euler", "Euler a", "Heun", "DDIM"])
106
- strength = gr.Slider(label="Strength", value=0.95, minimum=0, maximum=1, step=0.001)
107
- seed = gr.Slider(label="Seed", value=-1, minimum=-1, maximum=1000000000, step=1)
108
-
109
- with gr.Row():
110
- text_button = gr.Button("Run", variant='primary', elem_id="gen-button")
111
- with gr.Row():
112
- image_output = gr.Image(type="pil", label="Image Output",interactive=False, show_download_button=True, elem_id="gallery")
113
-
114
- with gr.Row():
115
- seed_output = gr.Textbox(label="Seed Used", interactive=False, show_copy_button = True, elem_id="seed-output")
116
-
117
- # Define examples that fill only the text_prompt input
118
- gr.Examples(
119
- examples = examples,
120
- fn = query,
121
- inputs = [text_prompt],
122
- )
123
-
124
-
125
- # text_button.click(query, inputs=[text_prompt, negative_prompt, steps, cfg, method, seed, strength], outputs=image_output)
126
-
127
-
128
- # We can choose which components to flag -- in this case, we'll flag all of them-, steps, cfg, method, seed, strength, image_output
129
- # text_button.click(lambda *args: callback.flag(truncate_prompts(*args)),
130
- # [text_prompt, negative_prompt,steps, cfg, method, seed, strength, image_output], None, preprocess=False, success=True)
131
-
132
- # Update the button click to first generate the image, then flag it
133
-
134
- callback.setup([text_prompt, aspect_ratio, steps, cfg, seed_output, strength, image_output],
135
- "flagged_data_points")
136
-
137
- def truncate_prompts(*args):
138
- truncated_text_prompt = args[0][:200] if isinstance(args[0], str) else args[0]
139
- return (truncated_text_prompt, *args[1:])
140
-
141
-
142
- text_button.click(
143
- query,
144
- inputs=[text_prompt, aspect_ratio, steps, cfg, seed, strength],
145
- outputs=[image_output,seed_output]
146
- ).then(
147
- lambda *args: callback.flag(truncate_prompts(*args)),
148
- inputs=[text_prompt, aspect_ratio, steps, cfg, seed_output, strength, image_output],
149
- outputs=None,
150
- preprocess=False
151
- )
152
-
153
- app.launch(show_api=False, share=False)
 
1
  import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline, DPMSolverMultistepScheduler
4
+ from safetensors.torch import load_file
5
+
6
+ model_id = "runwayml/stable-diffusion-v1-5"
7
+ lora_path = "https://huggingface.co/codermert/model_malika/resolve/main/sarah-lora.safetensors"
8
+
9
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
10
+ pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
11
+ pipe = pipe.to("cuda")
12
+
13
+ # LoRA dosyasını yükle
14
+ state_dict = load_file(lora_path)
15
+ pipe.unet.load_attn_procs(state_dict)
16
+
17
+ def generate_image(prompt, negative_prompt, guidance_scale, num_inference_steps):
18
+ image = pipe(
19
+ prompt=prompt,
20
+ negative_prompt=negative_prompt,
21
+ guidance_scale=guidance_scale,
22
+ num_inference_steps=num_inference_steps
23
+ ).images[0]
24
+ return image
25
+
26
+ iface = gr.Interface(
27
+ fn=generate_image,
28
+ inputs=[
29
+ gr.Textbox(label="Prompt"),
30
+ gr.Textbox(label="Negative Prompt"),
31
+ gr.Slider(minimum=1, maximum=20, step=0.5, label="Guidance Scale", value=7.5),
32
+ gr.Slider(minimum=1, maximum=100, step=1, label="Number of Inference Steps", value=50)
33
+ ],
34
+ outputs=gr.Image(label="Generated Image"),
35
+ title="Stable Diffusion with LoRA",
36
+ description="Generate images using Stable Diffusion v1.5 with a custom LoRA model."
37
+ )
38
+
39
+ iface.launch()