Gregor commited on
Commit
4f7c605
·
verified ·
1 Parent(s): bda4847

Upload 4 files

Browse files
Files changed (5) hide show
  1. .gitattributes +2 -0
  2. app.py +92 -0
  3. baklava.png +3 -0
  4. bee.jpg +3 -0
  5. requirements.txt +6 -0
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ baklava.png filter=lfs diff=lfs merge=lfs -text
37
+ bee.jpg filter=lfs diff=lfs merge=lfs -text
app.py ADDED
@@ -0,0 +1,92 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import TextIteratorStreamer, AutoModelForCausalLM, AutoProcessor
3
+ from threading import Thread
4
+ import re
5
+ import time
6
+ from PIL import Image
7
+ import torch
8
+ import argparse
9
+ import spaces
10
+
11
+ parser = argparse.ArgumentParser()
12
+ parser.add_argument('--model', type=str, default='aya')
13
+ args = parser.parse_args()
14
+
15
+ model_name = args.model
16
+
17
+ processor = AutoProcessor.from_pretrained(f"WueNLP/centurio_{model_name}", trust_remote_code=True)
18
+ model = AutoModelForCausalLM.from_pretrained(f"WueNLP/centurio_{model_name}",
19
+ trust_remote_code=True,
20
+ torch_dtype=torch.bfloat16,
21
+ low_cpu_mem_usage=True
22
+ ).to("cuda:0")
23
+
24
+ @spaces.GPU
25
+ def bot_streaming(message, history):
26
+ if message["files"]:
27
+ image = message["files"][-1]
28
+ else:
29
+ # if there's no image uploaded for this turn, look for images in the past turns
30
+ # kept inside tuples, take the last one
31
+ for hist in history:
32
+ if type(hist[0]) == tuple:
33
+ image = hist[0][0]
34
+
35
+ if "qwen" in model_name:
36
+ if image is None:
37
+ prompt = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n{message['text']}<|im_end|>\n<|im_start|>assistant\n"
38
+ else:
39
+ image = Image.open(image).convert("RGB")
40
+ prompt = f"<|im_start|>system\nYou are a helpful assistant.<|im_end|>\n<|im_start|>user\n<image_placeholder>\n{message['text']}<|im_end|>\n<|im_start|>assistant\n"
41
+ else:
42
+ if image is None:
43
+ prompt = f"<BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|>{message['text']}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
44
+ else:
45
+ image = Image.open(image).convert("RGB")
46
+ prompt = f"<BOS_TOKEN><|START_OF_TURN_TOKEN|><|USER_TOKEN|><image_placeholder>\n{message['text']}<|END_OF_TURN_TOKEN|><|START_OF_TURN_TOKEN|><|CHATBOT_TOKEN|>"
47
+
48
+ inputs = processor(text=prompt, images=image, return_tensors="pt").to("cuda:0", torch.bfloat16)
49
+
50
+ streamer = TextIteratorStreamer(processor, **{"skip_special_tokens": False})
51
+ generation_kwargs = dict(inputs, streamer=streamer,
52
+ do_sample=True,
53
+ num_beams=1,
54
+ repetition_penalty=1.15,
55
+ temperature=0.7,
56
+ top_p=0.8,
57
+ top_k=20,
58
+ max_new_tokens=512, min_new_tokens=1)
59
+
60
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
61
+ thread.start()
62
+
63
+ buffer = ""
64
+ for new_text in streamer:
65
+ buffer += new_text
66
+ if "qwen" in model_name:
67
+ generated_text_without_prompt = buffer.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0]
68
+ else:
69
+ generated_text_without_prompt = buffer.split("<|CHATBOT_TOKEN|>")[-1].split("<|END_OF_TURN_TOKEN|>")[0]
70
+
71
+ time.sleep(0.04)
72
+ yield generated_text_without_prompt
73
+
74
+
75
+ description = ("""# [Centurio: On Drivers of Multilingual Ability of Large Vision-Language Model](gregor-ge.github.io/Centurio/)
76
+ Try [Centurio](https://huggingface.co/collections/WueNLP/centurio-677cf0ab6ddea874927a154e), a massively multilingual large vision-language model, in this demo (specifically, [Centurio Aya](https://huggingface.co/WueNLP/centurio_aya)).
77
+ Upload an image and start chatting about it, or try one of the examples below.
78
+
79
+ Centurio is trained with 100 languages but quality of answers can differ greatly depending on your language.
80
+ Centurio is trained to read text in images but struggles with small text and with non-Latin scripts.
81
+
82
+ > If you don't upload an image, you will receive an error.
83
+ > This demo does not support multi-image prompts or multi-turn dialog. Every new prompt will refer to the last image (if no new image is included) without prior dialog as context.""")
84
+
85
+ demo = gr.ChatInterface(fn=bot_streaming, title="Centurio Demo",
86
+ examples=[{"text": "What is on the flower?", "files": ["./bee.jpg"]},
87
+ {"text": "How to make this pastry?", "files": ["./baklava.png"]}],
88
+ description=description,
89
+ stop_btn="Stop Generation",
90
+ multimodal=True
91
+ )
92
+ demo.launch(debug=True, share=True)
baklava.png ADDED

Git LFS Details

  • SHA256: 7839e93dd753e5356176bf70d38c43bc56355099d8891ead7aaa342029369268
  • Pointer size: 132 Bytes
  • Size of remote file: 2.04 MB
bee.jpg ADDED

Git LFS Details

  • SHA256: 8b21ba78250f852ca5990063866b1ace6432521d0251bde7f8de783b22c99a6d
  • Pointer size: 132 Bytes
  • Size of remote file: 5.37 MB
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ git+https://github.com/huggingface/transformers.git
3
+ timm
4
+ spaces
5
+ pillow
6
+ accelerate