arjunanand13 commited on
Commit
28bcecc
·
verified ·
1 Parent(s): 79e78be

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -11
app.py CHANGED
@@ -24,10 +24,10 @@ DELAY_REASONS = {
24
  }
25
 
26
  def load_video(video_data, strategy='chat'):
 
27
  bridge.set_bridge('torch')
28
  num_frames = 24
29
 
30
-
31
  if isinstance(video_data, str):
32
  decord_vr = VideoReader(video_data, ctx=cpu(0))
33
  else:
@@ -50,6 +50,7 @@ def load_video(video_data, strategy='chat'):
50
  return video_data
51
 
52
  def load_model():
 
53
  quantization_config = BitsAndBytesConfig(
54
  load_in_4bit=True,
55
  bnb_4bit_compute_dtype=TORCH_TYPE,
@@ -69,6 +70,7 @@ def load_model():
69
  return model, tokenizer
70
 
71
  def predict(prompt, video_data, temperature, model, tokenizer):
 
72
  video = load_video(video_data, strategy='chat')
73
 
74
  inputs = model.build_conversation_input_ids(
@@ -103,6 +105,7 @@ def predict(prompt, video_data, temperature, model, tokenizer):
103
  return response
104
 
105
  def get_analysis_prompt(step_number, possible_reasons):
 
106
  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.
107
  Task Context:
108
  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:
@@ -122,17 +125,13 @@ Important: Base your analysis solely on visual evidence from the video. Focus on
122
  model, tokenizer = load_model()
123
 
124
  def inference(video, step_number):
 
125
  try:
126
  if not video:
127
  return "Please upload a video first."
128
 
129
- # Get possible reasons for the selected step
130
  possible_reasons = DELAY_REASONS[step_number]
131
-
132
- # Generate the analysis prompt
133
  prompt = get_analysis_prompt(step_number, possible_reasons)
134
-
135
- # Get model prediction
136
  temperature = 0.8
137
  response = predict(prompt, video, temperature, model, tokenizer)
138
 
@@ -140,8 +139,8 @@ def inference(video, step_number):
140
  except Exception as e:
141
  return f"An error occurred during analysis: {str(e)}"
142
 
143
- # Gradio Interface
144
  def create_interface():
 
145
  with gr.Blocks() as demo:
146
  gr.Markdown("""
147
  # Manufacturing Delay Analysis System
@@ -151,18 +150,17 @@ def create_interface():
151
 
152
  with gr.Row():
153
  with gr.Column():
154
- video = gr.Video(label="Upload Manufacturing Video", sources=["upload"])
155
  step_number = gr.Dropdown(
156
  choices=list(DELAY_REASONS.keys()),
157
  label="Manufacturing Step",
158
- value="Step 1"
159
  )
160
  analyze_btn = gr.Button("Analyze Delay", variant="primary")
161
 
162
  with gr.Column():
163
  output = gr.Textbox(label="Analysis Result", lines=10)
164
 
165
- # Trigger analysis when button is clicked
166
  analyze_btn.click(
167
  fn=inference,
168
  inputs=[video, step_number],
@@ -173,4 +171,4 @@ def create_interface():
173
 
174
  if __name__ == "__main__":
175
  demo = create_interface()
176
- demo.launch(share=True) # Added share=True to create a public link
 
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')
29
  num_frames = 24
30
 
 
31
  if isinstance(video_data, str):
32
  decord_vr = VideoReader(video_data, ctx=cpu(0))
33
  else:
 
50
  return video_data
51
 
52
  def load_model():
53
+ """Loads the pre-trained model and tokenizer with quantization configurations."""
54
  quantization_config = BitsAndBytesConfig(
55
  load_in_4bit=True,
56
  bnb_4bit_compute_dtype=TORCH_TYPE,
 
70
  return model, tokenizer
71
 
72
  def predict(prompt, video_data, temperature, model, tokenizer):
73
+ """Generates predictions based on the video and textual prompt."""
74
  video = load_video(video_data, strategy='chat')
75
 
76
  inputs = model.build_conversation_input_ids(
 
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:
 
125
  model, tokenizer = load_model()
126
 
127
  def inference(video, step_number):
128
+ """Analyzes video to predict the most likely cause of delay in the selected manufacturing step."""
129
  try:
130
  if not video:
131
  return "Please upload a video first."
132
 
 
133
  possible_reasons = DELAY_REASONS[step_number]
 
 
134
  prompt = get_analysis_prompt(step_number, possible_reasons)
 
 
135
  temperature = 0.8
136
  response = predict(prompt, video, temperature, model, tokenizer)
137
 
 
139
  except Exception as e:
140
  return f"An error occurred during analysis: {str(e)}"
141
 
 
142
  def create_interface():
143
+ """Creates the Gradio interface for the Manufacturing Delay Analysis System."""
144
  with gr.Blocks() as demo:
145
  gr.Markdown("""
146
  # Manufacturing Delay Analysis System
 
150
 
151
  with gr.Row():
152
  with gr.Column():
153
+ video = gr.Video(label="Upload Manufacturing Video", sources=["upload"], value="delay_tyre.mp4")
154
  step_number = gr.Dropdown(
155
  choices=list(DELAY_REASONS.keys()),
156
  label="Manufacturing Step",
157
+ value="Step 8"
158
  )
159
  analyze_btn = gr.Button("Analyze Delay", variant="primary")
160
 
161
  with gr.Column():
162
  output = gr.Textbox(label="Analysis Result", lines=10)
163
 
 
164
  analyze_btn.click(
165
  fn=inference,
166
  inputs=[video, step_number],
 
171
 
172
  if __name__ == "__main__":
173
  demo = create_interface()
174
+ demo.launch(share=True)