chips commited on
Commit
a687c6a
Β·
1 Parent(s): 9acac5c

a lot of changes

Browse files
Files changed (1) hide show
  1. app.py +66 -27
app.py CHANGED
@@ -2,23 +2,22 @@ import gradio as gr
2
  import pandas as pd
3
  import numpy as np
4
  from typing import List, Dict
5
- from flask import Flask, send_from_directory
6
- import threading
7
  import os
8
 
9
- app = Flask(__name__, static_url_path='/static')
 
 
10
 
11
- @app.route('/static/thumbs/<path:filename>')
12
- def serve_image(filename):
13
- return send_from_directory('thumbs', filename)
14
-
15
- # Start Flask server in a separate thread
16
- def start_flask():
17
- app.run(port=7861, threaded=True)
18
-
19
- flask_thread = threading.Thread(target=start_flask)
20
- flask_thread.daemon = True
21
- flask_thread.start()
22
 
23
  class CSVViewer:
24
  def __init__(self, csv_path: str):
@@ -76,10 +75,10 @@ def create_page_view(page_num: int):
76
  product_code = page_data['product_info']['Product Code']
77
 
78
  # Find matching image file
79
- image_files = [f for f in os.listdir('thumbs') if f.startswith(product_code)]
80
  if image_files:
81
- # Use the Flask server URL for images
82
- image_path = f"http://localhost:7861/static/thumbs/{image_files[0]}"
83
  print(f"Found image: {image_path}")
84
  else:
85
  image_path = None
@@ -110,20 +109,60 @@ def create_page_view(page_num: int):
110
  ai_key = key + '_AI'
111
  ai_value = page_data['ai_data'].get(ai_key, '')
112
 
113
- # Determine comparison status
114
- if orig_value.lower() == ai_value.lower():
115
- status = "βœ“ Match"
116
- row_color = "#e8f5e9" # Light green background
117
- else:
118
- status = "βœ— Mismatch"
119
- row_color = "#ffebee" # Light red background
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
 
121
  # Add row with comparison
122
  html += f"<tr style='background-color: {row_color};'>"
123
  html += f"<td style='width: 150px;'>{key}</td>"
124
- html += f"<td>{orig_value}</td>"
125
- html += f"<td>{ai_value}</td>"
126
- html += f"<td style='color: {'#4CAF50' if orig_value.lower() == ai_value.lower() else '#f44336'}; font-weight: bold;'>{status}</td>"
127
  html += "</tr>"
128
 
129
  html += "</table></div>"
 
2
  import pandas as pd
3
  import numpy as np
4
  from typing import List, Dict
 
 
5
  import os
6
 
7
+ # Create static directory if it doesn't exist
8
+ if not os.path.exists('static'):
9
+ os.makedirs('static')
10
 
11
+ # Move thumbs directory into static if it exists
12
+ if os.path.exists('thumbs'):
13
+ import shutil
14
+ if not os.path.exists('static/thumbs'):
15
+ shutil.move('thumbs', 'static/')
16
+ else:
17
+ # If static/thumbs already exists, merge the contents
18
+ for file in os.listdir('thumbs'):
19
+ shutil.move(os.path.join('thumbs', file), 'static/thumbs/')
20
+ os.rmdir('thumbs')
 
21
 
22
  class CSVViewer:
23
  def __init__(self, csv_path: str):
 
75
  product_code = page_data['product_info']['Product Code']
76
 
77
  # Find matching image file
78
+ image_files = [f for f in os.listdir('static/thumbs') if f.startswith(product_code)]
79
  if image_files:
80
+ # Use Gradio's static file serving
81
+ image_path = f"/static/thumbs/{image_files[0]}"
82
  print(f"Found image: {image_path}")
83
  else:
84
  image_path = None
 
109
  ai_key = key + '_AI'
110
  ai_value = page_data['ai_data'].get(ai_key, '')
111
 
112
+ # Skip if AI value is empty
113
+ if not ai_value.strip():
114
+ continue
115
+
116
+ # Clean up values by removing brackets and quotes
117
+ orig_value = str(orig_value).strip('[]').strip('"').strip()
118
+ ai_value = str(ai_value).strip('[]').strip('"').strip()
119
+
120
+ # Handle array values
121
+ try:
122
+ # Try to parse as arrays
123
+ orig_items = [item.strip() for item in orig_value.split(',') if item.strip()]
124
+ ai_items = [item.strip() for item in ai_value.split(',') if item.strip()]
125
+
126
+ # Check if all original items are in AI items
127
+ if all(item.lower() in [x.lower() for x in ai_items] for item in orig_items):
128
+ if len(ai_items) > len(orig_items):
129
+ status = "βœ“ All and more"
130
+ status_color = "#4CAF50"
131
+ row_color = "#e8f5e9"
132
+ else:
133
+ status = "βœ“ Match"
134
+ status_color = "#4CAF50"
135
+ row_color = "#e8f5e9"
136
+ else:
137
+ status = "βœ— Mismatch"
138
+ status_color = "#f44336"
139
+ row_color = "#ffebee"
140
+
141
+ # Format the values nicely
142
+ orig_display = ', '.join(orig_items)
143
+ ai_display = ', '.join(ai_items)
144
+
145
+ except:
146
+ # Handle non-array values
147
+ if orig_value.lower() == ai_value.lower():
148
+ status = "βœ“ Match"
149
+ status_color = "#4CAF50"
150
+ row_color = "#e8f5e9"
151
+ orig_display = orig_value
152
+ ai_display = ai_value
153
+ else:
154
+ status = "βœ— Mismatch"
155
+ status_color = "#f44336"
156
+ row_color = "#ffebee"
157
+ orig_display = orig_value
158
+ ai_display = ai_value
159
 
160
  # Add row with comparison
161
  html += f"<tr style='background-color: {row_color};'>"
162
  html += f"<td style='width: 150px;'>{key}</td>"
163
+ html += f"<td>{orig_display}</td>"
164
+ html += f"<td>{ai_display}</td>"
165
+ html += f"<td style='color: {status_color}; font-weight: bold;'>{status}</td>"
166
  html += "</tr>"
167
 
168
  html += "</table></div>"