VinitT commited on
Commit
2867c32
·
verified ·
1 Parent(s): 5b0226f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -15
app.py CHANGED
@@ -34,7 +34,7 @@ if uploaded_files:
34
  # Open the image
35
  image = Image.open(uploaded_file)
36
  # Resize image to reduce memory usage
37
- image = image.resize((512, 512))
38
  st.image(image, caption='Uploaded Image.', use_column_width=True)
39
  st.write("Generating description...")
40
 
@@ -55,7 +55,7 @@ if uploaded_files:
55
  # Convert the frame to an image
56
  image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
57
  # Resize image to reduce memory usage
58
- image = image.resize((512, 512))
59
  st.image(image, caption='First Frame of Uploaded Video.', use_column_width=True)
60
  st.write("Generating description...")
61
 
@@ -66,6 +66,11 @@ if uploaded_files:
66
  st.error("Unsupported file type.")
67
  continue
68
 
 
 
 
 
 
69
  messages = [
70
  {
71
  "role": "user",
@@ -94,19 +99,24 @@ if uploaded_files:
94
  inputs = inputs.to(device) # Ensure inputs are on the same device as the model
95
 
96
  # Inference: Generation of the output
97
- generated_ids = model.generate(**inputs, max_new_tokens=512)
98
- generated_ids_trimmed = [
99
- out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
100
- ]
101
- output_text = processor.batch_decode(
102
- generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
103
- )
104
-
105
- st.write("Description:")
106
- st.write(output_text[0])
107
-
108
- # Append the output text to the list
109
- all_output_texts.append(output_text[0])
 
 
 
 
 
110
 
111
  # Clear memory after processing each file
112
  del image, inputs, generated_ids, generated_ids_trimmed, output_text
 
34
  # Open the image
35
  image = Image.open(uploaded_file)
36
  # Resize image to reduce memory usage
37
+ image = image.resize((256, 256)) # Reduce size to save memory
38
  st.image(image, caption='Uploaded Image.', use_column_width=True)
39
  st.write("Generating description...")
40
 
 
55
  # Convert the frame to an image
56
  image = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
57
  # Resize image to reduce memory usage
58
+ image = image.resize((256, 256)) # Reduce size to save memory
59
  st.image(image, caption='First Frame of Uploaded Video.', use_column_width=True)
60
  st.write("Generating description...")
61
 
 
66
  st.error("Unsupported file type.")
67
  continue
68
 
69
+ # Ensure the image is loaded correctly
70
+ if image is None:
71
+ st.error("Failed to load the image.")
72
+ continue
73
+
74
  messages = [
75
  {
76
  "role": "user",
 
99
  inputs = inputs.to(device) # Ensure inputs are on the same device as the model
100
 
101
  # Inference: Generation of the output
102
+ try:
103
+ generated_ids = model.generate(**inputs, max_new_tokens=512)
104
+ generated_ids_trimmed = [
105
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
106
+ ]
107
+ output_text = processor.batch_decode(
108
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
109
+ )
110
+
111
+ st.write("Description:")
112
+ st.write(output_text[0])
113
+
114
+ # Append the output text to the list
115
+ all_output_texts.append(output_text[0])
116
+
117
+ except Exception as e:
118
+ st.error(f"Error during generation: {e}")
119
+ continue
120
 
121
  # Clear memory after processing each file
122
  del image, inputs, generated_ids, generated_ids_trimmed, output_text