Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,5 @@
|
|
| 1 |
-
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
|
| 7 |
|
| 8 |
-
|
| 9 |
-
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
|
| 10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
-
pretrained_model_name_or_path=MODEL_PATH,
|
| 12 |
-
torch_dtype=torch.bfloat16,
|
| 13 |
-
device_map="auto",
|
| 14 |
-
trust_remote_code=True # Required for many GLM models
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
def chat_with_glm(message, history):
|
| 18 |
-
# 2. Format history for the chat template
|
| 19 |
-
# Gradio history is a list of [user_msg, assistant_msg]
|
| 20 |
-
messages = []
|
| 21 |
-
for h in history:
|
| 22 |
-
messages.append({"role": "user", "content": h[0]})
|
| 23 |
-
messages.append({"role": "assistant", "content": h[1]})
|
| 24 |
-
|
| 25 |
-
# Add current message
|
| 26 |
-
messages.append({"role": "user", "content": message})
|
| 27 |
-
|
| 28 |
-
# 3. Apply the ChatML-style template
|
| 29 |
-
inputs = tokenizer.apply_chat_template(
|
| 30 |
-
messages,
|
| 31 |
-
tokenize=True,
|
| 32 |
-
add_generation_prompt=True,
|
| 33 |
-
return_dict=True,
|
| 34 |
-
return_tensors="pt"
|
| 35 |
-
).to(model.device)
|
| 36 |
-
|
| 37 |
-
# 4. Generate the response
|
| 38 |
-
generated_ids = model.generate(
|
| 39 |
-
**inputs,
|
| 40 |
-
max_new_tokens=512,
|
| 41 |
-
do_sample=True,
|
| 42 |
-
top_p=0.9,
|
| 43 |
-
temperature=0.7
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
# 5. Decode and remove the prompt tokens
|
| 47 |
-
response = tokenizer.decode(
|
| 48 |
-
generated_ids[0][inputs.input_ids.shape[1]:],
|
| 49 |
-
skip_special_tokens=True
|
| 50 |
-
)
|
| 51 |
-
|
| 52 |
-
return response
|
| 53 |
-
|
| 54 |
-
# 6. Launch the interface
|
| 55 |
-
demo = gr.ChatInterface(
|
| 56 |
-
fn=chat_with_glm,
|
| 57 |
-
title="GLM-4.7-Flash Chatbot",
|
| 58 |
-
description="Ask anything to the 30B-A3B MoE model from Z.ai.",
|
| 59 |
-
examples=["Hello!", "Explain Mixture of Experts models.", "Write a Python script for a simple timer."]
|
| 60 |
-
)
|
| 61 |
-
|
| 62 |
-
if __name__ == "__main__":
|
| 63 |
-
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
demo = gr.load("Tongyi-MAI/Z-Image-Turbo", src="models")
|
| 4 |
|
| 5 |
+
demo.launch(css = """footer {visibility: hidden;}""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|