from threading import Thread import requests from io import BytesIO from PIL import Image import re import gradio as gr import torch import spaces from transformers import ( AutoTokenizer, AutoModelForCausalLM, AutoImageProcessor, TextIteratorStreamer, ) tokenizer = AutoTokenizer.from_pretrained("THUDM/glm-edge-v-5b", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("THUDM/glm-edge-v-5b", trust_remote_code=True, device_map="auto").eval() processor = AutoImageProcessor.from_pretrained("THUDM/glm-edge-v-5b", trust_remote_code=True, device_map="auto") def get_image(image): if is_url(image): response = requests.get(image) return Image.open(BytesIO(response.content)).convert("RGB") elif image: return Image.open(image).convert("RGB") def is_url(s): if re.match(r'^(?:http|ftp)s?://', s): return True return False def preprocess_messages(history, image): messages = [] pixel_values = None for idx, (user_msg, model_msg) in enumerate(history): if idx == len(history) - 1 and not messages: messages.append({"role": "user", "content": [{"type": "text", "text": user_msg}]}) break if user_msg: messages.append({"role": "user", "content": [{"type": "text", "text": user_msg}]}) if model_msg: messages.append({"role": "assistant", "content": [{"type": "text", "text": model_msg}]}) if image: messages[-1]['content'].append({"type": "image"}) try: image_input = get_image(image) pixel_values = torch.tensor( processor(image_input).pixel_values).to(model.device) except: print("Invalid image path. Continuing with text conversation.") return messages, pixel_values @spaces.GPU() def predict(history, max_length, top_p, temperature, image=None): messages, pixel_values = preprocess_messages(history, image) model_inputs = tokenizer.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_tensors="pt", return_dict=True ) streamer = TextIteratorStreamer(tokenizer, timeout=60, skip_prompt=True, skip_special_tokens=True) generate_kwargs = { "input_ids": model_inputs["input_ids"].to(model.device), "attention_mask": model_inputs["attention_mask"].to(model.device), "streamer": streamer, "max_new_tokens": max_length, "do_sample": True, "top_p": top_p, "temperature": temperature, "repetition_penalty": 1.2, "eos_token_id": [59246, 59253, 59255], } if image and isinstance(pixel_values, torch.Tensor): generate_kwargs['pixel_values'] = pixel_values t = Thread(target=model.generate, kwargs=generate_kwargs) t.start() for new_token in streamer: if new_token: history[-1][1] += new_token yield history def main(): with gr.Blocks() as demo: gr.HTML("""