VishalD1234 commited on
Commit
95649a9
·
verified ·
1 Parent(s): 93b798f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -42
app.py CHANGED
@@ -6,22 +6,58 @@ from decord import cpu, VideoReader, bridge
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."""
@@ -104,37 +140,55 @@ def predict(prompt, video_data, temperature, model, tokenizer):
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
 
@@ -143,38 +197,34 @@ def inference(video, step_number):
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
  )
 
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
+ def get_step_info(step_number):
15
+ """Returns detailed information about a manufacturing step."""
16
+ step_details = {
17
+ 1: {
18
+ "Name": "Bead Insertion",
19
+ "Standard Time": "4 seconds",
20
+ "Analysis": "Observe the bead placement process. If the insertion exceeds 4 seconds, identify potential issues such as missing beads, technician errors, or machinery malfunction."
21
+ },
22
+ 2: {
23
+ "Name": "Inner Liner Apply",
24
+ "Standard Time": "4 seconds",
25
+ "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."
26
+ },
27
+ 3: {
28
+ "Name": "Ply1 Apply",
29
+ "Standard Time": "4 seconds",
30
+ "Analysis": "Determine if the technician is manually adjusting the first ply. Manual intervention might suggest improper ply placement or machine misalignment."
31
+ },
32
+ 4: {
33
+ "Name": "Bead Set",
34
+ "Standard Time": "8 seconds",
35
+ "Analysis": "Observe the bead setting process. Delays may result from bead misalignment, machine pauses, or lack of technician involvement."
36
+ },
37
+ 5: {
38
+ "Name": "Turnup",
39
+ "Standard Time": "4 seconds",
40
+ "Analysis": "Examine the turnup step for any technician involvement or pauses in machine operation. Reasons for delays might include material misalignment or equipment issues."
41
+ },
42
+ 6: {
43
+ "Name": "Sidewall Apply",
44
+ "Standard Time": "14 seconds",
45
+ "Analysis": "If a technician is repairing the sidewall, this may indicate material damage or improper initial application. Look for signs of excessive manual handling."
46
+ },
47
+ 7: {
48
+ "Name": "Sidewall Stitching",
49
+ "Standard Time": "5 seconds",
50
+ "Analysis": "Observe the stitching process. Delays could occur due to machine speed inconsistencies or technician intervention for correction."
51
+ },
52
+ 8: {
53
+ "Name": "Carcass Unload",
54
+ "Standard Time": "7 seconds",
55
+ "Analysis": "Ensure a technician is present for the carcass unload. If absent, note their return time and identify potential reasons for their absence."
56
+ }
57
+ }
58
+
59
+ return step_details.get(step_number, {"Error": "Invalid step number. Please provide a valid step number."})
60
+
61
 
62
  def load_video(video_data, strategy='chat'):
63
  """Loads and processes video data into a format suitable for model input."""
 
140
 
141
  return response
142
 
143
+ def get_analysis_prompt(step_number):
144
  """Constructs the prompt for analyzing delay reasons based on the selected step."""
145
+ step_info = get_step_info(step_number)
146
+
147
+ if "Error" in step_info:
148
+ return step_info["Error"]
149
+
150
+ step_name = step_info["Name"]
151
+ standard_time = step_info["Standard Time"]
152
+ analysis = step_info["Analysis"]
153
+
154
+ return f"""
155
+ 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.
156
  Task Context:
157
+ You are analyzing video footage from Step {step_number} of a tire manufacturing process where a delay has been detected. The step is called {step_name}, and its standard time is {standard_time}.
 
158
  Required Analysis:
159
+ Carefully observe the video for visual cues indicating production interruption.
160
+ - If no person is visible in any of the frames, the reason probably might be due to their absence.
161
+ - If a person is visible in the video and is observed touching and modifying the layers of the tire, it indicates an issue with tire patching, and the person might be repairing it.
162
+ - Compare observed evidence against the following possible delay reason:
163
+ - {analysis}
164
+
165
  Please provide your analysis in the following format:
166
+ DELAY_REASONS = {
167
+ "Step 1": ["Delay in Bead Insertion","Lack of raw material"],
168
+ "Step 2": ["Inner Liner Adjustment by Technician","Person rebuilding defective Tire Sections"],
169
+ "Step 3": ["Manual Adjustment in Ply1 apply","Technician repairing defective Tire Sections"],
170
+ "Step 4": ["Delay in Bead set","Lack of raw material"],
171
+ "Step 5": ["Delay in Turnup","Lack of raw material"],
172
+ "Step 6": ["Person Repairing sidewall","Person rebuilding defective Tire Sections"],
173
+ "Step 7": ["Delay in sidewall stitching","Lack of raw material"],
174
+ "Step 8": ["No person available to load Carcass","No person available to collect tire"]
175
+ }
176
  1. Selected Reason: [State the most likely reason from the given options]
177
  2. Visual Evidence: [Describe specific visual cues that support your selection]
178
  3. Reasoning: [Explain why this reason best matches the observed evidence]
179
  4. Alternative Analysis: [Brief explanation of why other possible reasons are less likely]
180
+ 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.
181
+ """
182
 
 
 
183
  model, tokenizer = load_model()
184
 
185
  def inference(video, step_number):
186
+ """Analyzes video to predict possible issues based on the manufacturing step."""
187
  try:
188
  if not video:
189
  return "Please upload a video first."
190
 
191
+ prompt = get_analysis_prompt(step_number)
 
192
  temperature = 0.8
193
  response = predict(prompt, video, temperature, model, tokenizer)
194
 
 
197
  return f"An error occurred during analysis: {str(e)}"
198
 
199
  def create_interface():
200
+ """Creates the Gradio interface for the Manufacturing Analysis System."""
201
  with gr.Blocks() as demo:
202
  gr.Markdown("""
203
+ # Manufacturing Analysis System
204
  Upload a video of the manufacturing step and select the step number.
205
+ The system will analyze the video and provide observations.
206
  """)
207
 
208
  with gr.Row():
209
  with gr.Column():
210
  video = gr.Video(label="Upload Manufacturing Video", sources=["upload"])
211
  step_number = gr.Dropdown(
212
+ choices=[f"Step {i}" for i in range(1, 9)],
213
  label="Manufacturing Step"
214
  )
215
+ analyze_btn = gr.Button("Analyze", variant="primary")
216
 
217
  with gr.Column():
218
  output = gr.Textbox(label="Analysis Result", lines=10)
219
 
 
 
 
 
 
 
 
 
 
 
220
  gr.Examples(
221
+ examples=[
222
+ ["7838_step2_2_eval.mp4", "Step 2"],
223
+ ["7838_step6_2_eval.mp4", "Step 6"],
224
+ ["7838_step8_1_eval.mp4", "Step 8"],
225
+ ["7993_step6_3_eval.mp4", "Step 6"],
226
+ ["7993_step8_3_eval.mp4", "Step 8"]
227
+ ],
228
  inputs=[video, step_number],
229
  cache_examples=False
230
  )