almost
Browse files
app.py
CHANGED
|
@@ -15,10 +15,12 @@ model.eval()
|
|
| 15 |
processor = ChameleonProcessor.from_pretrained(model_path)
|
| 16 |
tokenizer = processor.tokenizer
|
| 17 |
|
|
|
|
| 18 |
multimodal_file = tuple[str, str]
|
|
|
|
| 19 |
multimodal_message = list[str | multimodal_file] | multimodal_file
|
| 20 |
# todo: verify this type with gr.ChatInterface
|
| 21 |
-
message_t = str |
|
| 22 |
history_t = list[tuple[str, str] | list[tuple[multimodal_message, multimodal_message]]]
|
| 23 |
|
| 24 |
def history_to_prompt(
|
|
@@ -28,9 +30,10 @@ def history_to_prompt(
|
|
| 28 |
image_placeholder = "<image>"
|
| 29 |
):
|
| 30 |
|
| 31 |
-
prompt = ""
|
| 32 |
-
images = []
|
| 33 |
-
|
|
|
|
| 34 |
print("turn:", turn)
|
| 35 |
# turn should be a tuple of user message and assistant message
|
| 36 |
for message in turn:
|
|
@@ -51,7 +54,7 @@ def history_to_prompt(
|
|
| 51 |
prompt += eot_id
|
| 52 |
return prompt, images
|
| 53 |
|
| 54 |
-
@spaces.GPU(duration=
|
| 55 |
def respond(
|
| 56 |
message,
|
| 57 |
history: history_t,
|
|
@@ -72,7 +75,7 @@ def respond(
|
|
| 72 |
|
| 73 |
inputs = processor(prompt, images=images, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
|
| 74 |
|
| 75 |
-
streamer = TextIteratorStreamer(tokenizer)
|
| 76 |
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=20)
|
| 77 |
|
| 78 |
try:
|
|
|
|
| 15 |
processor = ChameleonProcessor.from_pretrained(model_path)
|
| 16 |
tokenizer = processor.tokenizer
|
| 17 |
|
| 18 |
+
# file_name, alt
|
| 19 |
multimodal_file = tuple[str, str]
|
| 20 |
+
# {'text': 'message here', 'files': []}
|
| 21 |
multimodal_message = list[str | multimodal_file] | multimodal_file
|
| 22 |
# todo: verify this type with gr.ChatInterface
|
| 23 |
+
message_t = dict[str, str | list[multimodal_file]]
|
| 24 |
history_t = list[tuple[str, str] | list[tuple[multimodal_message, multimodal_message]]]
|
| 25 |
|
| 26 |
def history_to_prompt(
|
|
|
|
| 30 |
image_placeholder = "<image>"
|
| 31 |
):
|
| 32 |
|
| 33 |
+
prompt = message["text"]
|
| 34 |
+
images = [Image.open(f) for f in message["files"]]
|
| 35 |
+
|
| 36 |
+
for turn in history:
|
| 37 |
print("turn:", turn)
|
| 38 |
# turn should be a tuple of user message and assistant message
|
| 39 |
for message in turn:
|
|
|
|
| 54 |
prompt += eot_id
|
| 55 |
return prompt, images
|
| 56 |
|
| 57 |
+
@spaces.GPU(duration=30)
|
| 58 |
def respond(
|
| 59 |
message,
|
| 60 |
history: history_t,
|
|
|
|
| 75 |
|
| 76 |
inputs = processor(prompt, images=images, return_tensors="pt").to(model.device, dtype=torch.bfloat16)
|
| 77 |
|
| 78 |
+
streamer = TextIteratorStreamer(tokenizer, skip_prompt=True)
|
| 79 |
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=20)
|
| 80 |
|
| 81 |
try:
|