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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import json
|
| 7 |
+
import time
|
| 8 |
+
import os
|
| 9 |
+
import hashlib
|
| 10 |
|
| 11 |
+
# Global variables for model and processor
|
| 12 |
+
model = None
|
| 13 |
+
processor = None
|
| 14 |
+
|
| 15 |
+
# Initialize model and processor
|
| 16 |
+
def load_model():
|
| 17 |
+
global model, processor
|
| 18 |
+
if model is None:
|
| 19 |
+
try:
|
| 20 |
+
print("Loading Llama 4 Scout model...")
|
| 21 |
+
processor = AutoProcessor.from_pretrained("meta-llama/Llama-4-Scout-17B-16E-Instruct")
|
| 22 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 23 |
+
"meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
| 24 |
+
torch_dtype=torch.float16,
|
| 25 |
+
device_map="auto"
|
| 26 |
+
)
|
| 27 |
+
print("Model loaded successfully!")
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"Error loading model: {e}")
|
| 30 |
+
raise
|
| 31 |
+
return model, processor
|
| 32 |
+
|
| 33 |
+
# Simple caching mechanism
|
| 34 |
+
cache = {}
|
| 35 |
+
|
| 36 |
+
def compute_image_hash(image):
|
| 37 |
+
"""Compute a hash for an image to use as cache key"""
|
| 38 |
+
# Resize to small dimensions to ensure hash is based on content, not size
|
| 39 |
+
image = image.resize((100, 100), Image.LANCZOS)
|
| 40 |
+
|
| 41 |
+
# Convert to bytes
|
| 42 |
+
img_byte_arr = io.BytesIO()
|
| 43 |
+
image.save(img_byte_arr, format='PNG')
|
| 44 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 45 |
+
|
| 46 |
+
# Compute hash
|
| 47 |
+
return hashlib.md5(img_byte_arr).hexdigest()
|
| 48 |
+
|
| 49 |
+
def verify_document(img, doc_type, verification_info):
|
| 50 |
+
"""Verify a document using Llama 4 Scout"""
|
| 51 |
+
if img is None:
|
| 52 |
+
return "Please upload an image"
|
| 53 |
+
|
| 54 |
+
# Compute image hash for caching
|
| 55 |
+
image_hash = compute_image_hash(img)
|
| 56 |
+
cache_key = f"verify_{image_hash}_{doc_type}"
|
| 57 |
+
|
| 58 |
+
# Check cache
|
| 59 |
+
if cache_key in cache:
|
| 60 |
+
return f"[CACHED] {cache[cache_key]}"
|
| 61 |
+
|
| 62 |
+
try:
|
| 63 |
+
# Load model
|
| 64 |
+
model, processor = load_model()
|
| 65 |
+
|
| 66 |
+
# Create prompt
|
| 67 |
+
prompt = f"""This is a {doc_type} document.
|
| 68 |
+
Verify if it's authentic and extract the following information: {verification_info}
|
| 69 |
+
Provide your analysis in a structured format."""
|
| 70 |
+
|
| 71 |
+
# Process with model
|
| 72 |
+
inputs = processor(text=prompt, images=img, return_tensors="pt").to(model.device)
|
| 73 |
+
outputs = model.generate(**inputs, max_new_tokens=500)
|
| 74 |
+
result = processor.decode(outputs[0], skip_special_tokens=True)
|
| 75 |
+
|
| 76 |
+
# Save to cache
|
| 77 |
+
cache[cache_key] = result
|
| 78 |
+
|
| 79 |
+
return result
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return f"Error: {str(e)}"
|
| 82 |
+
|
| 83 |
+
def check_workplace(img, industry):
|
| 84 |
+
"""Check workplace compliance using Llama 4 Scout"""
|
| 85 |
+
if img is None:
|
| 86 |
+
return "Please upload an image"
|
| 87 |
+
|
| 88 |
+
# Compute image hash for caching
|
| 89 |
+
image_hash = compute_image_hash(img)
|
| 90 |
+
cache_key = f"workplace_{image_hash}_{industry}"
|
| 91 |
+
|
| 92 |
+
# Check cache
|
| 93 |
+
if cache_key in cache:
|
| 94 |
+
return f"[CACHED] {cache[cache_key]}"
|
| 95 |
|
| 96 |
+
try:
|
| 97 |
+
# Load model
|
| 98 |
+
model, processor = load_model()
|
| 99 |
+
|
| 100 |
+
# Create prompt
|
| 101 |
+
prompt = f"""This is a workplace in the {industry} industry.
|
| 102 |
+
Identify any safety or compliance issues visible in this image.
|
| 103 |
+
Focus on:
|
| 104 |
+
1. Safety hazards
|
| 105 |
+
2. Required signage
|
| 106 |
+
3. Proper equipment usage
|
| 107 |
+
4. Workspace organization
|
| 108 |
+
5. Compliance with regulations
|
| 109 |
+
|
| 110 |
+
Format your response as a detailed assessment with:
|
| 111 |
+
- Issues found (if any)
|
| 112 |
+
- Severity level for each issue
|
| 113 |
+
- Recommendations for correction"""
|
| 114 |
+
|
| 115 |
+
# Process with model
|
| 116 |
+
inputs = processor(text=prompt, images=img, return_tensors="pt").to(model.device)
|
| 117 |
+
outputs = model.generate(**inputs, max_new_tokens=800)
|
| 118 |
+
result = processor.decode(outputs[0], skip_special_tokens=True)
|
| 119 |
+
|
| 120 |
+
# Save to cache
|
| 121 |
+
cache[cache_key] = result
|
| 122 |
+
|
| 123 |
+
return result
|
| 124 |
+
except Exception as e:
|
| 125 |
+
return f"Error: {str(e)}"
|
| 126 |
+
|
| 127 |
+
# Create Gradio interface
|
| 128 |
+
with gr.Blocks(title="StaffManager AI Assistant") as demo:
|
| 129 |
+
gr.Markdown("# StaffManager AI Assistant")
|
| 130 |
+
gr.Markdown("This Space provides AI capabilities for StaffManager using Llama 4 Scout.")
|
| 131 |
+
|
| 132 |
+
with gr.Tab("Document Verification"):
|
| 133 |
+
with gr.Row():
|
| 134 |
+
with gr.Column():
|
| 135 |
+
doc_image = gr.Image(type="pil", label="Upload Document")
|
| 136 |
+
doc_type = gr.Dropdown(
|
| 137 |
+
["identification", "tax", "employment", "policy"],
|
| 138 |
+
label="Document Type",
|
| 139 |
+
value="identification"
|
| 140 |
+
)
|
| 141 |
+
verification_info = gr.Textbox(
|
| 142 |
+
label="Verification Data (JSON)",
|
| 143 |
+
value='{"name": "John Doe", "id_number": "ABC123456"}'
|
| 144 |
+
)
|
| 145 |
+
verify_button = gr.Button("Verify Document")
|
| 146 |
+
with gr.Column():
|
| 147 |
+
doc_result = gr.Textbox(label="Verification Result", lines=10)
|
| 148 |
+
|
| 149 |
+
verify_button.click(
|
| 150 |
+
fn=verify_document,
|
| 151 |
+
inputs=[doc_image, doc_type, verification_info],
|
| 152 |
+
outputs=[doc_result]
|
| 153 |
+
)
|
| 154 |
+
|
| 155 |
+
with gr.Tab("Workplace Compliance"):
|
| 156 |
+
with gr.Row():
|
| 157 |
+
with gr.Column():
|
| 158 |
+
workplace_image = gr.Image(type="pil", label="Upload Workplace Image")
|
| 159 |
+
industry_type = gr.Dropdown(
|
| 160 |
+
["retail", "restaurant", "healthcare", "manufacturing"],
|
| 161 |
+
label="Industry",
|
| 162 |
+
value="retail"
|
| 163 |
+
)
|
| 164 |
+
check_button = gr.Button("Check Compliance")
|
| 165 |
+
with gr.Column():
|
| 166 |
+
compliance_result = gr.Textbox(label="Compliance Assessment", lines=10)
|
| 167 |
+
|
| 168 |
+
check_button.click(
|
| 169 |
+
fn=check_workplace,
|
| 170 |
+
inputs=[workplace_image, industry_type],
|
| 171 |
+
outputs=[compliance_result]
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
with gr.Tab("About"):
|
| 175 |
+
gr.Markdown("""
|
| 176 |
+
## About StaffManager AI Assistant
|
| 177 |
+
|
| 178 |
+
This Space uses the Llama 4 Scout model to provide AI capabilities for StaffManager:
|
| 179 |
+
|
| 180 |
+
- **Document Verification**: Verify and extract information from documents
|
| 181 |
+
- **Workplace Compliance**: Identify safety and compliance issues in workplace images
|
| 182 |
+
|
| 183 |
+
The model is loaded on demand and results are cached for better performance.
|
| 184 |
+
|
| 185 |
+
### Model Information
|
| 186 |
+
|
| 187 |
+
- Model: meta-llama/Llama-4-Scout-17B-16E-Instruct
|
| 188 |
+
- Type: Multimodal (image + text)
|
| 189 |
+
- Size: 17B parameters
|
| 190 |
+
""")
|
| 191 |
+
|
| 192 |
+
# Launch the app
|
| 193 |
demo.launch()
|