Update app.py
Browse files
app.py
CHANGED
@@ -4,7 +4,8 @@ import random
|
|
4 |
import torch
|
5 |
import spaces
|
6 |
from diffusers import DiffusionPipeline, FlowMatchEulerDiscreteScheduler
|
7 |
-
from transformers import CLIPTextModel, CLIPTokenizer,T5EncoderModel, T5TokenizerFast
|
|
|
8 |
|
9 |
dtype = torch.bfloat16
|
10 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
@@ -36,19 +37,163 @@ examples = [
|
|
36 |
"Depict a breathtaking scene of a meteor rain showering down from a starry night sky. The meteors should vary in size and brightness, streaking across the sky with vibrant tails of light, creating a dazzling display. Below, a serene landscape—perhaps a tranquil lake reflecting the celestial spectacle, or a rugged mountain range—should enhance the sense of wonder. The foreground can include silhouettes of trees or figures gazing up in awe at the cosmic event. The overall atmosphere should evoke feelings of magic and inspiration, capturing the beauty and mystery of the universe.",
|
37 |
]
|
38 |
|
39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
#col-container {
|
41 |
margin: 0 auto;
|
42 |
max-width: 520px;
|
43 |
}
|
44 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
with gr.Blocks(css=css) as demo:
|
47 |
|
48 |
with gr.Column(elem_id="col-container"):
|
49 |
-
gr.Markdown(f"""
|
50 |
-
12B param rectified flow transformer guidance-distilled from [FLUX.1 [pro]](https://blackforestlabs.ai/)
|
51 |
-
[[non-commercial license](https://huggingface.co/black-forest-labs/FLUX.1-dev/blob/main/LICENSE.md)] [[blog](https://blackforestlabs.ai/announcing-black-forest-labs/)] [[model](https://huggingface.co/black-forest-labs/FLUX.1-dev)]
|
52 |
""")
|
53 |
|
54 |
with gr.Row():
|
@@ -136,4 +281,7 @@ with gr.Blocks(css=css) as demo:
|
|
136 |
outputs = [result, seed]
|
137 |
)
|
138 |
|
139 |
-
demo.launch()
|
|
|
|
|
|
|
|
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"
|
|
|
37 |
"Depict a breathtaking scene of a meteor rain showering down from a starry night sky. The meteors should vary in size and brightness, streaking across the sky with vibrant tails of light, creating a dazzling display. Below, a serene landscape—perhaps a tranquil lake reflecting the celestial spectacle, or a rugged mountain range—should enhance the sense of wonder. The foreground can include silhouettes of trees or figures gazing up in awe at the cosmic event. The overall atmosphere should evoke feelings of magic and inspiration, capturing the beauty and mystery of the universe.",
|
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 |
+
# Llama + Flux CSS
|
50 |
+
CSS = """
|
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"""TESTTESTTESTTESTTESTTEST]
|
|
|
|
|
197 |
""")
|
198 |
|
199 |
with gr.Row():
|
|
|
281 |
outputs = [result, seed]
|
282 |
)
|
283 |
|
284 |
+
demo.launch()
|
285 |
+
|
286 |
+
|
287 |
+
|