Spaces:
Runtime error
Runtime error
zR
commited on
Commit
·
ad14d34
1
Parent(s):
ac9c141
- README.md +11 -7
- app.py +121 -0
- requirements.txt +7 -0
README.md
CHANGED
|
@@ -1,13 +1,17 @@
|
|
| 1 |
---
|
| 2 |
-
title: GLM
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
-
sdk_version: 5.
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
-
short_description: Demo for. THUDM/glm-edge-v-5b
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: GLM-Edge-1.5B-Chat Space
|
| 3 |
+
emoji: 🐨
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: indigo
|
| 6 |
sdk: gradio
|
| 7 |
+
sdk_version: 5.7.0
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
|
|
|
| 10 |
---
|
| 11 |
|
| 12 |
+
## GLM-Edge-1.5B-Chat Space
|
| 13 |
+
|
| 14 |
+
Run with Gradio on GLM-Edge-1.5B-Chat Space.
|
| 15 |
+
```shell
|
| 16 |
+
python app.py
|
| 17 |
+
```
|
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from threading import Thread
|
| 2 |
+
import requests
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import re
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import (
|
| 9 |
+
AutoTokenizer,
|
| 10 |
+
AutoModelForCausalLM,
|
| 11 |
+
AutoImageProcessor,
|
| 12 |
+
TextIteratorStreamer,
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained("THUDM/glm-edge-v-5b", trust_remote_code=True)
|
| 16 |
+
model = AutoModelForCausalLM.from_pretrained("THUDM/glm-edge-v-5b", trust_remote_code=True, device_map="auto").eval()
|
| 17 |
+
processor = AutoImageProcessor.from_pretrained("THUDM/glm-edge-v-5b", trust_remote_code=True, device_map="auto")
|
| 18 |
+
|
| 19 |
+
def get_image(image):
|
| 20 |
+
if is_url(image):
|
| 21 |
+
response = requests.get(image)
|
| 22 |
+
return Image.open(BytesIO(response.content)).convert("RGB")
|
| 23 |
+
elif image:
|
| 24 |
+
return Image.open(image).convert("RGB")
|
| 25 |
+
|
| 26 |
+
def is_url(s):
|
| 27 |
+
if re.match(r'^(?:http|ftp)s?://', s):
|
| 28 |
+
return True
|
| 29 |
+
return False
|
| 30 |
+
|
| 31 |
+
def preprocess_messages(history, image):
|
| 32 |
+
messages = []
|
| 33 |
+
pixel_values = None
|
| 34 |
+
|
| 35 |
+
for idx, (user_msg, model_msg) in enumerate(history):
|
| 36 |
+
if idx == len(history) - 1 and not messages:
|
| 37 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": user_msg}]})
|
| 38 |
+
break
|
| 39 |
+
if user_msg:
|
| 40 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": user_msg}]})
|
| 41 |
+
if model_msg:
|
| 42 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": model_msg}]})
|
| 43 |
+
if image:
|
| 44 |
+
messages[-1]['content'].append({"type": "image"})
|
| 45 |
+
try:
|
| 46 |
+
image_input = get_image(image)
|
| 47 |
+
|
| 48 |
+
pixel_values = torch.tensor(
|
| 49 |
+
processor(image_input).pixel_values).to(model.device)
|
| 50 |
+
except:
|
| 51 |
+
print("Invalid image path. Continuing with text conversation.")
|
| 52 |
+
return messages, pixel_values
|
| 53 |
+
|
| 54 |
+
def predict(history, max_length, top_p, temperature, image=None):
|
| 55 |
+
messages, pixel_values = preprocess_messages(history, image)
|
| 56 |
+
|
| 57 |
+
model_inputs = tokenizer.apply_chat_template(
|
| 58 |
+
messages, add_generation_prompt=True, tokenize=True, return_tensors="pt", return_dict=True
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60, skip_prompt=True, skip_special_tokens=True)
|
| 62 |
+
generate_kwargs = {
|
| 63 |
+
"input_ids": model_inputs["input_ids"].to(model.device),
|
| 64 |
+
"attention_mask": model_inputs["attention_mask"].to(model.device),
|
| 65 |
+
"streamer": streamer,
|
| 66 |
+
"max_new_tokens": max_length,
|
| 67 |
+
"do_sample": True,
|
| 68 |
+
"top_p": top_p,
|
| 69 |
+
"temperature": temperature,
|
| 70 |
+
"repetition_penalty": 1.2,
|
| 71 |
+
"eos_token_id": [59246, 59253, 59255],
|
| 72 |
+
|
| 73 |
+
}
|
| 74 |
+
if image and isinstance(pixel_values, torch.Tensor):
|
| 75 |
+
generate_kwargs['pixel_values'] = pixel_values
|
| 76 |
+
print(generate_kwargs)
|
| 77 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 78 |
+
t.start()
|
| 79 |
+
for new_token in streamer:
|
| 80 |
+
if new_token:
|
| 81 |
+
history[-1][1] += new_token
|
| 82 |
+
yield history
|
| 83 |
+
|
| 84 |
+
def main():
|
| 85 |
+
with gr.Blocks() as demo:
|
| 86 |
+
gr.HTML("""<h1 align="center">GLM-Edge-v Gradio Chat Demo</h1>""")
|
| 87 |
+
|
| 88 |
+
# Top row: Chatbot and Image upload
|
| 89 |
+
with gr.Row():
|
| 90 |
+
with gr.Column(scale=3):
|
| 91 |
+
chatbot = gr.Chatbot()
|
| 92 |
+
with gr.Column(scale=1):
|
| 93 |
+
image_input = gr.Image(label="Upload an Image", type="filepath")
|
| 94 |
+
|
| 95 |
+
# Bottom row: System prompt, user input, and controls
|
| 96 |
+
with gr.Row():
|
| 97 |
+
with gr.Column(scale=2):
|
| 98 |
+
user_input = gr.Textbox(show_label=True, placeholder="Input...", label="User Input")
|
| 99 |
+
submitBtn = gr.Button("Submit")
|
| 100 |
+
emptyBtn = gr.Button("Clear History")
|
| 101 |
+
with gr.Column(scale=1):
|
| 102 |
+
max_length = gr.Slider(0, 8192, value=4096, step=1.0, label="Maximum length", interactive=True)
|
| 103 |
+
top_p = gr.Slider(0, 1, value=0.8, step=0.01, label="Top P", interactive=True)
|
| 104 |
+
temperature = gr.Slider(0.01, 1, value=0.6, step=0.01, label="Temperature", interactive=True)
|
| 105 |
+
|
| 106 |
+
# Define functions for button actions
|
| 107 |
+
def user(query, history):
|
| 108 |
+
return "", history + [[query, ""]]
|
| 109 |
+
|
| 110 |
+
# Button actions and callbacks
|
| 111 |
+
submitBtn.click(user, [user_input, chatbot], [user_input, chatbot], queue=False).then(
|
| 112 |
+
predict, [chatbot, max_length, top_p, temperature, image_input], chatbot
|
| 113 |
+
)
|
| 114 |
+
emptyBtn.click(lambda: (None, None), None, [chatbot], queue=False)
|
| 115 |
+
|
| 116 |
+
demo.queue()
|
| 117 |
+
demo.launch(server_name="127.0.0.1", server_port=7860)
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
git+https://github.com/huggingface/transformers.git
|
| 2 |
+
gradio==5.7.0
|
| 3 |
+
spaces==0.30.4
|
| 4 |
+
accelerate==1.1.0
|
| 5 |
+
Pillow
|
| 6 |
+
requests
|
| 7 |
+
torch
|