capradeepgujaran
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import torch
|
4 |
+
from transformers import AutoProcessor, LlamaForCausalLM, LlamaTokenizer
|
5 |
+
|
6 |
+
# Load the Llama 2 model and processor
|
7 |
+
# Note: You'll need to replace these with the actual Llama 3.2 vision model when it becomes available
|
8 |
+
model_name = "meta-llama/Llama-2-7b-chat-hf"
|
9 |
+
processor = AutoProcessor.from_pretrained(model_name)
|
10 |
+
model = LlamaForCausalLM.from_pretrained(model_name)
|
11 |
+
tokenizer = LlamaTokenizer.from_pretrained(model_name)
|
12 |
+
|
13 |
+
def analyze_construction_image(image):
|
14 |
+
# Process the image
|
15 |
+
inputs = processor(images=image, return_tensors="pt")
|
16 |
+
|
17 |
+
# Generate text based on the image
|
18 |
+
prompt = "Analyze this construction image and identify the snag category, snag description, and steps to desnag."
|
19 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids
|
20 |
+
|
21 |
+
# Concatenate the image embeddings with the text input
|
22 |
+
combined_inputs = torch.cat([inputs.pixel_values, input_ids], dim=1)
|
23 |
+
|
24 |
+
# Generate output
|
25 |
+
outputs = model.generate(combined_inputs, max_length=300)
|
26 |
+
result = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
# Parse the result (this is a simplified example)
|
29 |
+
lines = result.split('\n')
|
30 |
+
snag_category = lines[0] if len(lines) > 0 else "N/A"
|
31 |
+
snag_description = lines[1] if len(lines) > 1 else "N/A"
|
32 |
+
desnag_steps = lines[2:] if len(lines) > 2 else ["N/A"]
|
33 |
+
|
34 |
+
return snag_category, snag_description, "\n".join(desnag_steps)
|
35 |
+
|
36 |
+
# Create the Gradio interface
|
37 |
+
iface = gr.Interface(
|
38 |
+
fn=analyze_construction_image,
|
39 |
+
inputs=gr.Image(type="pil"),
|
40 |
+
outputs=[
|
41 |
+
gr.Textbox(label="Snag Category"),
|
42 |
+
gr.Textbox(label="Snag Description"),
|
43 |
+
gr.Textbox(label="Steps to Desnag")
|
44 |
+
],
|
45 |
+
title="Construction Image Analyzer",
|
46 |
+
description="Upload a construction site image to identify issues and get desnag steps."
|
47 |
+
)
|
48 |
+
|
49 |
+
# Launch the app
|
50 |
+
iface.launch()
|