Update app.py
Browse files
app.py
CHANGED
@@ -4,11 +4,6 @@ import random
|
|
4 |
import torch
|
5 |
import spaces
|
6 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
|
7 |
-
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast, AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
8 |
-
from threading import Thread
|
9 |
-
|
10 |
-
dtype = torch.bfloat16
|
11 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
12 |
|
13 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
|
14 |
|
@@ -38,162 +33,18 @@ examples = [
|
|
38 |
]
|
39 |
|
40 |
|
41 |
-
# Image Gen css - Only saving for backup - ***disregard***
|
42 |
-
#css="""
|
43 |
-
#col-container {
|
44 |
-
# margin: 0 auto;
|
45 |
-
# max-width: 520px;
|
46 |
-
#}
|
47 |
-
#"""
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
.duplicate-button {
|
52 |
-
margin: auto !important;
|
53 |
-
color: white !important;
|
54 |
-
background: black !important;
|
55 |
-
border-radius: 100vh !important;
|
56 |
-
}
|
57 |
-
h3 {
|
58 |
-
text-align: center;
|
59 |
-
}
|
60 |
-
.chatbox .messages .message.user {
|
61 |
-
background-color: #e1f5fe;
|
62 |
-
}
|
63 |
-
.chatbox .messages .message.bot {
|
64 |
-
background-color: #eeeeee;
|
65 |
-
}
|
66 |
-
#col-container {
|
67 |
margin: 0 auto;
|
68 |
max-width: 520px;
|
69 |
}
|
70 |
"""
|
71 |
-
#################################################################################################################################
|
72 |
-
##########################################
|
73 |
-
|
74 |
-
|
75 |
-
model_id = "hugging-quants/Meta-Llama-3.1-70B-Instruct-AWQ-INT4"
|
76 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
77 |
-
model = AutoModelForCausalLM.from_pretrained(
|
78 |
-
model_id,
|
79 |
-
torch_dtype=torch.float16,
|
80 |
-
device_map="sequential",
|
81 |
-
offload_folder="offload",
|
82 |
-
offload_state_dict=True
|
83 |
-
)
|
84 |
-
|
85 |
-
TITLE = "Quick Description"
|
86 |
-
|
87 |
-
DESCRIPTION = """
|
88 |
-
Generate a longer description for your image from a simple basic prompt
|
89 |
-
"""
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
@spaces.GPU(duration=120)
|
94 |
-
def stream_chat(message: str, history: list, temperature: float, max_new_tokens: int, top_p: float, top_k: int, penalty: float):
|
95 |
-
print(f'Message: {message}')
|
96 |
-
print(f'History: {history}')
|
97 |
-
|
98 |
-
conversation = []
|
99 |
-
for prompt, answer in history:
|
100 |
-
conversation.extend([{"role": "user", "content": prompt}, {"role": "assistant", "content": answer}])
|
101 |
-
conversation.append({"role": "user", "content": message})
|
102 |
-
|
103 |
-
input_ids = tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True)
|
104 |
-
inputs = tokenizer(input_ids, return_tensors="pt").to(0)
|
105 |
-
|
106 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=10., skip_prompt=True, skip_special_tokens=True)
|
107 |
-
|
108 |
-
generate_kwargs = dict(
|
109 |
-
inputs,
|
110 |
-
streamer=streamer,
|
111 |
-
top_k=top_k,
|
112 |
-
top_p=top_p,
|
113 |
-
repetition_penalty=penalty,
|
114 |
-
max_new_tokens=max_new_tokens,
|
115 |
-
do_sample=True,
|
116 |
-
temperature=temperature,
|
117 |
-
eos_token_id=[128001, 128009],
|
118 |
-
)
|
119 |
-
|
120 |
-
thread = Thread(target=model.generate, kwargs=generate_kwargs)
|
121 |
-
thread.start()
|
122 |
-
|
123 |
-
buffer = ""
|
124 |
-
for new_text in streamer:
|
125 |
-
buffer += new_text
|
126 |
-
yield buffer
|
127 |
|
128 |
-
chatbot = gr.Chatbot(height=500)
|
129 |
-
|
130 |
-
with gr.Blocks(css=CSS) as demo:
|
131 |
-
gr.HTML(TITLE)
|
132 |
-
gr.HTML(DESCRIPTION)
|
133 |
-
gr.ChatInterface(
|
134 |
-
fn=stream_chat,
|
135 |
-
chatbot=chatbot,
|
136 |
-
fill_height=True,
|
137 |
-
theme="soft",
|
138 |
-
retry_btn=None,
|
139 |
-
undo_btn="Delete Previous",
|
140 |
-
clear_btn="Clear",
|
141 |
-
additional_inputs_accordion=gr.Accordion(label="⚙️ Parameters", open=False, render=False),
|
142 |
-
additional_inputs=[
|
143 |
-
gr.Slider(
|
144 |
-
minimum=0,
|
145 |
-
maximum=1,
|
146 |
-
step=0.1,
|
147 |
-
value=0.8,
|
148 |
-
label="Temperature",
|
149 |
-
render=False,
|
150 |
-
),
|
151 |
-
gr.Slider(
|
152 |
-
minimum=128,
|
153 |
-
maximum=4096,
|
154 |
-
step=1,
|
155 |
-
value=1024,
|
156 |
-
label="Max new tokens",
|
157 |
-
render=False,
|
158 |
-
),
|
159 |
-
gr.Slider(
|
160 |
-
minimum=0.0,
|
161 |
-
maximum=1.0,
|
162 |
-
step=0.1,
|
163 |
-
value=0.8,
|
164 |
-
label="top_p",
|
165 |
-
render=False,
|
166 |
-
),
|
167 |
-
gr.Slider(
|
168 |
-
minimum=1,
|
169 |
-
maximum=20,
|
170 |
-
step=1,
|
171 |
-
value=20,
|
172 |
-
label="top_k",
|
173 |
-
render=False,
|
174 |
-
),
|
175 |
-
gr.Slider(
|
176 |
-
minimum=0.0,
|
177 |
-
maximum=2.0,
|
178 |
-
step=0.1,
|
179 |
-
value=1.2,
|
180 |
-
label="Repetition penalty",
|
181 |
-
render=False,
|
182 |
-
),
|
183 |
-
],
|
184 |
-
examples=[
|
185 |
-
["Explain Deep Learning as a pirate."],
|
186 |
-
["Give me five ideas for a child's summer science project."],
|
187 |
-
["Provide advice for writing a script for a puzzle game."],
|
188 |
-
["Create a tutorial for building a breakout game using markdown."]
|
189 |
-
],
|
190 |
-
cache_examples=False,
|
191 |
-
)
|
192 |
-
#################################################################################################################################
|
193 |
with gr.Blocks(css=css) as demo:
|
194 |
|
195 |
with gr.Column(elem_id="col-container"):
|
196 |
-
gr.Markdown(f"""
|
197 |
""")
|
198 |
|
199 |
with gr.Row():
|
@@ -266,13 +117,6 @@ with gr.Blocks(css=css) as demo:
|
|
266 |
cache_examples="lazy"
|
267 |
)
|
268 |
|
269 |
-
def greet(name):
|
270 |
-
return "Hello " + name + "! Imagine an image with Flux"
|
271 |
-
|
272 |
-
name = gr.Textbox(label="Name")
|
273 |
-
output = gr.Textbox(label="Output Box")
|
274 |
-
greet_btn = gr.Button("Greet")
|
275 |
-
greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")
|
276 |
|
277 |
gr.on(
|
278 |
triggers=[run_button.click, prompt.submit],
|
|
|
4 |
import torch
|
5 |
import spaces
|
6 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-dev", torch_dtype=torch.bfloat16).to(device)
|
9 |
|
|
|
33 |
]
|
34 |
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
+
css="""
|
38 |
+
col-container {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
margin: 0 auto;
|
40 |
max-width: 520px;
|
41 |
}
|
42 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
with gr.Blocks(css=css) as demo:
|
45 |
|
46 |
with gr.Column(elem_id="col-container"):
|
47 |
+
gr.Markdown(f"""Generate an image with Flux. Please review the license if you are not familiar with it. Not for Commercial Use!]
|
48 |
""")
|
49 |
|
50 |
with gr.Row():
|
|
|
117 |
cache_examples="lazy"
|
118 |
)
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
gr.on(
|
122 |
triggers=[run_button.click, prompt.submit],
|