Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,130 +1,118 @@
|
|
| 1 |
-
import sys
|
| 2 |
-
from threading import Thread
|
| 3 |
-
|
| 4 |
import gradio as gr
|
| 5 |
import torch
|
|
|
|
| 6 |
from transformers import AutoModel, AutoProcessor
|
| 7 |
-
from transformers import StoppingCriteria, TextIteratorStreamer, StoppingCriteriaList
|
| 8 |
|
|
|
|
| 9 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
def
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
messages.append({"role": "user", "content": message})
|
| 36 |
-
|
| 37 |
-
model_inputs = processor.tokenizer.apply_chat_template(
|
| 38 |
-
messages,
|
| 39 |
-
add_generation_prompt=True,
|
| 40 |
return_tensors="pt"
|
| 41 |
-
)
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
)
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
-
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 68 |
-
t.start()
|
| 69 |
-
|
| 70 |
-
history.append([message, ""])
|
| 71 |
-
partial_response = ""
|
| 72 |
-
for new_token in streamer:
|
| 73 |
-
partial_response += new_token
|
| 74 |
-
history[-1][1] = partial_response
|
| 75 |
-
yield history, gr.Button(visible=False), gr.Button(visible=True, interactive=True)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
|
|
|
| 78 |
with gr.Blocks() as demo:
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
gr.Examples(
|
| 102 |
-
[
|
| 103 |
-
["images/scream.jpg", "What is the main emotion of this image?"],
|
| 104 |
-
["images/louvre.jpg", "Where is this landmark located?"],
|
| 105 |
-
["images/three_people.jpg", "What are these people doing?"]
|
| 106 |
-
],
|
| 107 |
-
[image, message],
|
| 108 |
-
label="VQA"
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
response_handler = (
|
| 112 |
-
response,
|
| 113 |
-
[message, chat, image],
|
| 114 |
-
[chat, submit, stop]
|
| 115 |
)
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
[
|
| 120 |
)
|
| 121 |
|
| 122 |
-
|
| 123 |
-
event1.then(*postresponse_handler)
|
| 124 |
-
event2 = submit.click(*response_handler)
|
| 125 |
-
event2.then(*postresponse_handler)
|
| 126 |
-
|
| 127 |
-
stop.click(None, None, None, cancels=[event1, event2])
|
| 128 |
-
|
| 129 |
-
demo.queue()
|
| 130 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from PIL import Image
|
| 4 |
from transformers import AutoModel, AutoProcessor
|
|
|
|
| 5 |
|
| 6 |
+
# Set device
|
| 7 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
| 8 |
|
| 9 |
+
# Load the model and processor
|
| 10 |
+
# We use trust_remote_code=True because this model has custom code.
|
| 11 |
+
model = AutoModel.from_pretrained(
|
| 12 |
+
"unum-cloud/uform-gen2-dpo",
|
| 13 |
+
trust_remote_code=True,
|
| 14 |
+
torch_dtype=torch.bfloat16
|
| 15 |
+
).to(device)
|
| 16 |
+
processor = AutoProcessor.from_pretrained(
|
| 17 |
+
"unum-cloud/uform-gen2-dpo",
|
| 18 |
+
trust_remote_code=True
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
def transcribe_image(image):
|
| 22 |
+
"""
|
| 23 |
+
Generates a caption for the given image.
|
| 24 |
+
"""
|
| 25 |
+
if image is None:
|
| 26 |
+
return "Please upload an image."
|
| 27 |
+
|
| 28 |
+
prompt = "a photo of"
|
| 29 |
+
inputs = processor(
|
| 30 |
+
text=[prompt],
|
| 31 |
+
images=[image],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
return_tensors="pt"
|
| 33 |
+
).to(device)
|
| 34 |
+
|
| 35 |
+
with torch.inference_mode():
|
| 36 |
+
output = model.generate(
|
| 37 |
+
**inputs,
|
| 38 |
+
do_sample=False,
|
| 39 |
+
use_cache=True,
|
| 40 |
+
max_new_tokens=128,
|
| 41 |
+
eos_token_id=32001,
|
| 42 |
+
pad_token_id=processor.tokenizer.pad_token_id
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
prompt_len = inputs["input_ids"].shape[1]
|
| 46 |
+
decoded_text = processor.batch_decode(output[:, prompt_len:])[0]
|
|
|
|
| 47 |
|
| 48 |
+
# Remove the end-of-sequence token
|
| 49 |
+
result = decoded_text.replace("<|im_end|>", "").strip()
|
| 50 |
+
return result
|
| 51 |
+
|
| 52 |
+
def visual_question_answer(image, question):
|
| 53 |
+
"""
|
| 54 |
+
Answers a question about the given image.
|
| 55 |
+
"""
|
| 56 |
+
if image is None:
|
| 57 |
+
return "Please upload an image."
|
| 58 |
+
if not question:
|
| 59 |
+
return "Please ask a question."
|
| 60 |
+
|
| 61 |
+
# The model expects the prompt to be in a specific format.
|
| 62 |
+
prompt = f"<|im_start|>question\n{question}<|im_end|><|im_start|>answer\n"
|
| 63 |
+
|
| 64 |
+
inputs = processor(
|
| 65 |
+
text=[prompt],
|
| 66 |
+
images=[image],
|
| 67 |
+
return_tensors="pt"
|
| 68 |
+
).to(device)
|
| 69 |
+
|
| 70 |
+
with torch.inference_mode():
|
| 71 |
+
output = model.generate(
|
| 72 |
+
**inputs,
|
| 73 |
+
do_sample=False,
|
| 74 |
+
use_cache=True,
|
| 75 |
+
max_new_tokens=128,
|
| 76 |
+
eos_token_id=32001,
|
| 77 |
+
pad_token_id=processor.tokenizer.pad_token_id
|
| 78 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
prompt_len = inputs["input_ids"].shape[1]
|
| 81 |
+
decoded_text = processor.batch_decode(output[:, prompt_len:])[0]
|
| 82 |
+
|
| 83 |
+
# Remove the end-of-sequence token
|
| 84 |
+
result = decoded_text.replace("<|im_end|>", "").strip()
|
| 85 |
+
return result
|
| 86 |
|
| 87 |
+
# Create the Gradio interface
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
+
gr.Markdown("# Image Transcription and Visual Question Answering")
|
| 90 |
+
gr.Markdown("Powered by the unum-cloud/uform-gen2-dpo model.")
|
| 91 |
+
|
| 92 |
+
with gr.Tab("Image Transcription"):
|
| 93 |
+
with gr.Row():
|
| 94 |
+
transcribe_image_input = gr.Image(type="pil", label="Upload Image")
|
| 95 |
+
transcribe_output = gr.Textbox(label="Generated Caption")
|
| 96 |
+
transcribe_button = gr.Button("Generate Caption")
|
| 97 |
+
|
| 98 |
+
with gr.Tab("Visual Question Answering"):
|
| 99 |
+
with gr.Row():
|
| 100 |
+
vqa_image_input = gr.Image(type="pil", label="Upload Image")
|
| 101 |
+
with gr.Column():
|
| 102 |
+
vqa_question_input = gr.Textbox(label="Ask a question")
|
| 103 |
+
vqa_output = gr.Textbox(label="Answer")
|
| 104 |
+
vqa_button = gr.Button("Get Answer")
|
| 105 |
+
|
| 106 |
+
# Connect the functions to the Gradio components
|
| 107 |
+
transcribe_button.click(
|
| 108 |
+
fn=transcribe_image,
|
| 109 |
+
inputs=[transcribe_image_input],
|
| 110 |
+
outputs=[transcribe_output]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
+
vqa_button.click(
|
| 113 |
+
fn=visual_question_answer,
|
| 114 |
+
inputs=[vqa_image_input, vqa_question_input],
|
| 115 |
+
outputs=[vqa_output]
|
| 116 |
)
|
| 117 |
|
| 118 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|