Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoProcessor
|
3 |
+
import torch
|
4 |
+
from PIL import Image
|
5 |
+
import io
|
6 |
+
|
7 |
+
# Load model and processor (using CPU)
|
8 |
+
folder_path = "diffusers/shot-categorizer-v0"
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(folder_path, trust_remote_code=True).eval()
|
10 |
+
processor = AutoProcessor.from_pretrained(folder_path, trust_remote_code=True)
|
11 |
+
|
12 |
+
# Define analysis function
|
13 |
+
def analyze_image(image):
|
14 |
+
# Convert Gradio image input to PIL Image
|
15 |
+
if isinstance(image, Image.Image):
|
16 |
+
img = image.convert("RGB")
|
17 |
+
else:
|
18 |
+
img = Image.open(io.BytesIO(image)).convert("RGB")
|
19 |
+
|
20 |
+
prompts = ["<COLOR>", "<LIGHTING>", "<LIGHTING_TYPE>", "<COMPOSITION>"]
|
21 |
+
results = {}
|
22 |
+
|
23 |
+
# Process each prompt
|
24 |
+
with torch.no_grad():
|
25 |
+
for prompt in prompts:
|
26 |
+
inputs = processor(text=prompt, images=img, return_tensors="pt")
|
27 |
+
generated_ids = model.generate(
|
28 |
+
input_ids=inputs["input_ids"],
|
29 |
+
pixel_values=inputs["pixel_values"],
|
30 |
+
max_new_tokens=1024,
|
31 |
+
early_stopping=False,
|
32 |
+
do_sample=False,
|
33 |
+
num_beams=3,
|
34 |
+
)
|
35 |
+
generated_text = processor.batch_decode(generated_ids, skip_special_tokens=False)[0]
|
36 |
+
parsed_answer = processor.post_process_generation(
|
37 |
+
generated_text, task=prompt, image_size=(img.width, img.height)
|
38 |
+
)
|
39 |
+
results[prompt] = parsed_answer
|
40 |
+
|
41 |
+
# Format the output
|
42 |
+
output_text = "Image Analysis Results:\n\n"
|
43 |
+
output_text += f"Color: {results['<COLOR>']}\n"
|
44 |
+
output_text += f"Lighting: {results['<LIGHTING>']}\n"
|
45 |
+
output_text += f"Lighting Type: {results['<LIGHTING_TYPE>']}\n"
|
46 |
+
output_text += f"Composition: {results['<COMPOSITION>']}\n"
|
47 |
+
|
48 |
+
return output_text
|
49 |
+
|
50 |
+
# Create Gradio interface
|
51 |
+
with gr.Blocks(title="Image Analyzer") as demo:
|
52 |
+
gr.Markdown("# Image Analysis Demo")
|
53 |
+
gr.Markdown("Upload an image to analyze its color, lighting, and composition characteristics.")
|
54 |
+
|
55 |
+
with gr.Row():
|
56 |
+
with gr.Column():
|
57 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
58 |
+
analyze_button = gr.Button("Analyze Image")
|
59 |
+
|
60 |
+
with gr.Column():
|
61 |
+
output_text = gr.Textbox(label="Analysis Results", lines=10)
|
62 |
+
|
63 |
+
# Add example images
|
64 |
+
examples = gr.Examples(
|
65 |
+
examples=["./assets/image_3.jpg"],
|
66 |
+
inputs=image_input,
|
67 |
+
label="Try with this example"
|
68 |
+
)
|
69 |
+
|
70 |
+
# Connect the button to the function
|
71 |
+
analyze_button.click(
|
72 |
+
fn=analyze_image,
|
73 |
+
inputs=image_input,
|
74 |
+
outputs=output_text
|
75 |
+
)
|
76 |
+
|
77 |
+
# Launch the demo
|
78 |
+
demo.launch()
|