MonsterMMORPG
commited on
Commit
•
90bfba4
1
Parent(s):
d5244db
Upload gradio_web_server.py
Browse files- gradio_web_server.py +42 -22
gradio_web_server.py
CHANGED
@@ -155,28 +155,48 @@ def add_text(state, text, image, image_process_mode, request: gr.Request):
|
|
155 |
|
156 |
|
157 |
def batch_process_images(folder_path, textbox, model_selector, temperature, top_p, max_output_tokens, request: gr.Request):
|
158 |
-
print("
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
|
181 |
return "Batch processing completed."
|
182 |
|
|
|
155 |
|
156 |
|
157 |
def batch_process_images(folder_path, textbox, model_selector, temperature, top_p, max_output_tokens, request: gr.Request):
|
158 |
+
print("Starting batch processing of images")
|
159 |
+
|
160 |
+
# Initialize counters and timer
|
161 |
+
image_files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
|
162 |
+
total_images = len(image_files)
|
163 |
+
processed_images = 0
|
164 |
+
total_processing_time = 0
|
165 |
+
|
166 |
+
# Process each image file
|
167 |
+
for filename in image_files:
|
168 |
+
image_path = os.path.join(folder_path, filename)
|
169 |
+
start_time = time.time()
|
170 |
+
|
171 |
+
with Image.open(image_path) as image:
|
172 |
+
state = default_conversation.copy()
|
173 |
+
state, _, _, _, _, _, _, _, _ = add_text(state, textbox, image, "Default", request)
|
174 |
+
|
175 |
+
# Call http_bot and iterate over the generator
|
176 |
+
response_text = ""
|
177 |
+
for state_update in http_bot(state, model_selector, temperature, top_p, max_output_tokens, request):
|
178 |
+
# Update state and extract response text
|
179 |
+
state, chatbot_output, *_ = state_update
|
180 |
+
response_text = chatbot_output
|
181 |
+
|
182 |
+
# Save the final response to a file
|
183 |
+
try:
|
184 |
+
with open(os.path.splitext(image_path)[0] + '.txt', 'w') as f:
|
185 |
+
f.write(response_text[0][1])
|
186 |
+
except Exception as e:
|
187 |
+
print(f"An error occurred: {e}")
|
188 |
+
|
189 |
+
# Update processing information
|
190 |
+
processed_images += 1
|
191 |
+
processing_time = time.time() - start_time
|
192 |
+
total_processing_time += processing_time
|
193 |
+
average_processing_time = total_processing_time / processed_images
|
194 |
+
images_left = total_images - processed_images
|
195 |
+
eta_seconds = average_processing_time * images_left
|
196 |
+
eta = datetime.timedelta(seconds=int(eta_seconds))
|
197 |
+
|
198 |
+
# Display progress information
|
199 |
+
print(f"{processed_images}/{total_images} images processed, {images_left} left, average process time {average_processing_time :.2f} seconds, ETA: {str(eta)}")
|
200 |
|
201 |
return "Batch processing completed."
|
202 |
|