arjunanand13 commited on
Commit
36b757c
·
verified ·
1 Parent(s): 648e3f7

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from PIL import Image
3
+ from transformers import AutoProcessor, AutoModelForCausalLM
4
+ import gradio as gr
5
+ import json
6
+ import traceback
7
+ import os
8
+
9
+ model_name = "meta-llama/Llama-3.2-11B-Vision-Instruct"
10
+ token = os.environ.get("HUGGINGFACE_TOKEN")
11
+ processor = AutoProcessor.from_pretrained(model_name, token=token)
12
+ model = AutoModelForCausalLM.from_pretrained(
13
+ model_name,
14
+ quantization_config={"load_in_4bit": True},
15
+ token=token
16
+ )
17
+
18
+ if torch.cuda.is_available():
19
+ model = model.to('cuda')
20
+
21
+ def analyze_image(image, prompt):
22
+ try:
23
+ messages = [
24
+ {"role": "user", "content": [
25
+ {"type": "image"},
26
+ {"type": "text", "text": prompt}
27
+ ]}
28
+ ]
29
+ input_text = processor.apply_chat_template(messages, add_generation_prompt=True)
30
+ inputs = processor(
31
+ images=image,
32
+ text=input_text,
33
+ return_tensors="pt"
34
+ ).to(model.device)
35
+ with torch.no_grad():
36
+ output = model.generate(**inputs, max_new_tokens=100)
37
+ result = processor.decode(output[0], skip_special_tokens=True)
38
+ try:
39
+ return json.loads(result)
40
+ except json.JSONDecodeError:
41
+ return {"error": "Failed to parse model output as JSON", "raw_output": result}
42
+ except Exception as e:
43
+ return {"error": str(e), "traceback": traceback.format_exc()}
44
+
45
+ default_prompt = """Analyze this image and determine if it contains a data logger.
46
+ A data logger is typically a small, black electronic device used to monitor and record data
47
+ over time, such as voltage, temperature, or current, via external sensors.
48
+
49
+ If a data logger is present in the image, respond with:
50
+ {"present": true, "reason": "Brief explanation of why you believe it's a data logger"}
51
+
52
+ If no data logger is visible, respond with:
53
+ {"present": false, "reason": "Brief explanation of why you believe there's no data logger"}
54
+
55
+ Ensure your response is in valid JSON format."""
56
+
57
+ iface = gr.Interface(
58
+ fn=analyze_image,
59
+ inputs=[
60
+ gr.Image(type="pil", label="Upload Image"),
61
+ gr.Textbox(label="Prompt", default=default_prompt, lines=10)
62
+ ],
63
+ outputs=gr.JSON(label="Analysis Result"),
64
+ title="Data Logger Detection using Llama 3.2 Vision",
65
+ description="Upload an image and customize the prompt to check if it contains a data logger.",
66
+ examples=[
67
+ ["bad.jpg", default_prompt]
68
+ ]
69
+ )
70
+
71
+ iface.launch()