Spaces:
Sleeping
Sleeping
VishalD1234
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,70 +6,23 @@ from decord import cpu, VideoReader, bridge
|
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
from transformers import BitsAndBytesConfig
|
8 |
|
|
|
9 |
MODEL_PATH = "THUDM/cogvlm2-llama3-caption"
|
10 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
11 |
TORCH_TYPE = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 8 else torch.float16
|
12 |
|
13 |
-
|
14 |
DELAY_REASONS = {
|
15 |
-
"Step 1": ["Delay in Bead Insertion",
|
16 |
-
"Step 2": ["Inner Liner Adjustment by Technician",
|
17 |
-
"Step 3": ["Manual Adjustment in Ply1 apply",
|
18 |
-
"Step 4": ["Delay in Bead set",
|
19 |
-
"Step 5": ["Delay in Turnup",
|
20 |
-
"Step 6": ["Person Repairing sidewall",
|
21 |
-
"Step 7": ["Delay in sidewall stitching",
|
22 |
-
"Step 8": ["No person available to load Carcass",
|
23 |
}
|
24 |
|
25 |
-
def get_step_info(step_number):
|
26 |
-
"""Returns detailed information about a manufacturing step."""
|
27 |
-
step_details = {
|
28 |
-
1: {
|
29 |
-
"Name": "Bead Insertion",
|
30 |
-
"Standard Time": "4 seconds",
|
31 |
-
"Analysis": "Observe the bead placement process. If the insertion exceeds 4 seconds, identify potential issues such as missing beads, technician errors, or machinery malfunction."
|
32 |
-
},
|
33 |
-
2: {
|
34 |
-
"Name": "Inner Liner Apply",
|
35 |
-
"Standard Time": "4 seconds",
|
36 |
-
"Analysis": "Check for manual intervention during the inner layer application. If adjustment is required, it may indicate improper alignment or issues with the layer material."
|
37 |
-
},
|
38 |
-
3: {
|
39 |
-
"Name": "Ply1 Apply",
|
40 |
-
"Standard Time": "4 seconds",
|
41 |
-
"Analysis": "Determine if the technician is manually adjusting the first ply. Manual intervention might suggest improper ply placement or machine misalignment."
|
42 |
-
},
|
43 |
-
4: {
|
44 |
-
"Name": "Bead Set",
|
45 |
-
"Standard Time": "8 seconds",
|
46 |
-
"Analysis": "Observe the bead setting process. Delays may result from bead misalignment, machine pauses, or lack of technician involvement."
|
47 |
-
},
|
48 |
-
5: {
|
49 |
-
"Name": "Turnup",
|
50 |
-
"Standard Time": "4 seconds",
|
51 |
-
"Analysis": "Examine the turnup step for any technician involvement or pauses in machine operation. Reasons for delays might include material misalignment or equipment issues."
|
52 |
-
},
|
53 |
-
6: {
|
54 |
-
"Name": "Sidewall Apply",
|
55 |
-
"Standard Time": "14 seconds",
|
56 |
-
"Analysis": "If a technician is repairing the sidewall, this may indicate material damage or improper initial application. Look for signs of excessive manual handling."
|
57 |
-
},
|
58 |
-
7: {
|
59 |
-
"Name": "Sidewall Stitching",
|
60 |
-
"Standard Time": "5 seconds",
|
61 |
-
"Analysis": "Observe the stitching process. Delays could occur due to machine speed inconsistencies or technician intervention for correction."
|
62 |
-
},
|
63 |
-
8: {
|
64 |
-
"Name": "Carcass Unload",
|
65 |
-
"Standard Time": "7 seconds",
|
66 |
-
"Analysis": "Ensure a technician is present for the carcass unload. If absent, note their return time and identify potential reasons for their absence."
|
67 |
-
}
|
68 |
-
}
|
69 |
-
|
70 |
-
return step_details.get(step_number, {"Error": "Invalid step number. Please provide a valid step number."})
|
71 |
-
|
72 |
-
|
73 |
def load_video(video_data, strategy='chat'):
|
74 |
"""Loads and processes video data into a format suitable for model input."""
|
75 |
bridge.set_bridge('torch')
|
@@ -151,56 +104,38 @@ def predict(prompt, video_data, temperature, model, tokenizer):
|
|
151 |
|
152 |
return response
|
153 |
|
154 |
-
def get_analysis_prompt(step_number):
|
155 |
"""Constructs the prompt for analyzing delay reasons based on the selected step."""
|
156 |
-
|
157 |
-
|
158 |
-
if "Error" in step_info:
|
159 |
-
return step_info["Error"]
|
160 |
-
|
161 |
-
step_name = step_info["Name"]
|
162 |
-
standard_time = step_info["Standard Time"]
|
163 |
-
analysis = step_info["Analysis"]
|
164 |
-
|
165 |
-
return f"""
|
166 |
-
You are an AI expert system specialized in analyzing manufacturing processes and identifying production delays in tire manufacturing. Your role is to accurately classify delay reasons based on visual evidence from production line footage.
|
167 |
Task Context:
|
168 |
-
You are analyzing video footage from Step {step_number} of a tire manufacturing process where a delay has been detected.
|
|
|
169 |
Required Analysis:
|
170 |
-
Carefully observe the video for visual cues indicating production interruption.
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
Please provide your analysis in the following format:
|
177 |
-
DELAY_REASONS = {
|
178 |
-
"Step 1": ["Delay in Bead Insertion","Lack of raw material"],
|
179 |
-
"Step 2": ["Inner Liner Adjustment by Technician","Person rebuilding defective Tire Sections"],
|
180 |
-
"Step 3": ["Manual Adjustment in Ply1 apply","Technician repairing defective Tire Sections"],
|
181 |
-
"Step 4": ["Delay in Bead set","Lack of raw material"],
|
182 |
-
"Step 5": ["Delay in Turnup","Lack of raw material"],
|
183 |
-
"Step 6": ["Person Repairing sidewall","Person rebuilding defective Tire Sections"],
|
184 |
-
"Step 7": ["Delay in sidewall stitching","Lack of raw material"],
|
185 |
-
"Step 8": ["No person available to load Carcass","No person available to collect tire"]
|
186 |
-
}
|
187 |
1. Selected Reason: [State the most likely reason from the given options]
|
188 |
2. Visual Evidence: [Describe specific visual cues that support your selection]
|
189 |
3. Reasoning: [Explain why this reason best matches the observed evidence]
|
190 |
4. Alternative Analysis: [Brief explanation of why other possible reasons are less likely]
|
191 |
-
Important: Base your analysis solely on visual evidence from the video. Focus on concrete, observable details rather than assumptions. Clearly state if no person or specific activity is observed.
|
192 |
-
"""
|
193 |
|
|
|
|
|
194 |
model, tokenizer = load_model()
|
195 |
|
196 |
def inference(video, step_number):
|
197 |
-
"""Analyzes video to predict
|
198 |
try:
|
199 |
if not video:
|
200 |
return "Please upload a video first."
|
201 |
|
202 |
-
|
203 |
-
|
|
|
204 |
response = predict(prompt, video, temperature, model, tokenizer)
|
205 |
|
206 |
return response
|
@@ -208,34 +143,38 @@ def inference(video, step_number):
|
|
208 |
return f"An error occurred during analysis: {str(e)}"
|
209 |
|
210 |
def create_interface():
|
211 |
-
"""Creates the Gradio interface for the Manufacturing Analysis System."""
|
212 |
with gr.Blocks() as demo:
|
213 |
gr.Markdown("""
|
214 |
-
# Manufacturing Analysis System
|
215 |
Upload a video of the manufacturing step and select the step number.
|
216 |
-
The system will analyze the video and
|
217 |
""")
|
218 |
|
219 |
with gr.Row():
|
220 |
with gr.Column():
|
221 |
video = gr.Video(label="Upload Manufacturing Video", sources=["upload"])
|
222 |
step_number = gr.Dropdown(
|
223 |
-
choices=
|
224 |
label="Manufacturing Step"
|
225 |
)
|
226 |
-
analyze_btn = gr.Button("Analyze", variant="primary")
|
227 |
|
228 |
with gr.Column():
|
229 |
output = gr.Textbox(label="Analysis Result", lines=10)
|
230 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
231 |
gr.Examples(
|
232 |
-
examples=
|
233 |
-
["7838_step2_2_eval.mp4", "Step 2"],
|
234 |
-
["7838_step6_2_eval.mp4", "Step 6"],
|
235 |
-
["7838_step8_1_eval.mp4", "Step 8"],
|
236 |
-
["7993_step6_3_eval.mp4", "Step 6"],
|
237 |
-
["7993_step8_3_eval.mp4", "Step 8"]
|
238 |
-
],
|
239 |
inputs=[video, step_number],
|
240 |
cache_examples=False
|
241 |
)
|
@@ -250,4 +189,4 @@ def create_interface():
|
|
250 |
|
251 |
if __name__ == "__main__":
|
252 |
demo = create_interface()
|
253 |
-
demo.queue().launch(share=True)
|
|
|
6 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
7 |
from transformers import BitsAndBytesConfig
|
8 |
|
9 |
+
|
10 |
MODEL_PATH = "THUDM/cogvlm2-llama3-caption"
|
11 |
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
|
12 |
TORCH_TYPE = torch.bfloat16 if torch.cuda.is_available() and torch.cuda.get_device_capability()[0] >= 8 else torch.float16
|
13 |
|
14 |
+
|
15 |
DELAY_REASONS = {
|
16 |
+
"Step 1": ["Delay in Bead Insertion","Lack of raw material"],
|
17 |
+
"Step 2": ["Inner Liner Adjustment by Technician","Person rebuilding defective Tire Sections"],
|
18 |
+
"Step 3": ["Manual Adjustment in Ply1 apply","Technician repairing defective Tire Sections"],
|
19 |
+
"Step 4": ["Delay in Bead set","Lack of raw material"],
|
20 |
+
"Step 5": ["Delay in Turnup","Lack of raw material"],
|
21 |
+
"Step 6": ["Person Repairing sidewall","Person rebuilding defective Tire Sections"],
|
22 |
+
"Step 7": ["Delay in sidewall stitching","Lack of raw material"],
|
23 |
+
"Step 8": ["No person available to load Carcass","No person available to collect tire"]
|
24 |
}
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
def load_video(video_data, strategy='chat'):
|
27 |
"""Loads and processes video data into a format suitable for model input."""
|
28 |
bridge.set_bridge('torch')
|
|
|
104 |
|
105 |
return response
|
106 |
|
107 |
+
def get_analysis_prompt(step_number, possible_reasons):
|
108 |
"""Constructs the prompt for analyzing delay reasons based on the selected step."""
|
109 |
+
return f"""You are an AI expert system specialized in analyzing manufacturing processes and identifying production delays in tire manufacturing. Your role is to accurately classify delay reasons based on visual evidence from production line footage.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
110 |
Task Context:
|
111 |
+
You are analyzing video footage from Step {step_number} of a tire manufacturing process where a delay has been detected. Your task is to determine the most likely cause of the delay from the following possible reasons:
|
112 |
+
{', '.join(possible_reasons)}
|
113 |
Required Analysis:
|
114 |
+
Carefully observe the video for visual cues indicating production interruption.
|
115 |
+
If no person is visible in any of the frames, the reason probably might be due to his absence.
|
116 |
+
If a person is visible in the video and is observed touching and modifying the layers of the tire, it means there is a issue with tyre being patched hence he is repairing it.
|
117 |
+
Compare observed evidence against each possible delay reason.
|
118 |
+
Select the most likely reason based on visual evidence.
|
|
|
119 |
Please provide your analysis in the following format:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
1. Selected Reason: [State the most likely reason from the given options]
|
121 |
2. Visual Evidence: [Describe specific visual cues that support your selection]
|
122 |
3. Reasoning: [Explain why this reason best matches the observed evidence]
|
123 |
4. Alternative Analysis: [Brief explanation of why other possible reasons are less likely]
|
124 |
+
Important: Base your analysis solely on visual evidence from the video. Focus on concrete, observable details rather than assumptions. Clearly state if no person or specific activity is observed."""
|
|
|
125 |
|
126 |
+
|
127 |
+
# Load model globally
|
128 |
model, tokenizer = load_model()
|
129 |
|
130 |
def inference(video, step_number):
|
131 |
+
"""Analyzes video to predict the most likely cause of delay in the selected manufacturing step."""
|
132 |
try:
|
133 |
if not video:
|
134 |
return "Please upload a video first."
|
135 |
|
136 |
+
possible_reasons = DELAY_REASONS[step_number]
|
137 |
+
prompt = get_analysis_prompt(step_number, possible_reasons)
|
138 |
+
temperature = 0.8
|
139 |
response = predict(prompt, video, temperature, model, tokenizer)
|
140 |
|
141 |
return response
|
|
|
143 |
return f"An error occurred during analysis: {str(e)}"
|
144 |
|
145 |
def create_interface():
|
146 |
+
"""Creates the Gradio interface for the Manufacturing Delay Analysis System with examples."""
|
147 |
with gr.Blocks() as demo:
|
148 |
gr.Markdown("""
|
149 |
+
# Manufacturing Delay Analysis System
|
150 |
Upload a video of the manufacturing step and select the step number.
|
151 |
+
The system will analyze the video and determine the most likely cause of delay.
|
152 |
""")
|
153 |
|
154 |
with gr.Row():
|
155 |
with gr.Column():
|
156 |
video = gr.Video(label="Upload Manufacturing Video", sources=["upload"])
|
157 |
step_number = gr.Dropdown(
|
158 |
+
choices=list(DELAY_REASONS.keys()),
|
159 |
label="Manufacturing Step"
|
160 |
)
|
161 |
+
analyze_btn = gr.Button("Analyze Delay", variant="primary")
|
162 |
|
163 |
with gr.Column():
|
164 |
output = gr.Textbox(label="Analysis Result", lines=10)
|
165 |
|
166 |
+
# Add examples
|
167 |
+
examples = [
|
168 |
+
["7838_step2_2_eval.mp4", "Step 2"],
|
169 |
+
["7838_step6_2_eval.mp4", "Step 6"],
|
170 |
+
["7838_step8_1_eval.mp4", "Step 8"],
|
171 |
+
["7993_step6_3_eval.mp4", "Step 6"],
|
172 |
+
["7993_step8_3_eval.mp4", "Step 8"]
|
173 |
+
|
174 |
+
]
|
175 |
+
|
176 |
gr.Examples(
|
177 |
+
examples=examples,
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
inputs=[video, step_number],
|
179 |
cache_examples=False
|
180 |
)
|
|
|
189 |
|
190 |
if __name__ == "__main__":
|
191 |
demo = create_interface()
|
192 |
+
demo.queue().launch(share=True)
|