Update app.py
Browse files
app.py
CHANGED
|
@@ -1,16 +1,66 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import time
|
| 3 |
from datetime import datetime
|
| 4 |
import pytz
|
| 5 |
|
| 6 |
print("Starting application at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 7 |
|
| 8 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
def process_image(images, project_name, project_type):
|
|
|
|
| 10 |
if not images:
|
|
|
|
| 11 |
return "<p style='color: red;'>No images uploaded.</p>", "Pending", "", "", "0%"
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
# Gradio UI
|
| 16 |
with gr.Blocks(css="""
|
|
@@ -39,7 +89,7 @@ try:
|
|
| 39 |
demo.launch(share=True, debug=True)
|
| 40 |
print("Gradio interface launched successfully at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 41 |
while True:
|
| 42 |
-
time.sleep(10)
|
| 43 |
except Exception as e:
|
| 44 |
print(f"Failed to launch Gradio interface: {str(e)}")
|
| 45 |
raise
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
import time
|
| 4 |
from datetime import datetime
|
| 5 |
import pytz
|
| 6 |
|
| 7 |
print("Starting application at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 8 |
|
| 9 |
+
# Simplified mock AI model
|
| 10 |
+
def mock_ai_model(image):
|
| 11 |
+
print("Processing image in mock_ai_model at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 12 |
+
img = image.convert("RGB")
|
| 13 |
+
# Basic heuristic: assume interior work for now (adjustable)
|
| 14 |
+
milestone = "Interior Work"
|
| 15 |
+
percent_complete = 95
|
| 16 |
+
confidence_score = 0.92
|
| 17 |
+
print(f"Image processed: {milestone}, {percent_complete}%, Confidence: {confidence_score}")
|
| 18 |
+
return milestone, percent_complete, confidence_score
|
| 19 |
+
|
| 20 |
+
# Image processing function with debugging
|
| 21 |
def process_image(images, project_name, project_type):
|
| 22 |
+
print("Starting process_image at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 23 |
if not images:
|
| 24 |
+
print("No images uploaded")
|
| 25 |
return "<p style='color: red;'>No images uploaded.</p>", "Pending", "", "", "0%"
|
| 26 |
+
if not project_name:
|
| 27 |
+
print("No project name provided")
|
| 28 |
+
return "<p style='color: red;'>Project Name is required.</p>", "Pending", "", "", "0%"
|
| 29 |
+
|
| 30 |
+
results = []
|
| 31 |
+
all_percentages = []
|
| 32 |
+
all_milestones = set()
|
| 33 |
+
max_percent_complete = 0
|
| 34 |
+
dominant_milestone = None
|
| 35 |
+
|
| 36 |
+
for idx, image_path in enumerate(images):
|
| 37 |
+
try:
|
| 38 |
+
print(f"Processing image {idx+1} at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 39 |
+
img = Image.open(image_path)
|
| 40 |
+
milestone, percent_complete, confidence_score = mock_ai_model(img)
|
| 41 |
+
results.append(f"Image {idx+1}: {milestone} - {percent_complete}% completion (Confidence: {confidence_score})")
|
| 42 |
+
all_percentages.append(percent_complete)
|
| 43 |
+
all_milestones.add(milestone)
|
| 44 |
+
if percent_complete > max_percent_complete:
|
| 45 |
+
max_percent_complete = percent_complete
|
| 46 |
+
dominant_milestone = milestone
|
| 47 |
+
except Exception as e:
|
| 48 |
+
print(f"Error processing image {idx+1}: {str(e)}")
|
| 49 |
+
results.append(f"Image {idx+1}: Error - {str(e)}")
|
| 50 |
+
|
| 51 |
+
average_percent_complete = round(sum(all_percentages) / len(all_percentages), 2) if all_percentages else 0
|
| 52 |
+
all_milestones_str = ", ".join(all_milestones)
|
| 53 |
+
|
| 54 |
+
output_html = "<div style='text-align: left;'>"
|
| 55 |
+
output_html += "<h3>Processing Results:</h3><ul>"
|
| 56 |
+
for result in results:
|
| 57 |
+
output_html += f"<li>{result}</li>"
|
| 58 |
+
output_html += "</ul>"
|
| 59 |
+
output_html += f"<p><strong>Project Record Created:</strong> {project_name} - {dominant_milestone or 'No milestone'} (Highest: {max_percent_complete}%, Average: {average_percent_complete}%, Milestones: {all_milestones_str})</p>"
|
| 60 |
+
output_html += "</div>"
|
| 61 |
+
|
| 62 |
+
print("Finished process_image at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 63 |
+
return output_html, "Success", dominant_milestone or "", str(max([0.90] if not all_percentages else [c for _, c in [(m, 0.90) for m in all_milestones)])), f"{max_percent_complete}%"
|
| 64 |
|
| 65 |
# Gradio UI
|
| 66 |
with gr.Blocks(css="""
|
|
|
|
| 89 |
demo.launch(share=True, debug=True)
|
| 90 |
print("Gradio interface launched successfully at", datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S IST"))
|
| 91 |
while True:
|
| 92 |
+
time.sleep(10)
|
| 93 |
except Exception as e:
|
| 94 |
print(f"Failed to launch Gradio interface: {str(e)}")
|
| 95 |
raise
|