import gradio as gr from transformers import AutoModelForCausalLM, AutoProcessor, TextIteratorStreamer from threading import Thread import re import time from PIL import Image import torch import spaces processor = AutoProcessor.from_pretrained("ucsahin/TraVisionLM-base", trust_remote_code=True) model = AutoModelForCausalLM.from_pretrained("ucsahin/TraVisionLM-base", trust_remote_code=True) model.to("cuda:0") @spaces.GPU def bot_streaming(message, history, max_tokens, temperature, top_p, top_k, repetition_penalty): print(max_tokens, temperature, top_p, top_k, repetition_penalty) if message.files: image = message.files[-1].path else: # if there's no image uploaded for this turn, look for images in the past turns for hist in history: if type(hist[0])==tuple: image = hist[0][0] if image is None: gr.Error("Lütfen önce bir resim yükleyin.") prompt = f"{message.text}" image = Image.open(image).convert("RGB") inputs = processor(text=prompt, images=image, return_tensors="pt").to("cuda:0") streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": True}) generation_kwargs = dict( inputs, streamer=streamer, max_new_tokens=max_tokens, do_sample=True, temperature=temperature, top_p=top_p, top_k=top_k, repetition_penalty=repetition_penalty ) generated_text = "" thread = Thread(target=model.generate, kwargs=generation_kwargs) thread.start() text_prompt = f"{message.text}\n" buffer = "" for new_text in streamer: buffer += new_text generated_text_without_prompt = buffer[len(text_prompt):] time.sleep(0.04) yield generated_text_without_prompt gr.set_static_paths(paths=["static/images/"]) logo_path = "static/images/logo-color-v2.png" PLACEHOLDER = f"""
Örnek resim ve soruları kullanabilirsiniz.