gokaygokay commited on
Commit
e47a249
1 Parent(s): d5115de

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +138 -135
app.py CHANGED
@@ -1,146 +1,149 @@
1
  import spaces
2
- from llama_cpp import Llama
3
- from llama_cpp_agent import LlamaCppAgent, MessagesFormatterType
4
- from llama_cpp_agent.providers import LlamaCppPythonProvider
5
- from llama_cpp_agent.chat_history import BasicChatHistory
6
- from llama_cpp_agent.chat_history.messages import Roles
7
  import gradio as gr
8
- from huggingface_hub import hf_hub_download
9
-
10
- hf_hub_download(
11
- repo_id="MaziyarPanahi/Mistral-Nemo-Instruct-2407-GGUF",
12
- filename="Mistral-Nemo-Instruct-2407.Q5_K_M.gguf",
13
- local_dir="./models"
14
- )
15
-
16
-
17
- llm = None
18
- llm_model = None
19
-
20
- @spaces.GPU(duration=120)
21
- def respond(
22
- message,
23
- history: list[tuple[str, str]],
24
- model,
25
- system_message,
26
- max_tokens,
27
- temperature,
28
- top_p,
29
- top_k,
30
- repeat_penalty,
31
- ):
32
-
33
-
34
- chat_template = MessagesFormatterType.MISTRAL
35
-
36
- global llm
37
- global llm_model
38
-
39
- if llm is None or llm_model != model:
40
- llm = Llama(
41
- model_path=f"models/{model}",
42
- flash_attn=True,
43
- n_gpu_layers=81,
44
- n_batch=1024,
45
- n_ctx=32768,
46
- )
47
- llm_model = model
48
-
49
- provider = LlamaCppPythonProvider(llm)
50
-
51
- agent = LlamaCppAgent(
52
- provider,
53
- system_prompt=f"{system_message}",
54
- predefined_messages_formatter_type=chat_template,
55
- debug_output=True
56
  )
57
-
58
- settings = provider.get_provider_default_settings()
59
- settings.temperature = temperature
60
- settings.top_k = top_k
61
- settings.top_p = top_p
62
- settings.max_tokens = max_tokens
63
- settings.repeat_penalty = repeat_penalty
64
- settings.stream = True
65
-
66
- messages = BasicChatHistory()
67
-
68
- for msn in history:
69
- user = {
70
- 'role': Roles.user,
71
- 'content': msn[0]
72
- }
73
- assistant = {
74
- 'role': Roles.assistant,
75
- 'content': msn[1]
76
- }
77
- messages.add_message(user)
78
- messages.add_message(assistant)
79
-
80
- stream = agent.get_chat_response(
81
- message,
82
- llm_sampling_settings=settings,
83
- chat_history=messages,
84
- returns_streaming_generator=True,
85
- print_output=False
86
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- outputs = ""
89
- for output in stream:
90
- outputs += output
91
- yield outputs
92
-
93
- description = """<p><center>
94
- <a href="https://huggingface.co/mistralai/Mistral-Nemo-Instruct-2407" target="_blank">[Instruct Model]</a>
95
- <a href="https://huggingface.co/MaziyarPanahi/Mistral-Nemo-Instruct-2407-GGUF" target="_blank">[GGUF Version]</a>
96
  </center></p>
97
  """
98
 
99
- demo = gr.ChatInterface(
100
- respond,
101
- additional_inputs=[
102
- gr.Dropdown([
103
- 'Mistral-Nemo-Instruct-2407.Q5_K_M.gguf',
104
- ],
105
- value="Mistral-Nemo-Instruct-2407.Q5_K_M.gguf",
106
- label="Model"
107
- ),
108
- gr.Textbox(value="You are a helpful assistant.", label="System message"),
109
- gr.Slider(minimum=1, maximum=4096, value=2048, step=1, label="Max tokens"),
110
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
111
- gr.Slider(
112
- minimum=0.1,
113
- maximum=1.0,
114
- value=0.95,
115
- step=0.05,
116
- label="Top-p",
117
- ),
118
- gr.Slider(
119
- minimum=0,
120
- maximum=100,
121
- value=40,
122
- step=1,
123
- label="Top-k",
124
- ),
125
- gr.Slider(
126
- minimum=0.0,
127
- maximum=2.0,
128
- value=1.1,
129
- step=0.1,
130
- label="Repetition penalty",
131
- ),
132
- ],
133
- retry_btn="Retry",
134
- undo_btn="Undo",
135
- clear_btn="Clear",
136
- submit_btn="Send",
137
- title="Chat with Mistral-Nemo using llama.cpp",
138
- description=description,
139
- chatbot=gr.Chatbot(
140
- scale=1,
141
- likeable=False,
142
- show_copy_button=True
143
- )
144
  )
145
 
146
  demo.launch(debug=True)
 
1
  import spaces
 
 
 
 
 
2
  import gradio as gr
3
+ import torch
4
+ from PIL import Image
5
+ from transformers import AutoProcessor, AutoModelForCausalLM, pipeline
6
+ from diffusers import DiffusionPipeline
7
+ import random
8
+ import numpy as np
9
+ import os
10
+ import subprocess
11
+
12
+ subprocess.run('pip install flash-attn --no-build-isolation', env={'FLASH_ATTENTION_SKIP_CUDA_BUILD': "TRUE"}, shell=True)
13
+
14
+ # Initialize models
15
+ device = "cuda" if torch.cuda.is_available() else "cpu"
16
+ dtype = torch.bfloat16
17
+
18
+ huggingface_token = os.getenv("HUGGINGFACE_TOKEN")
19
+
20
+ # FLUX.1-schnell model
21
+ pipe = DiffusionPipeline.from_pretrained("black-forest-labs/FLUX.1-schnell", torch_dtype=dtype, revision="refs/pr/1", token=huggingface_token).to(device)
22
+
23
+ # Initialize Florence model
24
+ florence_model = AutoModelForCausalLM.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True).to(device).eval()
25
+ florence_processor = AutoProcessor.from_pretrained('microsoft/Florence-2-base', trust_remote_code=True)
26
+
27
+ # Prompt Enhancer
28
+ enhancer_long = pipeline("summarization", model="gokaygokay/Lamini-Prompt-Enchance-Long", device=device)
29
+
30
+ MAX_SEED = np.iinfo(np.int32).max
31
+ MAX_IMAGE_SIZE = 2048
32
+
33
+ # Florence caption function
34
+ def florence_caption(image):
35
+ if not isinstance(image, Image.Image):
36
+ image = Image.fromarray(image)
37
+
38
+ inputs = florence_processor(text="<MORE_DETAILED_CAPTION>", images=image, return_tensors="pt").to(device)
39
+ generated_ids = florence_model.generate(
40
+ input_ids=inputs["input_ids"],
41
+ pixel_values=inputs["pixel_values"],
42
+ max_new_tokens=1024,
43
+ early_stopping=False,
44
+ do_sample=False,
45
+ num_beams=3,
 
 
 
 
 
46
  )
47
+ generated_text = florence_processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
48
+ parsed_answer = florence_processor.post_process_generation(
49
+ generated_text,
50
+ task="<MORE_DETAILED_CAPTION>",
51
+ image_size=(image.width, image.height)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  )
53
+ return parsed_answer["<MORE_DETAILED_CAPTION>"]
54
+
55
+ # Prompt Enhancer function
56
+ def enhance_prompt(input_prompt):
57
+ result = enhancer_long("Enhance the description: " + input_prompt)
58
+ enhanced_text = result[0]['summary_text']
59
+ return enhanced_text
60
+
61
+ @spaces.GPU(duration=190)
62
+ def process_workflow(image, text_prompt, use_enhancer, seed, randomize_seed, width, height, num_inference_steps, progress=gr.Progress(track_tqdm=True)):
63
+ if image is not None:
64
+ if not isinstance(image, Image.Image):
65
+ image = Image.fromarray(image)
66
+ prompt = florence_caption(image)
67
+ else:
68
+ prompt = text_prompt
69
+
70
+ if use_enhancer:
71
+ prompt = enhance_prompt(prompt)
72
+
73
+ if randomize_seed:
74
+ seed = random.randint(0, MAX_SEED)
75
+
76
+ generator = torch.Generator(device=device).manual_seed(seed)
77
+
78
+ image = pipe(
79
+ prompt=prompt,
80
+ generator=generator,
81
+ num_inference_steps=num_inference_steps,
82
+ width=width,
83
+ height=height,
84
+ guidance_scale=0.0
85
+ ).images[0]
86
+
87
+ return image, prompt, seed
88
+
89
+ custom_css = """
90
+ .input-group, .output-group {
91
+ border: 1px solid #e0e0e0;
92
+ border-radius: 10px;
93
+ padding: 20px;
94
+ margin-bottom: 20px;
95
+ background-color: #f9f9f9;
96
+ }
97
+ .submit-btn {
98
+ background-color: #2980b9 !important;
99
+ color: white !important;
100
+ }
101
+ .submit-btn:hover {
102
+ background-color: #3498db !important;
103
+ }
104
+ """
105
 
106
+ title = """<h1 align="center">FLUX.1-schnell with Florence-2 Captioner and Prompt Enhancer</h1>
107
+ <p><center>
108
+ <a href="https://huggingface.co/black-forest-labs/FLUX.1-schnell" target="_blank">[FLUX.1-schnell Model]</a>
109
+ <a href="https://huggingface.co/microsoft/Florence-2-base" target="_blank">[Florence-2 Model]</a>
110
+ <a href="https://huggingface.co/gokaygokay/Lamini-Prompt-Enchance-Long" target="_blank">[Prompt Enhancer Long]</a>
111
+ <p align="center">Create long prompts from images or enhance your short prompts with prompt enhancer</p>
 
 
112
  </center></p>
113
  """
114
 
115
+ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(primary_hue="blue", secondary_hue="gray")) as demo:
116
+ gr.HTML(title)
117
+
118
+ with gr.Row():
119
+ with gr.Column(scale=1):
120
+ with gr.Group(elem_classes="input-group"):
121
+ input_image = gr.Image(label="Input Image (Florence-2 Captioner)")
122
+
123
+ with gr.Accordion("Advanced Settings", open=False):
124
+ text_prompt = gr.Textbox(label="Text Prompt (optional, used if no image is uploaded)")
125
+ use_enhancer = gr.Checkbox(label="Use Prompt Enhancer", value=False)
126
+ seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=0)
127
+ randomize_seed = gr.Checkbox(label="Randomize Seed", value=True)
128
+ width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
129
+ height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
130
+ num_inference_steps = gr.Slider(label="Inference Steps", minimum=1, maximum=50, step=1, value=4)
131
+
132
+ generate_btn = gr.Button("Generate Image", elem_classes="submit-btn")
133
+
134
+ with gr.Column(scale=1):
135
+ with gr.Group(elem_classes="output-group"):
136
+ output_image = gr.Image(label="Result", elem_id="gallery", show_label=False)
137
+ final_prompt = gr.Textbox(label="Final Prompt Used")
138
+ used_seed = gr.Number(label="Seed Used")
139
+
140
+ generate_btn.click(
141
+ fn=process_workflow,
142
+ inputs=[
143
+ input_image, text_prompt, use_enhancer, seed, randomize_seed,
144
+ width, height, num_inference_steps
145
+ ],
146
+ outputs=[output_image, final_prompt, used_seed]
 
 
 
 
 
 
 
 
 
 
 
 
 
147
  )
148
 
149
  demo.launch(debug=True)