Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -1,45 +1,95 @@
|
|
1 |
-
import torch
|
2 |
-
import argparse
|
3 |
import gradio as gr
|
4 |
-
|
5 |
-
from transformers import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Load
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
model_id, torch_dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32
|
15 |
-
).to(device)
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
31 |
|
32 |
-
chat_history = gr.
|
33 |
-
|
34 |
-
|
35 |
-
image_input = gr.Image(label="Upload an image (optional)", type="pil", optional=True)
|
36 |
-
send_btn = gr.Button("Send")
|
37 |
|
38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
# ["Describe this image", "example_image.jpg"],
|
42 |
-
# ["Generate an image of a futuristic city"],
|
43 |
-
# ], inputs=[user_input, image_input])
|
44 |
|
45 |
-
demo.launch()
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import AutoConfig, AutoModelForCausalLM
|
4 |
+
from janus.models import MultiModalityCausalLM, VLChatProcessor
|
5 |
+
from janus.utils.io import load_pil_images
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
import numpy as np
|
9 |
+
import os
|
10 |
+
import time
|
11 |
+
from Upsample import RealESRGAN
|
12 |
+
import spaces # Import spaces for ZeroGPU compatibility
|
13 |
|
14 |
+
# Load model and processor
|
15 |
+
model_path = "deepseek-ai/Janus-Pro-7B"
|
16 |
+
config = AutoConfig.from_pretrained(model_path)
|
17 |
+
language_config = config.language_config
|
18 |
+
language_config._attn_implementation = 'eager'
|
19 |
+
vl_gpt = AutoModelForCausalLM.from_pretrained(model_path, language_config=language_config, trust_remote_code=True)
|
20 |
+
if torch.cuda.is_available():
|
21 |
+
vl_gpt = vl_gpt.to(torch.bfloat16).cuda()
|
22 |
+
else:
|
23 |
+
vl_gpt = vl_gpt.to(torch.float16)
|
24 |
|
25 |
+
vl_chat_processor = VLChatProcessor.from_pretrained(model_path)
|
26 |
+
tokenizer = vl_chat_processor.tokenizer
|
27 |
+
cuda_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
|
|
|
|
28 |
|
29 |
+
# SR model
|
30 |
+
sr_model = RealESRGAN(torch.device('cuda' if torch.cuda.is_available() else 'cpu'), scale=2)
|
31 |
+
sr_model.load_weights(f'weights/RealESRGAN_x2.pth', download=False)
|
32 |
+
|
33 |
+
@torch.inference_mode()
|
34 |
+
@spaces.GPU(duration=120)
|
35 |
+
def multimodal_understanding(image, question, seed, top_p, temperature, progress=gr.Progress(track_tqdm=True)):
|
36 |
+
# Clear CUDA cache before generating
|
37 |
+
torch.cuda.empty_cache()
|
38 |
+
|
39 |
+
# set seed
|
40 |
+
torch.manual_seed(seed)
|
41 |
+
np.random.seed(seed)
|
42 |
+
torch.cuda.manual_seed(seed)
|
43 |
|
44 |
+
conversation = [
|
45 |
+
{
|
46 |
+
"role": "<|User |>",
|
47 |
+
"content": f"<image_placeholder>\n{question}",
|
48 |
+
"images": [image],
|
49 |
+
},
|
50 |
+
{"role": "<|Assistant|>", "content": ""},
|
51 |
+
]
|
52 |
|
53 |
+
pil_images = [Image.fromarray(image)]
|
54 |
+
prepare_inputs = vl_chat_processor(conversations=conversation, images=pil_images, force_batchify=True
|
55 |
+
).to(cuda_device, dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float16)
|
56 |
+
|
57 |
+
inputs_embeds = vl_gpt.prepare_inputs_embeds(**prepare_inputs)
|
58 |
+
|
59 |
+
outputs = vl_gpt.language_model.generate(inputs_embeds=inputs_embeds, attention_mask=prepare_inputs.attention_mask,
|
60 |
+
pad_token_id=tokenizer.eos_token_id, bos_token_id=tokenizer.bos_token_id,
|
61 |
+
eos_token_id=tokenizer.eos_token_id, max_new_tokens=512, temperature=temperature, top_p=top_p,
|
62 |
+
do_sample=False if temperature == 0 else True, use_cache=True,)
|
63 |
+
|
64 |
+
answer = tokenizer.decode(outputs[0].cpu().tolist(), skip_special_tokens=True)
|
65 |
+
return answer
|
66 |
|
67 |
+
# Gradio interface
|
68 |
+
css = '''
|
69 |
+
.gradio-container {max-width: 960px !important}
|
70 |
+
'''
|
71 |
+
with gr.Blocks(css=css) as demo:
|
72 |
+
gr.Markdown("# Janus Pro 7B Chat Interface")
|
73 |
|
74 |
+
chat_history = gr.Chatbot(label="Chat History")
|
75 |
+
message_input = gr.Textbox(label="Type your message here...")
|
76 |
+
image_input = gr.Image(label="Upload an image (optional)", type="numpy", tool="editor")
|
|
|
|
|
77 |
|
78 |
+
def respond(message, image):
|
79 |
+
# Here you can add logic to handle the image if provided
|
80 |
+
if image is not None:
|
81 |
+
# Call multimodal understanding with the image and message
|
82 |
+
response = multimodal_understanding(image, message, seed=42, top_p=0.95, temperature=0.1)
|
83 |
+
else:
|
84 |
+
# If no image is provided, just respond with a text-based answer
|
85 |
+
response = "Please provide an image for multimodal understanding."
|
86 |
+
|
87 |
+
return response
|
88 |
+
|
89 |
+
def submit_message(message, image):
|
90 |
+
response = respond(message, image)
|
91 |
+
return message, response
|
92 |
|
93 |
+
message_input.submit(submit_message, inputs=[message_input, image_input], outputs=[message_input, chat_history])
|
|
|
|
|
|
|
94 |
|
95 |
+
demo.launch(share=True)
|