Sandra1165 commited on
Commit
45c93b1
ยท
verified ยท
1 Parent(s): a113d89

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +137 -0
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
+ )