Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,232 +1,151 @@
|
|
1 |
-
import gradio as gr
|
2 |
import spaces
|
3 |
-
import
|
4 |
-
|
5 |
-
|
6 |
-
import
|
7 |
-
import
|
8 |
-
from
|
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 |
-
"prompt": "hyper-realistic 8K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
44 |
-
},
|
45 |
-
{
|
46 |
-
"name": "2560 x 1440",
|
47 |
-
"prompt": "hyper-realistic 4K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
48 |
-
},
|
49 |
-
{
|
50 |
-
"name": "HD+",
|
51 |
-
"prompt": "hyper-realistic 2K image of {prompt}. ultra-detailed, lifelike, high-resolution, sharp, vibrant colors, photorealistic",
|
52 |
-
},
|
53 |
-
{
|
54 |
-
"name": "Style Zero",
|
55 |
-
"prompt": "{prompt}",
|
56 |
-
},
|
57 |
-
]
|
58 |
-
|
59 |
-
styles = {k["name"]: k["prompt"] for k in style_list}
|
60 |
-
|
61 |
-
DEFAULT_STYLE_NAME = "3840 x 2160"
|
62 |
-
STYLE_NAMES = list(styles.keys())
|
63 |
-
|
64 |
-
def apply_style(style_name: str, positive: str) -> str:
|
65 |
-
return styles.get(style_name, styles[DEFAULT_STYLE_NAME]).replace("{prompt}", positive)
|
66 |
-
|
67 |
-
@spaces.GPU(duration=60, enable_queue=True)
|
68 |
-
def generate(
|
69 |
-
prompt: str,
|
70 |
-
seed: int = 0,
|
71 |
-
width: int = 1024,
|
72 |
-
height: int = 1024,
|
73 |
-
guidance_scale: float = 3,
|
74 |
-
randomize_seed: bool = False,
|
75 |
-
style_name: str = DEFAULT_STYLE_NAME,
|
76 |
-
lora_choice: str = "Sketch-Smudge",
|
77 |
-
progress=gr.Progress(track_tqdm=True),
|
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 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
.
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
)
|
168 |
-
|
169 |
-
with gr.Row():
|
170 |
-
guidance_scale = gr.Slider(
|
171 |
-
label="Guidance Scale",
|
172 |
-
minimum=0.1,
|
173 |
-
maximum=20.0,
|
174 |
-
step=0.1,
|
175 |
-
value=3.0,
|
176 |
-
)
|
177 |
-
num_inference_steps = gr.Slider(
|
178 |
-
label="Number of inference steps",
|
179 |
-
minimum=1,
|
180 |
-
maximum=40,
|
181 |
-
step=1,
|
182 |
-
value=30,
|
183 |
-
)
|
184 |
-
|
185 |
-
style_selection = gr.Radio(
|
186 |
-
show_label=True,
|
187 |
-
container=True,
|
188 |
-
interactive=True,
|
189 |
-
choices=STYLE_NAMES,
|
190 |
-
value=DEFAULT_STYLE_NAME,
|
191 |
-
label="Quality Style",
|
192 |
-
)
|
193 |
-
|
194 |
-
model_choice = gr.Dropdown(
|
195 |
-
label="LoRA Selection",
|
196 |
-
choices=list(LORA_OPTIONS.keys()),
|
197 |
-
value="Sketch-Smudge"
|
198 |
-
)
|
199 |
-
|
200 |
-
with gr.Column(scale=2):
|
201 |
-
result = gr.Gallery(label="Result", columns=1, show_label=False)
|
202 |
-
|
203 |
-
gr.Examples(
|
204 |
-
examples=examples,
|
205 |
-
inputs=prompt,
|
206 |
-
outputs=[result, seed],
|
207 |
-
fn=generate,
|
208 |
-
cache_examples=False,
|
209 |
-
)
|
210 |
-
|
211 |
-
gr.on(
|
212 |
-
triggers=[
|
213 |
-
prompt.submit,
|
214 |
-
run_button.click,
|
215 |
-
],
|
216 |
-
fn=generate,
|
217 |
-
inputs=[
|
218 |
-
prompt,
|
219 |
-
seed,
|
220 |
-
width,
|
221 |
-
height,
|
222 |
-
guidance_scale,
|
223 |
-
randomize_seed,
|
224 |
-
style_selection,
|
225 |
-
model_choice,
|
226 |
-
],
|
227 |
-
outputs=[result, seed],
|
228 |
-
api_name="run",
|
229 |
)
|
|
|
230 |
|
231 |
if __name__ == "__main__":
|
232 |
-
demo.
|
|
|
|
|
1 |
import spaces
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import subprocess
|
5 |
+
from llama_cpp import Llama
|
6 |
+
from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
|
7 |
+
from llama_cpp_agent.providers import LlamaCppPythonProvider
|
8 |
+
from llama_cpp_agent.chat_history import BasicChatHistory
|
9 |
+
from llama_cpp_agent.chat_history.messages import Roles
|
10 |
+
import gradio as gr
|
11 |
+
from huggingface_hub import hf_hub_download
|
12 |
+
|
13 |
+
|
14 |
+
huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
|
15 |
+
|
16 |
+
hf_hub_download(
|
17 |
+
repo_id="prithivMLmods/GWQ-9B-Preview2-GGUF",
|
18 |
+
filename="gemma-with-question-prev-1-f16.gguf",
|
19 |
+
local_dir="./models"
|
20 |
+
)
|
21 |
+
|
22 |
+
hf_hub_download(
|
23 |
+
repo_id="SanctumAI/gemma-2-9b-it-GGUF",
|
24 |
+
filename="gemma-2-9b-it.f16.gguf",
|
25 |
+
local_dir="./models"
|
26 |
+
)
|
27 |
+
|
28 |
+
llm = None
|
29 |
+
llm_model = None
|
30 |
+
|
31 |
+
@spaces.GPU(duration=120)
|
32 |
+
def respond(
|
33 |
+
message,
|
34 |
+
history: list[tuple[str, str]],
|
35 |
+
model,
|
36 |
+
system_message,
|
37 |
+
max_tokens,
|
38 |
+
temperature,
|
39 |
+
top_p,
|
40 |
+
top_k,
|
41 |
+
repeat_penalty,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
):
|
43 |
+
chat_template = MessagesFormatterType.GEMMA_2
|
44 |
|
45 |
+
global llm
|
46 |
+
global llm_model
|
47 |
|
48 |
+
if llm is None or llm_model != model:
|
49 |
+
llm = Llama(
|
50 |
+
model_path=f"models/{model}",
|
51 |
+
flash_attn=True,
|
52 |
+
n_gpu_layers=81,
|
53 |
+
n_batch=1024,
|
54 |
+
n_ctx=8192,
|
55 |
+
)
|
56 |
+
llm_model = model
|
57 |
+
|
58 |
+
provider = LlamaCppPythonProvider(llm)
|
59 |
+
|
60 |
+
agent = LlamaCppAgent(
|
61 |
+
provider,
|
62 |
+
system_prompt=f"{system_message}",
|
63 |
+
predefined_messages_formatter_type=chat_template,
|
64 |
+
debug_output=True
|
65 |
+
)
|
66 |
|
67 |
+
settings = provider.get_provider_default_settings()
|
68 |
+
settings.temperature = temperature
|
69 |
+
settings.top_k = top_k
|
70 |
+
settings.top_p = top_p
|
71 |
+
settings.max_tokens = max_tokens
|
72 |
+
settings.repeat_penalty = repeat_penalty
|
73 |
+
settings.stream = True
|
74 |
+
|
75 |
+
messages = BasicChatHistory()
|
76 |
+
|
77 |
+
for msn in history:
|
78 |
+
user = {
|
79 |
+
'role': Roles.user,
|
80 |
+
'content': msn[0]
|
81 |
+
}
|
82 |
+
assistant = {
|
83 |
+
'role': Roles.assistant,
|
84 |
+
'content': msn[1]
|
85 |
+
}
|
86 |
+
messages.add_message(user)
|
87 |
+
messages.add_message(assistant)
|
88 |
+
|
89 |
+
stream = agent.get_chat_response(
|
90 |
+
message,
|
91 |
+
llm_sampling_settings=settings,
|
92 |
+
chat_history=messages,
|
93 |
+
returns_streaming_generator=True,
|
94 |
+
print_output=False
|
95 |
+
)
|
96 |
+
|
97 |
+
outputs = ""
|
98 |
+
for output in stream:
|
99 |
+
outputs += output
|
100 |
+
yield outputs
|
101 |
+
|
102 |
+
demo = gr.ChatInterface(
|
103 |
+
respond,
|
104 |
+
additional_inputs=[
|
105 |
+
gr.Dropdown([
|
106 |
+
'gemma-with-question-prev-1-f16.gguf',
|
107 |
+
'gemma-2-9b-it.f16.gguf'
|
108 |
+
],
|
109 |
+
value="gemma-with-question-prev-1-f16.gguf",
|
110 |
+
label="Model"
|
111 |
+
),
|
112 |
+
gr.Textbox(value="You are a helpful assistant.", label="System message"),
|
113 |
+
gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
|
114 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
115 |
+
gr.Slider(
|
116 |
+
minimum=0.1,
|
117 |
+
maximum=1.0,
|
118 |
+
value=0.95,
|
119 |
+
step=0.05,
|
120 |
+
label="Top-p",
|
121 |
+
),
|
122 |
+
gr.Slider(
|
123 |
+
minimum=0,
|
124 |
+
maximum=100,
|
125 |
+
value=40,
|
126 |
+
step=1,
|
127 |
+
label="Top-k",
|
128 |
+
),
|
129 |
+
gr.Slider(
|
130 |
+
minimum=0.0,
|
131 |
+
maximum=2.0,
|
132 |
+
value=1.1,
|
133 |
+
step=0.1,
|
134 |
+
label="Repetition penalty",
|
135 |
+
),
|
136 |
+
],
|
137 |
+
retry_btn="Retry",
|
138 |
+
undo_btn="Undo",
|
139 |
+
clear_btn="Clear",
|
140 |
+
submit_btn="Send",
|
141 |
+
title="Chat with Gemma 2 using llama.cpp",
|
142 |
+
description=description,
|
143 |
+
chatbot=gr.Chatbot(
|
144 |
+
scale=1,
|
145 |
+
likeable=False,
|
146 |
+
show_copy_button=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
)
|
148 |
+
)
|
149 |
|
150 |
if __name__ == "__main__":
|
151 |
+
demo.launch()
|