VishalD1234 commited on
Commit
cc79d65
·
verified ·
1 Parent(s): 79a8f2f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -145
app.py CHANGED
@@ -1,154 +1,162 @@
1
  import gradio as gr
 
2
  import numpy as np
3
- import random
4
-
5
- # import spaces #[uncomment to use ZeroGPU]
6
- from diffusers import DiffusionPipeline
7
  import torch
8
-
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
10
- model_repo_id = "stabilityai/sdxl-turbo" # Replace to the model you would like to use
11
-
12
- if torch.cuda.is_available():
13
- torch_dtype = torch.float16
14
- else:
15
- torch_dtype = torch.float32
16
-
17
- pipe = DiffusionPipeline.from_pretrained(model_repo_id, torch_dtype=torch_dtype)
18
- pipe = pipe.to(device)
19
-
20
- MAX_SEED = np.iinfo(np.int32).max
21
- MAX_IMAGE_SIZE = 1024
22
-
23
-
24
- # @spaces.GPU #[uncomment to use ZeroGPU]
25
- def infer(
26
- prompt,
27
- negative_prompt,
28
- seed,
29
- randomize_seed,
30
- width,
31
- height,
32
- guidance_scale,
33
- num_inference_steps,
34
- progress=gr.Progress(track_tqdm=True),
35
- ):
36
- if randomize_seed:
37
- seed = random.randint(0, MAX_SEED)
38
-
39
- generator = torch.Generator().manual_seed(seed)
40
-
41
- image = pipe(
42
- prompt=prompt,
43
- negative_prompt=negative_prompt,
44
- guidance_scale=guidance_scale,
45
- num_inference_steps=num_inference_steps,
46
- width=width,
47
- height=height,
48
- generator=generator,
49
- ).images[0]
50
-
51
- return image, seed
52
-
53
-
54
- examples = [
55
- "Astronaut in a jungle, cold color palette, muted colors, detailed, 8k",
56
- "An astronaut riding a green horse",
57
- "A delicious ceviche cheesecake slice",
58
- ]
59
-
60
- css = """
61
- #col-container {
62
- margin: 0 auto;
63
- max-width: 640px;
64
  }
65
- """
66
-
67
- with gr.Blocks(css=css) as demo:
68
- with gr.Column(elem_id="col-container"):
69
- gr.Markdown(" # Text-to-Image Gradio Template")
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  with gr.Row():
72
- prompt = gr.Text(
73
- label="Prompt",
74
- show_label=False,
75
- max_lines=1,
76
- placeholder="Enter your prompt",
77
- container=False,
78
- )
79
-
80
- run_button = gr.Button("Run", scale=0, variant="primary")
81
-
82
- result = gr.Image(label="Result", show_label=False)
83
-
84
- with gr.Accordion("Advanced Settings", open=False):
85
- negative_prompt = gr.Text(
86
- label="Negative prompt",
87
- max_lines=1,
88
- placeholder="Enter a negative prompt",
89
- visible=False,
90
- )
91
-
92
- seed = gr.Slider(
93
- label="Seed",
94
- minimum=0,
95
- maximum=MAX_SEED,
96
- step=1,
97
- value=0,
98
- )
99
-
100
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
101
-
102
- with gr.Row():
103
- width = gr.Slider(
104
- label="Width",
105
- minimum=256,
106
- maximum=MAX_IMAGE_SIZE,
107
- step=32,
108
- value=1024, # Replace with defaults that work for your model
109
- )
110
-
111
- height = gr.Slider(
112
- label="Height",
113
- minimum=256,
114
- maximum=MAX_IMAGE_SIZE,
115
- step=32,
116
- value=1024, # Replace with defaults that work for your model
117
- )
118
-
119
- with gr.Row():
120
- guidance_scale = gr.Slider(
121
- label="Guidance scale",
122
- minimum=0.0,
123
- maximum=10.0,
124
- step=0.1,
125
- value=0.0, # Replace with defaults that work for your model
126
  )
127
-
128
- num_inference_steps = gr.Slider(
129
- label="Number of inference steps",
130
- minimum=1,
131
- maximum=50,
132
- step=1,
133
- value=2, # Replace with defaults that work for your model
134
- )
135
-
136
- gr.Examples(examples=examples, inputs=[prompt])
137
- gr.on(
138
- triggers=[run_button.click, prompt.submit],
139
- fn=infer,
140
- inputs=[
141
- prompt,
142
- negative_prompt,
143
- seed,
144
- randomize_seed,
145
- width,
146
- height,
147
- guidance_scale,
148
- num_inference_steps,
149
- ],
150
- outputs=[result, seed],
151
- )
152
 
153
  if __name__ == "__main__":
154
- demo.launch()
 
 
1
  import gradio as gr
2
+ import io
3
  import numpy as np
 
 
 
 
4
  import torch
5
+ from decord import cpu, VideoReader, bridge
6
+ from transformers import AutoModelForCausalLM, AutoTokenizer
7
+ from transformers import BitsAndBytesConfig
8
+
9
+ # Adjust the model path to Qwen-2B
10
+ MODEL_PATH = "Qwen/Qwen2-VL-2B-Instruct"
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
+ DELAY_REASONS = {
15
+ "Step 1": ["No raw material available", "Person repatching the tire"],
16
+ "Step 2": ["Person repatching the tire", "Lack of raw material"],
17
+ "Step 3": ["Person repatching the tire", "Lack of raw material"],
18
+ "Step 4": ["Person repatching the tire", "Lack of raw material"],
19
+ "Step 5": ["Person repatching the tire", "Lack of raw material"],
20
+ "Step 6": ["Person repatching the tire", "Lack of raw material"],
21
+ "Step 7": ["Person repatching the tire", "Lack of raw material"],
22
+ "Step 8": ["No person available to collect tire", "Person repatching the tire"]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  }
 
 
 
 
 
24
 
25
+ def load_video(video_data, strategy='chat'):
26
+ """Loads and processes video data into a format suitable for model input."""
27
+ bridge.set_bridge('torch')
28
+ num_frames = 48
29
+
30
+ if isinstance(video_data, str):
31
+ decord_vr = VideoReader(video_data, ctx=cpu(0))
32
+ else:
33
+ decord_vr = VideoReader(io.BytesIO(video_data), ctx=cpu(0))
34
+
35
+ frame_id_list = []
36
+ total_frames = len(decord_vr)
37
+ timestamps = [i[0] for i in decord_vr.get_frame_timestamp(np.arange(total_frames))]
38
+ max_second = round(max(timestamps)) + 1
39
+
40
+ for second in range(max_second):
41
+ closest_num = min(timestamps, key=lambda x: abs(x - second))
42
+ index = timestamps.index(closest_num)
43
+ frame_id_list.append(index)
44
+ if len(frame_id_list) >= num_frames:
45
+ break
46
+
47
+ video_data = decord_vr.get_batch(frame_id_list)
48
+ video_data = video_data.permute(3, 0, 1, 2)
49
+ return video_data
50
+
51
+ def load_model():
52
+ """Loads the Qwen-2B model and tokenizer with quantization configurations."""
53
+ quantization_config = BitsAndBytesConfig(
54
+ load_in_4bit=True,
55
+ bnb_4bit_compute_dtype=TORCH_TYPE,
56
+ bnb_4bit_use_double_quant=True,
57
+ bnb_4bit_quant_type="nf4"
58
+ )
59
+
60
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
61
+ model = AutoModelForCausalLM.from_pretrained(
62
+ MODEL_PATH,
63
+ torch_dtype=TORCH_TYPE,
64
+ trust_remote_code=True,
65
+ quantization_config=quantization_config,
66
+ device_map="auto"
67
+ ).eval()
68
+
69
+ return model, tokenizer
70
+
71
+ def predict(prompt, video_data, temperature, model, tokenizer):
72
+ """Generates predictions based on the video and textual prompt."""
73
+ video = load_video(video_data, strategy='chat')
74
+
75
+ inputs = tokenizer(prompt, return_tensors="pt", padding=True, truncation=True)
76
+ inputs = {key: value.to(DEVICE) for key, value in inputs.items()}
77
+ inputs["images"] = [video.to(DEVICE).to(TORCH_TYPE)]
78
+
79
+ gen_kwargs = {
80
+ "max_new_tokens": 2048,
81
+ "pad_token_id": tokenizer.pad_token_id,
82
+ "top_k": 1,
83
+ "do_sample": False,
84
+ "top_p": 0.1,
85
+ "temperature": temperature,
86
+ }
87
+
88
+ with torch.no_grad():
89
+ outputs = model.generate(**inputs, **gen_kwargs)
90
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
91
+
92
+ return response
93
+
94
+ def get_analysis_prompt(step_number, possible_reasons):
95
+ """Constructs the prompt for analyzing delay reasons based on the selected step."""
96
+ 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.
97
+ Task Context:
98
+ 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:
99
+ {', '.join(possible_reasons)}
100
+ Required Analysis:
101
+ 1. Carefully observe the video for visual cues indicating production interruption.
102
+ 2. If no person is visible in any of the frames, the reason probably might be due to his absence
103
+ 3. If a person is visible in the video and is observed touching and modifying the layers of the tire, it means there is an issue with the tire being patched, hence they are repatching it.
104
+ 4. Compare observed evidence against each possible delay reason.
105
+ 5. Select the most likely reason based on visual evidence.
106
+ Please provide your analysis in the following format:
107
+ 1. Selected Reason: [State the most likely reason from the given options]
108
+ 2. Visual Evidence: [Describe specific visual cues that support your selection]
109
+ 3. Reasoning: [Explain why this reason best matches the observed evidence]
110
+ 4. Alternative Analysis: [Brief explanation of why other possible reasons are less likely]
111
+ 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."""
112
+
113
+ # Load model globally
114
+ model, tokenizer = load_model()
115
+
116
+ def inference(video, step_number):
117
+ """Analyzes video to predict the most likely cause of delay in the selected manufacturing step."""
118
+ try:
119
+ if not video:
120
+ return "Please upload a video first."
121
+
122
+ possible_reasons = DELAY_REASONS[step_number]
123
+ prompt = get_analysis_prompt(step_number, possible_reasons)
124
+ temperature = 0.8
125
+ response = predict(prompt, video, temperature, model, tokenizer)
126
+
127
+ return response
128
+ except Exception as e:
129
+ return f"An error occurred during analysis: {str(e)}"
130
+
131
+ def create_interface():
132
+ """Creates the Gradio interface for the Manufacturing Delay Analysis System with examples."""
133
+ with gr.Blocks() as demo:
134
+ gr.Markdown("""
135
+ # Manufacturing Delay Analysis System
136
+ Upload a video of the manufacturing step and select the step number.
137
+ The system will analyze the video and determine the most likely cause of delay.
138
+ """)
139
+
140
  with gr.Row():
141
+ with gr.Column():
142
+ video = gr.Video(label="Upload Manufacturing Video", sources=["upload"])
143
+ step_number = gr.Dropdown(
144
+ choices=list(DELAY_REASONS.keys()),
145
+ label="Manufacturing Step"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  )
147
+ analyze_btn = gr.Button("Analyze Delay", variant="primary")
148
+
149
+ with gr.Column():
150
+ output = gr.Textbox(label="Analysis Result", lines=10)
151
+
152
+ analyze_btn.click(
153
+ fn=inference,
154
+ inputs=[video, step_number],
155
+ outputs=[output]
156
+ )
157
+
158
+ return demo
 
 
 
 
 
 
 
 
 
 
 
 
 
159
 
160
  if __name__ == "__main__":
161
+ demo = create_interface()
162
+ demo.launch(share=True)