Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import pipeline
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
import json
|
|
@@ -11,6 +10,7 @@ import base64
|
|
| 11 |
from huggingface_hub import login
|
| 12 |
import traceback
|
| 13 |
import sys
|
|
|
|
| 14 |
|
| 15 |
# Print Python and library versions for debugging
|
| 16 |
print(f"Python version: {sys.version}")
|
|
@@ -33,15 +33,27 @@ except Exception as e:
|
|
| 33 |
print(f"Error logging in: {e}")
|
| 34 |
|
| 35 |
# Global variables
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
# Initialize Llama 4 Scout
|
| 39 |
-
def
|
| 40 |
-
global
|
| 41 |
|
| 42 |
-
if
|
| 43 |
try:
|
| 44 |
-
print("Loading Llama 4 Scout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
# Use 4-bit quantization to reduce memory usage
|
| 47 |
from transformers import BitsAndBytesConfig
|
|
@@ -52,83 +64,43 @@ def load_llama_pipeline():
|
|
| 52 |
bnb_4bit_quant_type="nf4"
|
| 53 |
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
"
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
for pipeline_type in pipeline_types:
|
| 63 |
-
try:
|
| 64 |
-
print(f"Trying pipeline type: {pipeline_type}")
|
| 65 |
-
llama_pipeline = pipeline(
|
| 66 |
-
pipeline_type,
|
| 67 |
-
model="meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
| 68 |
-
device_map="auto",
|
| 69 |
-
model_kwargs={"quantization_config": quantization_config},
|
| 70 |
-
token=token
|
| 71 |
-
)
|
| 72 |
-
print(f"Successfully loaded Llama 4 Scout with pipeline type: {pipeline_type}")
|
| 73 |
-
break
|
| 74 |
-
except Exception as pipeline_error:
|
| 75 |
-
print(f"Failed to load with pipeline type {pipeline_type}: {pipeline_error}")
|
| 76 |
|
| 77 |
-
|
| 78 |
-
# If all pipeline types fail, try loading with AutoModel classes
|
| 79 |
-
print("Trying to load with AutoModel classes...")
|
| 80 |
-
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 81 |
-
|
| 82 |
-
processor = AutoProcessor.from_pretrained(
|
| 83 |
-
"meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
| 84 |
-
token=token
|
| 85 |
-
)
|
| 86 |
-
|
| 87 |
-
model = AutoModelForVision2Seq.from_pretrained(
|
| 88 |
-
"meta-llama/Llama-4-Scout-17B-16E-Instruct",
|
| 89 |
-
token=token,
|
| 90 |
-
quantization_config=quantization_config,
|
| 91 |
-
device_map="auto"
|
| 92 |
-
)
|
| 93 |
-
|
| 94 |
-
# Create a custom pipeline function
|
| 95 |
-
def custom_pipeline(image, prompt, max_new_tokens=300):
|
| 96 |
-
inputs = processor(text=prompt, images=image, return_tensors="pt").to(model.device)
|
| 97 |
-
outputs = model.generate(**inputs, max_new_tokens=max_new_tokens)
|
| 98 |
-
return processor.decode(outputs[0], skip_special_tokens=True)
|
| 99 |
-
|
| 100 |
-
llama_pipeline = custom_pipeline
|
| 101 |
-
print("Successfully created custom Llama 4 Scout pipeline")
|
| 102 |
-
|
| 103 |
-
# If still None, fall back to LLaVA
|
| 104 |
-
if llama_pipeline is None:
|
| 105 |
-
print("All Llama 4 Scout loading attempts failed, falling back to LLaVA...")
|
| 106 |
-
llama_pipeline = pipeline(
|
| 107 |
-
"image-to-text",
|
| 108 |
-
model="llava-hf/llava-1.5-7b-hf",
|
| 109 |
-
device_map="auto",
|
| 110 |
-
model_kwargs={"quantization_config": quantization_config}
|
| 111 |
-
)
|
| 112 |
-
print("LLaVA pipeline loaded as fallback")
|
| 113 |
|
| 114 |
except Exception as e:
|
| 115 |
-
print(f"Error loading
|
| 116 |
print(traceback.format_exc())
|
| 117 |
|
| 118 |
-
#
|
| 119 |
try:
|
| 120 |
-
print("Falling back to LLaVA
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
device_map="auto"
|
| 125 |
)
|
| 126 |
-
print("LLaVA
|
| 127 |
except Exception as fallback_error:
|
| 128 |
print(f"Even fallback failed: {fallback_error}")
|
| 129 |
raise
|
| 130 |
|
| 131 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# Simple caching mechanism
|
| 134 |
cache = {}
|
|
@@ -160,38 +132,57 @@ def verify_document(img, doc_type, verification_info):
|
|
| 160 |
return f"[CACHED] {cache[cache_key]}"
|
| 161 |
|
| 162 |
try:
|
| 163 |
-
# Load
|
| 164 |
-
|
| 165 |
|
| 166 |
# Create prompt
|
| 167 |
prompt = f"""This is a {doc_type} document.
|
| 168 |
Verify if it's authentic and extract the following information: {verification_info}
|
| 169 |
Provide your analysis in a structured format."""
|
| 170 |
|
| 171 |
-
# Process with
|
| 172 |
start_time = time.time()
|
| 173 |
print(f"Starting document verification at {start_time}")
|
| 174 |
|
| 175 |
-
#
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
|
| 188 |
end_time = time.time()
|
| 189 |
print(f"Completed document verification in {end_time - start_time:.2f} seconds")
|
| 190 |
|
| 191 |
# Save to cache
|
| 192 |
-
cache[cache_key] =
|
| 193 |
|
| 194 |
-
return
|
| 195 |
except Exception as e:
|
| 196 |
error_details = traceback.format_exc()
|
| 197 |
print(f"Error in verify_document: {e}")
|
|
@@ -212,8 +203,8 @@ def check_workplace(img, industry):
|
|
| 212 |
return f"[CACHED] {cache[cache_key]}"
|
| 213 |
|
| 214 |
try:
|
| 215 |
-
# Load
|
| 216 |
-
|
| 217 |
|
| 218 |
# Create prompt
|
| 219 |
prompt = f"""This is a workplace in the {industry} industry.
|
|
@@ -230,30 +221,49 @@ Format your response as a detailed assessment with:
|
|
| 230 |
- Severity level for each issue
|
| 231 |
- Recommendations for correction"""
|
| 232 |
|
| 233 |
-
# Process with
|
| 234 |
start_time = time.time()
|
| 235 |
print(f"Starting workplace compliance check at {start_time}")
|
| 236 |
|
| 237 |
-
#
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
end_time = time.time()
|
| 251 |
print(f"Completed workplace compliance check in {end_time - start_time:.2f} seconds")
|
| 252 |
|
| 253 |
# Save to cache
|
| 254 |
-
cache[cache_key] =
|
| 255 |
|
| 256 |
-
return
|
| 257 |
except Exception as e:
|
| 258 |
error_details = traceback.format_exc()
|
| 259 |
print(f"Error in check_workplace: {e}")
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
import io
|
| 5 |
import json
|
|
|
|
| 10 |
from huggingface_hub import login
|
| 11 |
import traceback
|
| 12 |
import sys
|
| 13 |
+
import requests
|
| 14 |
|
| 15 |
# Print Python and library versions for debugging
|
| 16 |
print(f"Python version: {sys.version}")
|
|
|
|
| 33 |
print(f"Error logging in: {e}")
|
| 34 |
|
| 35 |
# Global variables
|
| 36 |
+
model = None
|
| 37 |
+
processor = None
|
| 38 |
|
| 39 |
+
# Initialize Llama 4 Scout model
|
| 40 |
+
def load_llama4_model():
|
| 41 |
+
global model, processor
|
| 42 |
|
| 43 |
+
if model is None or processor is None:
|
| 44 |
try:
|
| 45 |
+
print("Loading Llama 4 Scout model...")
|
| 46 |
+
|
| 47 |
+
# Import the correct classes for Llama 4
|
| 48 |
+
from transformers import AutoProcessor, Llama4ForConditionalGeneration
|
| 49 |
+
|
| 50 |
+
model_id = "meta-llama/Llama-4-Scout-17B-16E-Instruct"
|
| 51 |
+
|
| 52 |
+
# Load processor and model
|
| 53 |
+
processor = AutoProcessor.from_pretrained(
|
| 54 |
+
model_id,
|
| 55 |
+
token=token
|
| 56 |
+
)
|
| 57 |
|
| 58 |
# Use 4-bit quantization to reduce memory usage
|
| 59 |
from transformers import BitsAndBytesConfig
|
|
|
|
| 64 |
bnb_4bit_quant_type="nf4"
|
| 65 |
)
|
| 66 |
|
| 67 |
+
model = Llama4ForConditionalGeneration.from_pretrained(
|
| 68 |
+
model_id,
|
| 69 |
+
token=token,
|
| 70 |
+
device_map="auto",
|
| 71 |
+
torch_dtype=torch.bfloat16,
|
| 72 |
+
quantization_config=quantization_config
|
| 73 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
print("Llama 4 Scout model loaded successfully!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
except Exception as e:
|
| 78 |
+
print(f"Error loading Llama 4 Scout model: {e}")
|
| 79 |
print(traceback.format_exc())
|
| 80 |
|
| 81 |
+
# Fall back to LLaVA if Llama 4 fails
|
| 82 |
try:
|
| 83 |
+
print("Falling back to LLaVA...")
|
| 84 |
+
from transformers import AutoProcessor, AutoModelForVision2Seq
|
| 85 |
+
|
| 86 |
+
processor = AutoProcessor.from_pretrained("llava-hf/llava-1.5-7b-hf")
|
| 87 |
+
model = AutoModelForVision2Seq.from_pretrained(
|
| 88 |
+
"llava-hf/llava-1.5-7b-hf",
|
| 89 |
device_map="auto"
|
| 90 |
)
|
| 91 |
+
print("LLaVA model loaded as fallback")
|
| 92 |
except Exception as fallback_error:
|
| 93 |
print(f"Even fallback failed: {fallback_error}")
|
| 94 |
raise
|
| 95 |
|
| 96 |
+
return model, processor
|
| 97 |
+
|
| 98 |
+
# Function to convert PIL Image to base64
|
| 99 |
+
def image_to_base64(img):
|
| 100 |
+
buffered = io.BytesIO()
|
| 101 |
+
img.save(buffered, format="PNG")
|
| 102 |
+
img_str = base64.b64encode(buffered.getvalue()).decode()
|
| 103 |
+
return f"data:image/png;base64,{img_str}"
|
| 104 |
|
| 105 |
# Simple caching mechanism
|
| 106 |
cache = {}
|
|
|
|
| 132 |
return f"[CACHED] {cache[cache_key]}"
|
| 133 |
|
| 134 |
try:
|
| 135 |
+
# Load model and processor
|
| 136 |
+
model, processor = load_llama4_model()
|
| 137 |
|
| 138 |
# Create prompt
|
| 139 |
prompt = f"""This is a {doc_type} document.
|
| 140 |
Verify if it's authentic and extract the following information: {verification_info}
|
| 141 |
Provide your analysis in a structured format."""
|
| 142 |
|
| 143 |
+
# Process with model
|
| 144 |
start_time = time.time()
|
| 145 |
print(f"Starting document verification at {start_time}")
|
| 146 |
|
| 147 |
+
# Convert image to base64 URL
|
| 148 |
+
img_url = image_to_base64(img)
|
| 149 |
+
|
| 150 |
+
# Create messages format
|
| 151 |
+
messages = [
|
| 152 |
+
{
|
| 153 |
+
"role": "user",
|
| 154 |
+
"content": [
|
| 155 |
+
{"type": "image", "url": img_url},
|
| 156 |
+
{"type": "text", "text": prompt},
|
| 157 |
+
]
|
| 158 |
+
},
|
| 159 |
+
]
|
| 160 |
+
|
| 161 |
+
# Process input using the chat template
|
| 162 |
+
inputs = processor.apply_chat_template(
|
| 163 |
+
messages,
|
| 164 |
+
add_generation_prompt=True,
|
| 165 |
+
tokenize=True,
|
| 166 |
+
return_dict=True,
|
| 167 |
+
return_tensors="pt",
|
| 168 |
+
).to(model.device)
|
| 169 |
+
|
| 170 |
+
# Generate output
|
| 171 |
+
outputs = model.generate(
|
| 172 |
+
**inputs,
|
| 173 |
+
max_new_tokens=300,
|
| 174 |
+
)
|
| 175 |
+
|
| 176 |
+
# Decode output
|
| 177 |
+
result = processor.batch_decode(outputs[:, inputs["input_ids"].shape[-1]:])[0]
|
| 178 |
|
| 179 |
end_time = time.time()
|
| 180 |
print(f"Completed document verification in {end_time - start_time:.2f} seconds")
|
| 181 |
|
| 182 |
# Save to cache
|
| 183 |
+
cache[cache_key] = result
|
| 184 |
|
| 185 |
+
return result
|
| 186 |
except Exception as e:
|
| 187 |
error_details = traceback.format_exc()
|
| 188 |
print(f"Error in verify_document: {e}")
|
|
|
|
| 203 |
return f"[CACHED] {cache[cache_key]}"
|
| 204 |
|
| 205 |
try:
|
| 206 |
+
# Load model and processor
|
| 207 |
+
model, processor = load_llama4_model()
|
| 208 |
|
| 209 |
# Create prompt
|
| 210 |
prompt = f"""This is a workplace in the {industry} industry.
|
|
|
|
| 221 |
- Severity level for each issue
|
| 222 |
- Recommendations for correction"""
|
| 223 |
|
| 224 |
+
# Process with model
|
| 225 |
start_time = time.time()
|
| 226 |
print(f"Starting workplace compliance check at {start_time}")
|
| 227 |
|
| 228 |
+
# Convert image to base64 URL
|
| 229 |
+
img_url = image_to_base64(img)
|
| 230 |
+
|
| 231 |
+
# Create messages format
|
| 232 |
+
messages = [
|
| 233 |
+
{
|
| 234 |
+
"role": "user",
|
| 235 |
+
"content": [
|
| 236 |
+
{"type": "image", "url": img_url},
|
| 237 |
+
{"type": "text", "text": prompt},
|
| 238 |
+
]
|
| 239 |
+
},
|
| 240 |
+
]
|
| 241 |
+
|
| 242 |
+
# Process input using the chat template
|
| 243 |
+
inputs = processor.apply_chat_template(
|
| 244 |
+
messages,
|
| 245 |
+
add_generation_prompt=True,
|
| 246 |
+
tokenize=True,
|
| 247 |
+
return_dict=True,
|
| 248 |
+
return_tensors="pt",
|
| 249 |
+
).to(model.device)
|
| 250 |
+
|
| 251 |
+
# Generate output
|
| 252 |
+
outputs = model.generate(
|
| 253 |
+
**inputs,
|
| 254 |
+
max_new_tokens=300,
|
| 255 |
+
)
|
| 256 |
+
|
| 257 |
+
# Decode output
|
| 258 |
+
result = processor.batch_decode(outputs[:, inputs["input_ids"].shape[-1]:])[0]
|
| 259 |
|
| 260 |
end_time = time.time()
|
| 261 |
print(f"Completed workplace compliance check in {end_time - start_time:.2f} seconds")
|
| 262 |
|
| 263 |
# Save to cache
|
| 264 |
+
cache[cache_key] = result
|
| 265 |
|
| 266 |
+
return result
|
| 267 |
except Exception as e:
|
| 268 |
error_details = traceback.format_exc()
|
| 269 |
print(f"Error in check_workplace: {e}")
|