prithivMLmods commited on
Commit
46e9c9e
Β·
verified Β·
1 Parent(s): 0906918

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +143 -224
app.py CHANGED
@@ -1,232 +1,151 @@
1
- import gradio as gr
2
  import spaces
3
- import torch
4
- from PIL import Image
5
- from diffusers import DiffusionPipeline
6
- import random
7
- import uuid
8
- from typing import Tuple
9
- import numpy as np
10
-
11
- def save_image(img):
12
- unique_name = str(uuid.uuid4()) + ".png"
13
- img.save(unique_name)
14
- return unique_name
15
-
16
- def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
17
- if randomize_seed:
18
- seed = random.randint(0, MAX_SEED)
19
- return seed
20
-
21
- MAX_SEED = np.iinfo(np.int32).max
22
-
23
- if not torch.cuda.is_available():
24
- DESCRIPTIONz += "\n<p>⚠️Running on CPU, This may not work on CPU.</p>"
25
-
26
- base_model = "black-forest-labs/FLUX.1-dev"
27
- pipe = DiffusionPipeline.from_pretrained(base_model, torch_dtype=torch.bfloat16)
28
-
29
- LORA_OPTIONS = {
30
- "Sketch-Smudge": {
31
- "lora_repo": "strangerzonehf/Flux-Sketch-Smudge-LoRA",
32
- "trigger_word": "Sketch Smudge"
33
- },
34
- "Sketch-Sized": {
35
- "lora_repo": "strangerzonehf/Flux-Sketch-Sized-LoRA",
36
- "trigger_word": "Sketch Sized"
37
- }
38
- }
39
-
40
- style_list = [
41
- {
42
- "name": "3840 x 2160",
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
- seed = int(randomize_seed_fn(seed, randomize_seed))
80
 
81
- positive_prompt = apply_style(style_name, prompt)
 
82
 
83
- # Load the selected LoRA weights
84
- lora_repo = LORA_OPTIONS[lora_choice]["lora_repo"]
85
- trigger_word = LORA_OPTIONS[lora_choice]["trigger_word"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
 
87
- # Load LoRA weights and move the entire pipeline to GPU
88
- pipe.load_lora_weights(lora_repo)
89
- pipe.to("cuda") # Move the entire pipeline to GPU
90
-
91
- if trigger_word:
92
- positive_prompt = f"{trigger_word} {positive_prompt}"
93
-
94
- images = pipe(
95
- prompt=positive_prompt,
96
- width=width,
97
- height=height,
98
- guidance_scale=guidance_scale,
99
- num_inference_steps=30,
100
- num_images_per_prompt=1,
101
- output_type="pil",
102
- ).images
103
- image_paths = [save_image(img) for img in images]
104
- print(image_paths)
105
- return image_paths, seed
106
-
107
- examples = [
108
- "midjourney mix, a tiny astronaut hatching from an egg on the moon",
109
- "midjourney mix, intense Red, a black cat is facing the left side of the frame. The cats head is tilted upward, with its eyes closed. Its whiskers are protruding from its mouth, adding a touch of warmth to the scene. The background is a vibrant red, creating a striking contrast with the cats fur.",
110
- "midjourney mix, a close-up shot of a womans face, the womans hair is wet, and she is wearing a cream-colored sweater. The background is blurred, and there are red and white signs visible in the background. The womans eyebrows are wet, adding a touch of color to her face. Her lips are a vibrant shade of pink, and her eyes are a darker shade of brown.",
111
- "midjourney mix, woman in a red jacket, snowy, in the style of hyper-realistic portraiture, caninecore, mountainous vistas, timeless beauty, palewave, iconic, distinctive noses --ar 72:101 --stylize 750 --v 6",
112
- "midjourney mix, an anime-style illustration of a delicious, golden-brown wiener schnitzel on a plate, served with fresh lemon slices, parsley --style raw5"
113
- ]
114
-
115
- css = '''
116
- .gradio-container{max-width: 888px !important}
117
- h1{text-align:center}
118
- footer {
119
- visibility: hidden
120
- }
121
- .submit-btn {
122
- background-color: #d73333 !important;
123
- color: white !important;
124
- }
125
- .submit-btn:hover {
126
- background-color: #ff0000 !important;
127
- }
128
- '''
129
-
130
- with gr.Blocks(css=css, theme="bethecloud/storj_theme") as demo:
131
- with gr.Row():
132
- with gr.Column(scale=1):
133
- prompt = gr.Text(
134
- label="Prompt",
135
- show_label=False,
136
- max_lines=1,
137
- placeholder="Enter your prompt",
138
- container=False,
139
- )
140
- run_button = gr.Button("Generate as : ( 1280 x 832 )πŸ€—", scale=0, elem_classes="submit-btn")
141
-
142
- with gr.Accordion("Advanced options", open=True, visible=True):
143
- seed = gr.Slider(
144
- label="Seed",
145
- minimum=0,
146
- maximum=MAX_SEED,
147
- step=1,
148
- value=0,
149
- visible=True
150
- )
151
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
152
-
153
- with gr.Row(visible=True):
154
- width = gr.Slider(
155
- label="Width",
156
- minimum=512,
157
- maximum=2048,
158
- step=64,
159
- value=1280,
160
- )
161
- height = gr.Slider(
162
- label="Height",
163
- minimum=512,
164
- maximum=2048,
165
- step=64,
166
- value=832,
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.queue(max_size=40).launch(share=True) # Set share=True to create a public link
 
 
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()