Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# pip install -U transformers gradio pillow torchvision
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
import gradio as gr
|
| 5 |
+
from transformers import AutoProcessor, AutoModelForImageTextToText
|
| 6 |
+
|
| 7 |
+
# โโ ่ผๅ
ฅๆจกๅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 8 |
+
MODEL_ID = "google/gemma-4-12B"
|
| 9 |
+
|
| 10 |
+
print(f"่ผๅ
ฅๆจกๅ๏ผ{MODEL_ID} ...")
|
| 11 |
+
processor = AutoProcessor.from_pretrained(MODEL_ID)
|
| 12 |
+
model = AutoModelForImageTextToText.from_pretrained(
|
| 13 |
+
MODEL_ID,
|
| 14 |
+
dtype=torch.float16, # torch_dtype ๅทฒๆฃ็จ๏ผๆน็จ dtype
|
| 15 |
+
device_map="auto",
|
| 16 |
+
)
|
| 17 |
+
model.eval()
|
| 18 |
+
print("ๆจกๅ่ผๅ
ฅๅฎๆ๏ผ")
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# โโ ๆจ่ซๅฝๅผ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 22 |
+
def build_messages(history: list, user_text: str, image=None) -> list:
|
| 23 |
+
"""ๅฐ Gradio tuples ๆญทๅฒ่ฝๆๆ HuggingFace messages ๆ ผๅผใ"""
|
| 24 |
+
messages = []
|
| 25 |
+
for user_turn, assistant_turn in history:
|
| 26 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": user_turn or ""}]})
|
| 27 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": assistant_turn or ""}]})
|
| 28 |
+
|
| 29 |
+
user_content = []
|
| 30 |
+
if image is not None:
|
| 31 |
+
user_content.append({"type": "image", "image": image})
|
| 32 |
+
user_content.append({"type": "text", "text": user_text})
|
| 33 |
+
messages.append({"role": "user", "content": user_content})
|
| 34 |
+
return messages
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def chat(user_message, image, history, max_new_tokens, temperature, top_p):
|
| 38 |
+
"""ไธปๅฐ่ฉฑๅฝๅผใhistory ็บ list of [user, assistant] tuplesใ"""
|
| 39 |
+
if not (user_message and user_message.strip()) and image is None:
|
| 40 |
+
return history, history, "", None
|
| 41 |
+
|
| 42 |
+
messages = build_messages(history, user_message, image)
|
| 43 |
+
|
| 44 |
+
pil_images = [image] if image is not None else None
|
| 45 |
+
inputs = processor.apply_chat_template(
|
| 46 |
+
messages,
|
| 47 |
+
add_generation_prompt=True,
|
| 48 |
+
tokenize=True,
|
| 49 |
+
return_tensors="pt",
|
| 50 |
+
return_dict=True,
|
| 51 |
+
images=pil_images,
|
| 52 |
+
).to(model.device, dtype=torch.float16)
|
| 53 |
+
|
| 54 |
+
with torch.inference_mode():
|
| 55 |
+
output_ids = model.generate(
|
| 56 |
+
**inputs,
|
| 57 |
+
max_new_tokens=int(max_new_tokens),
|
| 58 |
+
do_sample=temperature > 0,
|
| 59 |
+
temperature=float(temperature) if temperature > 0 else 1.0,
|
| 60 |
+
top_p=float(top_p),
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
input_len = inputs["input_ids"].shape[-1]
|
| 64 |
+
response = processor.decode(
|
| 65 |
+
output_ids[0, input_len:], skip_special_tokens=True
|
| 66 |
+
).strip()
|
| 67 |
+
|
| 68 |
+
history = history + [[user_message, response]]
|
| 69 |
+
return history, history, "", None
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def clear_history():
|
| 73 |
+
return [], [], "", None
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
# โโ Gradio UI๏ผGradio 6 ็ธๅฎน๏ผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 77 |
+
CSS = """
|
| 78 |
+
#chatbot { height: 550px; }
|
| 79 |
+
.send-btn { background: #10b981 !important; color: white !important; }
|
| 80 |
+
footer { display: none !important; }
|
| 81 |
+
"""
|
| 82 |
+
|
| 83 |
+
with gr.Blocks(title="Gemma-4 Chat") as demo:
|
| 84 |
+
gr.Markdown(
|
| 85 |
+
"""
|
| 86 |
+
# ๐ค Gemma-4 ๅคๆจกๆ
ๅฐ่ฉฑๅฉ็
|
| 87 |
+
ๆฏๆด็ดๆๅญๅฐ่ฉฑ๏ผไบฆๅฏไธๅณๅ็้ฒ่กๅๆๅ็ญใ
|
| 88 |
+
"""
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
state = gr.State([])
|
| 92 |
+
|
| 93 |
+
with gr.Row():
|
| 94 |
+
with gr.Column(scale=3):
|
| 95 |
+
chatbot = gr.Chatbot(
|
| 96 |
+
elem_id="chatbot",
|
| 97 |
+
label="ๅฐ่ฉฑ่ฆ็ช",
|
| 98 |
+
# Gradio 6 ไธๆฏๆด type="messages"๏ผไฝฟ็จ้ ่จญ tuples ๆ ผๅผ
|
| 99 |
+
)
|
| 100 |
+
with gr.Row():
|
| 101 |
+
user_input = gr.Textbox(
|
| 102 |
+
placeholder="่ผธๅ
ฅ่จๆฏ๏ผๆ Enter ๆ้ปๆ้ๅบโฆ",
|
| 103 |
+
show_label=False,
|
| 104 |
+
lines=2,
|
| 105 |
+
scale=5,
|
| 106 |
+
)
|
| 107 |
+
send_btn = gr.Button("้ๅบ โถ", elem_classes="send-btn", scale=1)
|
| 108 |
+
|
| 109 |
+
with gr.Column(scale=1):
|
| 110 |
+
image_input = gr.Image(
|
| 111 |
+
label="ไธๅณๅ็๏ผ้ธๅกซ๏ผ",
|
| 112 |
+
type="pil",
|
| 113 |
+
height=220,
|
| 114 |
+
)
|
| 115 |
+
gr.Markdown("### โ๏ธ ็ๆๅๆธ")
|
| 116 |
+
max_new_tokens = gr.Slider(64, 2048, value=512, step=64, label="ๆๅคง็ๆ้ทๅบฆ")
|
| 117 |
+
temperature = gr.Slider(0.0, 2.0, value=0.7, step=0.05, label="Temperature๏ผ0 = ็ขบๅฎๆง๏ผ")
|
| 118 |
+
top_p = gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p")
|
| 119 |
+
clear_btn = gr.Button("๐๏ธ ๆธ
้คๅฐ่ฉฑ", variant="secondary")
|
| 120 |
+
|
| 121 |
+
send_inputs = [user_input, image_input, state, max_new_tokens, temperature, top_p]
|
| 122 |
+
send_outputs = [chatbot, state, user_input, image_input]
|
| 123 |
+
|
| 124 |
+
send_btn.click(chat, inputs=send_inputs, outputs=send_outputs)
|
| 125 |
+
user_input.submit(chat, inputs=send_inputs, outputs=send_outputs)
|
| 126 |
+
clear_btn.click(clear_history, outputs=[chatbot, state, user_input, image_input])
|
| 127 |
+
|
| 128 |
+
# โโ ๅๅ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
|
| 129 |
+
if __name__ == "__main__":
|
| 130 |
+
demo.launch(
|
| 131 |
+
server_name="0.0.0.0",
|
| 132 |
+
server_port=7860,
|
| 133 |
+
share=False, # Colab ่ซๆน True
|
| 134 |
+
inbrowser=True,
|
| 135 |
+
theme=gr.themes.Soft(primary_hue="emerald"), # Gradio 6: theme ็งปๅฐ launch()
|
| 136 |
+
css=CSS,
|
| 137 |
+
)
|