Spaces:
Running
on
Zero
Running
on
Zero
File size: 7,125 Bytes
8e31ab1 44f3097 8e31ab1 8d741e2 44f3097 8e31ab1 44f3097 8d741e2 44f3097 e266b1f 44f3097 e266b1f 44f3097 8d741e2 e266b1f 44f3097 8d741e2 44f3097 e266b1f 8d741e2 e266b1f 44f3097 8d741e2 e266b1f 44f3097 8d741e2 e266b1f 8d741e2 44f3097 8d741e2 44f3097 8d741e2 44f3097 8d741e2 44f3097 e266b1f 8d741e2 e266b1f 44f3097 8d741e2 e266b1f 8d741e2 44f3097 8d741e2 44f3097 8d741e2 44f3097 8d741e2 44f3097 8d741e2 44f3097 8d741e2 44f3097 e266b1f 44f3097 8e31ab1 8d741e2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
import gradio as gr
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoProcessor
from PIL import Image
import os
import spaces
# Initial setup without loading model to device
print("Setting up the application...")
# We'll load the model in the GPU functions to avoid CPU memory issues
model = None
tokenizer = AutoTokenizer.from_pretrained("sagar007/Lava_phi")
processor = AutoProcessor.from_pretrained("openai/clip-vit-base-patch32")
print("Tokenizer and processor loaded successfully!")
# For text-only generation with GPU on demand
@spaces.GPU
def generate_text(prompt, max_length=128):
try:
global model
# Load model if not already loaded
if model is None:
print("Loading model on first request...")
model = AutoModelForCausalLM.from_pretrained(
"sagar007/Lava_phi",
torch_dtype=torch.float16, # Use float16 on GPU
device_map="auto" # This will put the model on GPU automatically
)
print("Model loaded successfully!")
inputs = tokenizer(f"human: {prompt}\ngpt:", return_tensors="pt").to(model.device)
# Generate with GPU
with torch.no_grad():
outputs = model.generate(
**inputs,
max_new_tokens=max_length,
do_sample=True,
temperature=0.7,
top_p=0.9,
)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only the model's response
if "gpt:" in generated_text:
generated_text = generated_text.split("gpt:", 1)[1].strip()
return generated_text
except Exception as e:
# Capture and return any errors
return f"Error generating text: {str(e)}"
# For image and text processing with GPU on demand
@spaces.GPU
def process_image_and_prompt(image, prompt, max_length=128):
try:
if image is None:
return "No image provided. Please upload an image."
global model
# Load model if not already loaded
if model is None:
print("Loading model on first request...")
model = AutoModelForCausalLM.from_pretrained(
"sagar007/Lava_phi",
torch_dtype=torch.float16, # Use float16 on GPU
device_map="auto" # This will put the model on GPU automatically
)
print("Model loaded successfully!")
# Process image
image_tensor = processor(images=image, return_tensors="pt").pixel_values.to(model.device)
# Tokenize input with image token
inputs = tokenizer(f"human: <image>\n{prompt}\ngpt:", return_tensors="pt").to(model.device)
# Generate with GPU
with torch.no_grad():
outputs = model.generate(
input_ids=inputs["input_ids"],
attention_mask=inputs["attention_mask"],
images=image_tensor,
max_new_tokens=max_length,
do_sample=True,
temperature=0.7,
top_p=0.9,
)
generated_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
# Extract only the model's response
if "gpt:" in generated_text:
generated_text = generated_text.split("gpt:", 1)[1].strip()
return generated_text
except Exception as e:
# Capture and return any errors
return f"Error processing image: {str(e)}"
# Create Gradio Interface
with gr.Blocks(title="LLaVA-Phi: Vision-Language Model") as demo:
gr.Markdown("# LLaVA-Phi: Vision-Language Model")
gr.Markdown("This model uses ZeroGPU technology - GPU resources are allocated only when generating responses and released afterward.")
with gr.Tab("Text Generation"):
with gr.Row():
with gr.Column():
text_input = gr.Textbox(label="Enter your prompt", lines=3, placeholder="What is artificial intelligence?")
text_max_length = gr.Slider(minimum=16, maximum=512, value=128, step=8, label="Maximum response length")
text_button = gr.Button("Generate")
with gr.Column():
text_output = gr.Textbox(label="Generated response", lines=8)
text_status = gr.Markdown("*Status: Ready*")
def text_fn(prompt, max_length):
text_status.update("*Status: Generating response...*")
try:
response = generate_text(prompt, max_length)
text_status.update("*Status: Complete*")
return response
except Exception as e:
text_status.update("*Status: Error*")
return f"Error: {str(e)}"
text_button.click(
fn=text_fn,
inputs=[text_input, text_max_length],
outputs=text_output
)
with gr.Tab("Image + Text Analysis"):
with gr.Row():
with gr.Column():
image_input = gr.Image(type="pil", label="Upload an image")
image_text_input = gr.Textbox(label="Enter your prompt about the image",
lines=2,
placeholder="Describe this image in detail.")
image_max_length = gr.Slider(minimum=16, maximum=512, value=128, step=8, label="Maximum response length")
image_button = gr.Button("Analyze")
with gr.Column():
image_output = gr.Textbox(label="Model response", lines=8)
image_status = gr.Markdown("*Status: Ready*")
def image_fn(image, prompt, max_length):
image_status.update("*Status: Analyzing image...*")
try:
response = process_image_and_prompt(image, prompt, max_length)
image_status.update("*Status: Complete*")
return response
except Exception as e:
image_status.update("*Status: Error*")
return f"Error: {str(e)}"
image_button.click(
fn=image_fn,
inputs=[image_input, image_text_input, image_max_length],
outputs=image_output
)
# Example inputs for each tab
gr.Examples(
examples=["What is the advantage of vision-language models?",
"Explain how multimodal AI models work.",
"Tell me a short story about robots."],
inputs=text_input
)
# Status indicator
with gr.Row():
gr.Markdown("*Note: When you click Generate or Analyze, a GPU will be temporarily allocated to process your request and then released. The first request may take longer as the model needs to be loaded.*")
# Launch the app
if __name__ == "__main__":
demo.launch(
enable_queue=True,
show_error=True
) |