chips commited on
Commit
9acac5c
·
1 Parent(s): 0fa8876

a lot of changes

Browse files
Files changed (2) hide show
  1. app.py +63 -34
  2. image.jpg +0 -0
app.py CHANGED
@@ -3,8 +3,23 @@ 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 os
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  class CSVViewer:
9
  def __init__(self, csv_path: str):
10
  self.df = pd.read_csv(csv_path)
@@ -50,7 +65,7 @@ class CSVViewer:
50
  'total_pages': self.total_pages
51
  }
52
 
53
- def create_page_view(page_num: int) -> str:
54
  viewer = CSVViewer('attr.csv')
55
  page_data = viewer.get_page_data(page_num)
56
 
@@ -63,29 +78,56 @@ def create_page_view(page_num: int) -> str:
63
  # Find matching image file
64
  image_files = [f for f in os.listdir('thumbs') if f.startswith(product_code)]
65
  if image_files:
66
- # Use relative path for Gradio
67
- image_path = f"/static/thumbs/{image_files[0]}"
 
68
  else:
69
  image_path = None
70
 
71
- # Product information header
72
  html = f"<h3>Page {page_data['page_num']} of {page_data['total_pages']}</h3>"
73
  html += "<div style='margin-bottom: 20px; display: flex; gap: 20px;'>"
74
 
75
- # Image section
76
- if image_path:
77
- html += f"<div style='flex: 0 0 300px;'>"
78
- html += f"<img src='{image_path}' alt='Product Image' style='max-width: 100%; border-radius: 5px;'>"
79
- html += "</div>"
80
-
81
  # Product info section
82
  html += "<div>"
 
 
83
  for key, value in page_data['product_info'].items():
84
  html += f"<p><strong>{key}:</strong> {value}</p>"
85
  html += "</div></div>"
86
 
87
  html += "<div style='display: flex; gap: 20px;'>"
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  # Original data section
90
  html += "<div style='flex: 1; border: 1px solid #ddd; padding: 15px; border-radius: 5px;'>"
91
  html += "<h4>Original Data</h4>"
@@ -106,29 +148,16 @@ def create_page_view(page_num: int) -> str:
106
  return html
107
 
108
  # Create Gradio interface
109
- demo = gr.Interface(
110
- fn=create_page_view,
111
- inputs=gr.Slider(0, 100, step=1, label="Page Number"),
112
- outputs="html",
113
- title="CSV Data Viewer",
114
- description="Navigate through CSV data pages to compare original and AI-predicted values"
115
- )
116
-
117
- # Create Flask app
118
- app = Flask(__name__, static_folder='.', static_url_path='')
119
-
120
- @app.route('/static/thumbs/<path:filename>')
121
- def serve_image(filename):
122
- return send_from_directory('thumbs', filename)
123
 
124
- # Create Gradio interface
125
- demo = gr.Interface(
126
- fn=create_page_view,
127
- inputs=gr.Slider(0, 100, step=1, label="Page Number"),
128
- outputs="html",
129
- title="CSV Data Viewer",
130
- description="Navigate through CSV data pages to compare original and AI-predicted values"
131
- )
132
 
133
- # Launch Gradio interface with Flask server
134
- demo.launch(server_name="0.0.0.0", server_port=7860)
 
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):
25
  self.df = pd.read_csv(csv_path)
 
65
  'total_pages': self.total_pages
66
  }
67
 
68
+ def create_page_view(page_num: int):
69
  viewer = CSVViewer('attr.csv')
70
  page_data = viewer.get_page_data(page_num)
71
 
 
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
86
 
87
+ # Create HTML content
88
  html = f"<h3>Page {page_data['page_num']} of {page_data['total_pages']}</h3>"
89
  html += "<div style='margin-bottom: 20px; display: flex; gap: 20px;'>"
90
 
 
 
 
 
 
 
91
  # Product info section
92
  html += "<div>"
93
+ if image_path:
94
+ html += f"<img src='{image_path}' alt='Product Image' style='max-width: 100%; border-radius: 5px;'>"
95
  for key, value in page_data['product_info'].items():
96
  html += f"<p><strong>{key}:</strong> {value}</p>"
97
  html += "</div></div>"
98
 
99
  html += "<div style='display: flex; gap: 20px;'>"
100
 
101
+ # Comparison section
102
+ html += "<div style='flex: 1; border: 1px solid #ddd; padding: 15px; border-radius: 5px;'>"
103
+ html += "<h4>Comparison</h4>"
104
+ html += "<table style='width: 100%;'>"
105
+ html += "<tr style='background-color: #f5f5f5;'><th>Attribute</th><th>Original</th><th>AI Prediction</th><th>Status</th></tr>"
106
+
107
+ # Add comparison rows
108
+ for key, orig_value in page_data['original_data'].items():
109
+ # Find matching AI prediction
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>"
130
+
131
  # Original data section
132
  html += "<div style='flex: 1; border: 1px solid #ddd; padding: 15px; border-radius: 5px;'>"
133
  html += "<h4>Original Data</h4>"
 
148
  return html
149
 
150
  # Create Gradio interface
151
+ demo = gr.Blocks()
152
+ with demo:
153
+ gr.Markdown("# CSV Data Viewer")
154
+ gr.Markdown("Navigate through CSV data pages to compare original and AI-predicted values")
155
+
156
+ page_num = gr.Slider(0, 100, step=1, label="Page Number")
157
+ html_output = gr.HTML()
158
+
159
+ page_num.change(create_page_view, [page_num], [html_output])
 
 
 
 
 
160
 
161
+ # Launch Gradio interface
162
+ demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
 
 
 
 
 
163
 
 
 
image.jpg ADDED