cpg716 commited on
Commit
90a2abe
·
verified ·
1 Parent(s): 5bf0183

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -5
app.py CHANGED
@@ -7,7 +7,6 @@ import json
7
  import time
8
  import os
9
  import hashlib
10
- import huggingface_hub
11
  from huggingface_hub import login
12
 
13
  # Print token information (first few characters only for security)
@@ -24,9 +23,6 @@ try:
24
  except Exception as e:
25
  print(f"Error logging in: {e}")
26
 
27
- # Set token for all huggingface_hub operations
28
- huggingface_hub.set_token(token)
29
-
30
  # Simple test to verify token works
31
  try:
32
  from huggingface_hub import whoami
@@ -74,4 +70,164 @@ def load_model():
74
  raise
75
  return model, processor
76
 
77
- # Rest of the code remains the same...
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
  import time
8
  import os
9
  import hashlib
 
10
  from huggingface_hub import login
11
 
12
  # Print token information (first few characters only for security)
 
23
  except Exception as e:
24
  print(f"Error logging in: {e}")
25
 
 
 
 
26
  # Simple test to verify token works
27
  try:
28
  from huggingface_hub import whoami
 
70
  raise
71
  return model, processor
72
 
73
+ # Simple caching mechanism
74
+ cache = {}
75
+
76
+ def compute_image_hash(image):
77
+ """Compute a hash for an image to use as cache key"""
78
+ # Resize to small dimensions to ensure hash is based on content, not size
79
+ image = image.resize((100, 100), Image.LANCZOS)
80
+
81
+ # Convert to bytes
82
+ img_byte_arr = io.BytesIO()
83
+ image.save(img_byte_arr, format='PNG')
84
+ img_byte_arr = img_byte_arr.getvalue()
85
+
86
+ # Compute hash
87
+ return hashlib.md5(img_byte_arr).hexdigest()
88
+
89
+ def verify_document(img, doc_type, verification_info):
90
+ """Verify a document using Llama 4 Scout"""
91
+ if img is None:
92
+ return "Please upload an image"
93
+
94
+ # Compute image hash for caching
95
+ image_hash = compute_image_hash(img)
96
+ cache_key = f"verify_{image_hash}_{doc_type}"
97
+
98
+ # Check cache
99
+ if cache_key in cache:
100
+ return f"[CACHED] {cache[cache_key]}"
101
+
102
+ try:
103
+ # Load model
104
+ model, processor = load_model()
105
+
106
+ # Create prompt
107
+ prompt = f"""This is a {doc_type} document.
108
+ Verify if it's authentic and extract the following information: {verification_info}
109
+ Provide your analysis in a structured format."""
110
+
111
+ # Process with model
112
+ inputs = processor(text=prompt, images=img, return_tensors="pt").to(model.device)
113
+ outputs = model.generate(**inputs, max_new_tokens=500)
114
+ result = processor.decode(outputs[0], skip_special_tokens=True)
115
+
116
+ # Save to cache
117
+ cache[cache_key] = result
118
+
119
+ return result
120
+ except Exception as e:
121
+ return f"Error: {str(e)}"
122
+
123
+ def check_workplace(img, industry):
124
+ """Check workplace compliance using Llama 4 Scout"""
125
+ if img is None:
126
+ return "Please upload an image"
127
+
128
+ # Compute image hash for caching
129
+ image_hash = compute_image_hash(img)
130
+ cache_key = f"workplace_{image_hash}_{industry}"
131
+
132
+ # Check cache
133
+ if cache_key in cache:
134
+ return f"[CACHED] {cache[cache_key]}"
135
+
136
+ try:
137
+ # Load model
138
+ model, processor = load_model()
139
+
140
+ # Create prompt
141
+ prompt = f"""This is a workplace in the {industry} industry.
142
+ Identify any safety or compliance issues visible in this image.
143
+ Focus on:
144
+ 1. Safety hazards
145
+ 2. Required signage
146
+ 3. Proper equipment usage
147
+ 4. Workspace organization
148
+ 5. Compliance with regulations
149
+
150
+ Format your response as a detailed assessment with:
151
+ - Issues found (if any)
152
+ - Severity level for each issue
153
+ - Recommendations for correction"""
154
+
155
+ # Process with model
156
+ inputs = processor(text=prompt, images=img, return_tensors="pt").to(model.device)
157
+ outputs = model.generate(**inputs, max_new_tokens=800)
158
+ result = processor.decode(outputs[0], skip_special_tokens=True)
159
+
160
+ # Save to cache
161
+ cache[cache_key] = result
162
+
163
+ return result
164
+ except Exception as e:
165
+ return f"Error: {str(e)}"
166
+
167
+ # Create Gradio interface
168
+ with gr.Blocks(title="StaffManager AI Assistant") as demo:
169
+ gr.Markdown("# StaffManager AI Assistant")
170
+ gr.Markdown("This Space provides AI capabilities for StaffManager using Llama 4 Scout.")
171
+
172
+ with gr.Tab("Document Verification"):
173
+ with gr.Row():
174
+ with gr.Column():
175
+ doc_image = gr.Image(type="pil", label="Upload Document")
176
+ doc_type = gr.Dropdown(
177
+ ["identification", "tax", "employment", "policy"],
178
+ label="Document Type",
179
+ value="identification"
180
+ )
181
+ verification_info = gr.Textbox(
182
+ label="Verification Data (JSON)",
183
+ value='{"name": "John Doe", "id_number": "ABC123456"}'
184
+ )
185
+ verify_button = gr.Button("Verify Document")
186
+ with gr.Column():
187
+ doc_result = gr.Textbox(label="Verification Result", lines=10)
188
+
189
+ verify_button.click(
190
+ fn=verify_document,
191
+ inputs=[doc_image, doc_type, verification_info],
192
+ outputs=[doc_result]
193
+ )
194
+
195
+ with gr.Tab("Workplace Compliance"):
196
+ with gr.Row():
197
+ with gr.Column():
198
+ workplace_image = gr.Image(type="pil", label="Upload Workplace Image")
199
+ industry_type = gr.Dropdown(
200
+ ["retail", "restaurant", "healthcare", "manufacturing"],
201
+ label="Industry",
202
+ value="retail"
203
+ )
204
+ check_button = gr.Button("Check Compliance")
205
+ with gr.Column():
206
+ compliance_result = gr.Textbox(label="Compliance Assessment", lines=10)
207
+
208
+ check_button.click(
209
+ fn=check_workplace,
210
+ inputs=[workplace_image, industry_type],
211
+ outputs=[compliance_result]
212
+ )
213
+
214
+ with gr.Tab("About"):
215
+ gr.Markdown("""
216
+ ## About StaffManager AI Assistant
217
+
218
+ This Space uses the Llama 4 Scout model to provide AI capabilities for StaffManager:
219
+
220
+ - **Document Verification**: Verify and extract information from documents
221
+ - **Workplace Compliance**: Identify safety and compliance issues in workplace images
222
+
223
+ The model is loaded on demand and results are cached for better performance.
224
+
225
+ ### Model Information
226
+
227
+ - Model: meta-llama/Llama-4-Scout-17B-16E-Instruct
228
+ - Type: Multimodal (image + text)
229
+ - Size: 17B parameters
230
+ """)
231
+
232
+ # Launch the app
233
+ demo.launch()