|
import gradio as gr |
|
from PIL import Image |
|
import torch |
|
from transformers import AutoProcessor, LlamaForCausalLM, LlamaTokenizer |
|
|
|
|
|
|
|
model_name = "meta-llama/Llama-2-7b-chat-hf" |
|
processor = AutoProcessor.from_pretrained(model_name) |
|
model = LlamaForCausalLM.from_pretrained(model_name) |
|
tokenizer = LlamaTokenizer.from_pretrained(model_name) |
|
|
|
def analyze_construction_image(image): |
|
|
|
inputs = processor(images=image, return_tensors="pt") |
|
|
|
|
|
prompt = "Analyze this construction image and identify the snag category, snag description, and steps to desnag." |
|
input_ids = tokenizer(prompt, return_tensors="pt").input_ids |
|
|
|
|
|
combined_inputs = torch.cat([inputs.pixel_values, input_ids], dim=1) |
|
|
|
|
|
outputs = model.generate(combined_inputs, max_length=300) |
|
result = tokenizer.decode(outputs[0], skip_special_tokens=True) |
|
|
|
|
|
lines = result.split('\n') |
|
snag_category = lines[0] if len(lines) > 0 else "N/A" |
|
snag_description = lines[1] if len(lines) > 1 else "N/A" |
|
desnag_steps = lines[2:] if len(lines) > 2 else ["N/A"] |
|
|
|
return snag_category, snag_description, "\n".join(desnag_steps) |
|
|
|
|
|
iface = gr.Interface( |
|
fn=analyze_construction_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs=[ |
|
gr.Textbox(label="Snag Category"), |
|
gr.Textbox(label="Snag Description"), |
|
gr.Textbox(label="Steps to Desnag") |
|
], |
|
title="Construction Image Analyzer", |
|
description="Upload a construction site image to identify issues and get desnag steps." |
|
) |
|
|
|
|
|
iface.launch() |