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|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import spaces
|
| 3 |
+
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load model and tokenizer
|
| 8 |
+
model_name = "mistral-community/pixtral-12b-240910"
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_name,
|
| 12 |
+
torch_dtype=torch.float16,
|
| 13 |
+
device_map="auto"
|
| 14 |
+
)
|
| 15 |
+
|
| 16 |
+
@spaces.GPU(duration=120)
|
| 17 |
+
def generate_response(image, prompt, max_length, temperature):
|
| 18 |
+
messages = [
|
| 19 |
+
{"role": "system", "content": "You are a helpful assistant that can analyze images and text."},
|
| 20 |
+
{"role": "user", "content": prompt}
|
| 21 |
+
]
|
| 22 |
+
formatted_prompt = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
|
| 23 |
+
|
| 24 |
+
# Preprocess the image
|
| 25 |
+
if image is not None:
|
| 26 |
+
image = Image.open(image).convert("RGB")
|
| 27 |
+
inputs = tokenizer(formatted_prompt, images=[image], return_tensors="pt", padding=True).to(model.device)
|
| 28 |
+
else:
|
| 29 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt", padding=True).to(model.device)
|
| 30 |
+
|
| 31 |
+
# Generate
|
| 32 |
+
with torch.no_grad():
|
| 33 |
+
outputs = model.generate(
|
| 34 |
+
**inputs,
|
| 35 |
+
max_new_tokens=max_length,
|
| 36 |
+
do_sample=True,
|
| 37 |
+
temperature=temperature,
|
| 38 |
+
top_k=100,
|
| 39 |
+
top_p=0.95,
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
# Decode and return the response
|
| 43 |
+
response = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
|
| 44 |
+
return response
|
| 45 |
+
|
| 46 |
+
# Custom CSS
|
| 47 |
+
css = """
|
| 48 |
+
body {
|
| 49 |
+
background-color: #1a1a2e;
|
| 50 |
+
color: #e0e0e0;
|
| 51 |
+
font-family: 'Arial', sans-serif;
|
| 52 |
+
}
|
| 53 |
+
.container {
|
| 54 |
+
max-width: 900px;
|
| 55 |
+
margin: auto;
|
| 56 |
+
padding: 20px;
|
| 57 |
+
}
|
| 58 |
+
.gradio-container {
|
| 59 |
+
background-color: #16213e;
|
| 60 |
+
border-radius: 15px;
|
| 61 |
+
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
| 62 |
+
}
|
| 63 |
+
.header {
|
| 64 |
+
background-color: #0f3460;
|
| 65 |
+
padding: 20px;
|
| 66 |
+
border-radius: 15px 15px 0 0;
|
| 67 |
+
text-align: center;
|
| 68 |
+
margin-bottom: 20px;
|
| 69 |
+
}
|
| 70 |
+
.header h1 {
|
| 71 |
+
color: #e94560;
|
| 72 |
+
font-size: 2.5em;
|
| 73 |
+
margin-bottom: 10px;
|
| 74 |
+
}
|
| 75 |
+
.header p {
|
| 76 |
+
color: #a0a0a0;
|
| 77 |
+
}
|
| 78 |
+
.input-group, .output-group {
|
| 79 |
+
background-color: #1a1a2e;
|
| 80 |
+
padding: 20px;
|
| 81 |
+
border-radius: 10px;
|
| 82 |
+
margin-bottom: 20px;
|
| 83 |
+
}
|
| 84 |
+
.input-group label, .output-group label {
|
| 85 |
+
color: #e94560;
|
| 86 |
+
font-weight: bold;
|
| 87 |
+
}
|
| 88 |
+
.generate-btn {
|
| 89 |
+
background-color: #e94560 !important;
|
| 90 |
+
color: white !important;
|
| 91 |
+
border: none !important;
|
| 92 |
+
border-radius: 5px !important;
|
| 93 |
+
padding: 10px 20px !important;
|
| 94 |
+
font-size: 16px !important;
|
| 95 |
+
cursor: pointer !important;
|
| 96 |
+
transition: background-color 0.3s ease !important;
|
| 97 |
+
}
|
| 98 |
+
.generate-btn:hover {
|
| 99 |
+
background-color: #c81e45 !important;
|
| 100 |
+
}
|
| 101 |
+
.example-prompts {
|
| 102 |
+
background-color: #1f2b47;
|
| 103 |
+
padding: 15px;
|
| 104 |
+
border-radius: 10px;
|
| 105 |
+
margin-bottom: 20px;
|
| 106 |
+
}
|
| 107 |
+
.example-prompts h3 {
|
| 108 |
+
color: #e94560;
|
| 109 |
+
margin-bottom: 10px;
|
| 110 |
+
}
|
| 111 |
+
.example-prompts ul {
|
| 112 |
+
list-style-type: none;
|
| 113 |
+
padding-left: 0;
|
| 114 |
+
}
|
| 115 |
+
.example-prompts li {
|
| 116 |
+
margin-bottom: 5px;
|
| 117 |
+
cursor: pointer;
|
| 118 |
+
transition: color 0.3s ease;
|
| 119 |
+
}
|
| 120 |
+
.example-prompts li:hover {
|
| 121 |
+
color: #e94560;
|
| 122 |
+
}
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
# Example prompts
|
| 126 |
+
example_prompts = [
|
| 127 |
+
"Describe this image in detail.",
|
| 128 |
+
"What emotions does this image evoke?",
|
| 129 |
+
"Imagine a story based on this image.",
|
| 130 |
+
"What technical aspects of photography are demonstrated in this image?",
|
| 131 |
+
"How might this image be used in advertising?"
|
| 132 |
+
]
|
| 133 |
+
|
| 134 |
+
# Gradio interface
|
| 135 |
+
with gr.Blocks(css=css) as iface:
|
| 136 |
+
gr.HTML(
|
| 137 |
+
"""
|
| 138 |
+
<div class="header">
|
| 139 |
+
<h1>Pixtral-12B Multimodal Generation</h1>
|
| 140 |
+
<p>Generate text responses based on images and prompts using the powerful Pixtral-12B model.</p>
|
| 141 |
+
</div>
|
| 142 |
+
"""
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
with gr.Group():
|
| 146 |
+
with gr.Group(elem_classes="example-prompts"):
|
| 147 |
+
gr.HTML("<h3>Example Prompts:</h3>")
|
| 148 |
+
example_buttons = [gr.Button(prompt) for prompt in example_prompts]
|
| 149 |
+
|
| 150 |
+
with gr.Group(elem_classes="input-group"):
|
| 151 |
+
image_input = gr.Image(type="filepath", label="Upload an image (optional)")
|
| 152 |
+
prompt = gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=5)
|
| 153 |
+
max_length = gr.Slider(minimum=1, maximum=500, value=128, step=1, label="Max Length")
|
| 154 |
+
temperature = gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature")
|
| 155 |
+
generate_btn = gr.Button("Generate", elem_classes="generate-btn")
|
| 156 |
+
|
| 157 |
+
with gr.Group(elem_classes="output-group"):
|
| 158 |
+
output = gr.Textbox(label="Generated Text", lines=10)
|
| 159 |
+
|
| 160 |
+
generate_btn.click(generate_response, inputs=[image_input, prompt, max_length, temperature], outputs=output)
|
| 161 |
+
|
| 162 |
+
# Set up example prompt buttons
|
| 163 |
+
for button in example_buttons:
|
| 164 |
+
button.click(lambda x: x, inputs=[button], outputs=[prompt])
|
| 165 |
+
|
| 166 |
+
# Launch the app
|
| 167 |
+
iface.launch()
|