Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -75,92 +75,6 @@ def save_image(image, incident_type):
|
|
| 75 |
|
| 76 |
# Function to create Freshdesk ticket
|
| 77 |
|
| 78 |
-
# def create_freshdesk_ticket(incident_type, confidence_score, img):
|
| 79 |
-
# # Save the image to the appropriate directory and get its local path
|
| 80 |
-
# image_url = None
|
| 81 |
-
# image_path = None
|
| 82 |
-
# if incident_type.lower() == "fire incident":
|
| 83 |
-
# image_url = save_image(img, "fire")
|
| 84 |
-
# image_path = os.path.join(MEDIA_DIR, "fire", os.path.basename(image_url.split("/")[-1]))
|
| 85 |
-
# elif incident_type.lower() == "accident incident":
|
| 86 |
-
# image_url = save_image(img, "accidents")
|
| 87 |
-
# image_path = os.path.join(MEDIA_DIR, "accidents", os.path.basename(image_url.split("/")[-1]))
|
| 88 |
-
# elif incident_type.lower() == "fire and accident incident":
|
| 89 |
-
# fire_url = save_image(img, "fire")
|
| 90 |
-
# accident_url = save_image(img, "accidents")
|
| 91 |
-
# image_url = fire_url or accident_url
|
| 92 |
-
# image_path = os.path.join(MEDIA_DIR, "fire" if fire_url else "accidents", os.path.basename(image_url.split("/")[-1]))
|
| 93 |
-
|
| 94 |
-
# # Shortened subject
|
| 95 |
-
# subject = f"""{incident_type} Detected - Confidence: {confidence_score*100:.1f}%
|
| 96 |
-
# Details:
|
| 97 |
-
# 1. Address: Hafeez Centre, Gulberg, Lahore
|
| 98 |
-
# 2. Image URL: {image_url or 'https://example.com/roboi.jpg'}
|
| 99 |
-
# """
|
| 100 |
-
|
| 101 |
-
# # Detailed description
|
| 102 |
-
# description = f"""
|
| 103 |
-
# {incident_type} is critical.
|
| 104 |
-
# Details:
|
| 105 |
-
# 1. Address: 123 Main Street, Lahore
|
| 106 |
-
# 2. Phone: 923013225853
|
| 107 |
-
# 3. Confidence Score: {confidence_score*100:.1f}%
|
| 108 |
-
# 4. Image URL: {image_url or 'https://example.com/roboi.jpg'}
|
| 109 |
-
# 5. Incident Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 110 |
-
# """
|
| 111 |
-
|
| 112 |
-
# ticket_data = {
|
| 113 |
-
# "email": "safe.city@example.com",
|
| 114 |
-
# "subject": subject,
|
| 115 |
-
# "description": description,
|
| 116 |
-
# "priority": 4, # Urgent
|
| 117 |
-
# "status": 2 # Open
|
| 118 |
-
# }
|
| 119 |
-
|
| 120 |
-
# # Create ticket
|
| 121 |
-
# url = f"https://{FRESHDESK_DOMAIN}/api/v2/tickets"
|
| 122 |
-
# headers = {"Content-Type": "application/json"}
|
| 123 |
-
|
| 124 |
-
# response = requests.post(
|
| 125 |
-
# url,
|
| 126 |
-
# auth=(API_KEY, "X"),
|
| 127 |
-
# headers=headers,
|
| 128 |
-
# data=json.dumps(ticket_data)
|
| 129 |
-
# )
|
| 130 |
-
|
| 131 |
-
# if response.status_code == 201:
|
| 132 |
-
# ticket = response.json()
|
| 133 |
-
# ticket_id = ticket.get('id')
|
| 134 |
-
# print(f"✅ Ticket created successfully: Ticket ID {ticket_id}")
|
| 135 |
-
# print(json.dumps(ticket, indent=2))
|
| 136 |
-
|
| 137 |
-
# # Attach image to ticket if image_path exists
|
| 138 |
-
# if image_path and os.path.exists(image_path):
|
| 139 |
-
# attachment_url = f"https://{FRESHDESK_DOMAIN}/api/v2/tickets/{ticket_id}/attachments"
|
| 140 |
-
# try:
|
| 141 |
-
# with open(image_path, 'rb') as f:
|
| 142 |
-
# files = {'attachments[]': (os.path.basename(image_path), f, 'image/jpeg')}
|
| 143 |
-
# # Do not set Content-Type header; let requests handle it
|
| 144 |
-
# attachment_response = requests.post(
|
| 145 |
-
# attachment_url,
|
| 146 |
-
# auth=(API_KEY, "X"),
|
| 147 |
-
# files=files
|
| 148 |
-
# )
|
| 149 |
-
# if attachment_response.status_code == 201:
|
| 150 |
-
# print(f"✅ Image attached to ticket {ticket_id}")
|
| 151 |
-
# else:
|
| 152 |
-
# print(f"❌ Failed to attach image: {attachment_response.status_code} - {attachment_response.text}")
|
| 153 |
-
# except Exception as e:
|
| 154 |
-
# print(f"❌ Error accessing image file {image_path}: {str(e)}")
|
| 155 |
-
# else:
|
| 156 |
-
# print(f"❌ Image file not found: {image_path}")
|
| 157 |
-
|
| 158 |
-
# return f"Ticket created for {incident_type} with ID {ticket_id}"
|
| 159 |
-
# else:
|
| 160 |
-
# print(f"❌ Failed to create ticket: {response.status_code} - {response.text}")
|
| 161 |
-
# return f"Failed to create ticket for {incident_type}: {response.status_code} - {response.text}"
|
| 162 |
-
|
| 163 |
-
|
| 164 |
def create_freshdesk_ticket(incident_type, confidence_score, img):
|
| 165 |
# Save the image to the appropriate directory and get its local path
|
| 166 |
image_url = None
|
|
@@ -189,14 +103,14 @@ Details:
|
|
| 189 |
{incident_type} is critical.
|
| 190 |
Details:
|
| 191 |
1. Address: 123 Main Street, Lahore
|
| 192 |
-
2.
|
| 193 |
-
3.
|
| 194 |
-
4.
|
|
|
|
| 195 |
"""
|
| 196 |
|
| 197 |
ticket_data = {
|
| 198 |
"email": "safe.city@example.com",
|
| 199 |
-
"phone": "923013225853", # Added phone number as a separate field
|
| 200 |
"subject": subject,
|
| 201 |
"description": description,
|
| 202 |
"priority": 4, # Urgent
|
|
@@ -245,6 +159,8 @@ Details:
|
|
| 245 |
else:
|
| 246 |
print(f"❌ Failed to create ticket: {response.status_code} - {response.text}")
|
| 247 |
return f"Failed to create ticket for {incident_type}: {response.status_code} - {response.text}"
|
|
|
|
|
|
|
| 248 |
# Image inference function
|
| 249 |
def detect_image(image):
|
| 250 |
try:
|
|
|
|
| 75 |
|
| 76 |
# Function to create Freshdesk ticket
|
| 77 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
def create_freshdesk_ticket(incident_type, confidence_score, img):
|
| 79 |
# Save the image to the appropriate directory and get its local path
|
| 80 |
image_url = None
|
|
|
|
| 103 |
{incident_type} is critical.
|
| 104 |
Details:
|
| 105 |
1. Address: 123 Main Street, Lahore
|
| 106 |
+
2. Phone: 923013225853
|
| 107 |
+
3. Confidence Score: {confidence_score*100:.1f}%
|
| 108 |
+
4. Image URL: {image_url or 'https://example.com/roboi.jpg'}
|
| 109 |
+
5. Incident Time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
|
| 110 |
"""
|
| 111 |
|
| 112 |
ticket_data = {
|
| 113 |
"email": "safe.city@example.com",
|
|
|
|
| 114 |
"subject": subject,
|
| 115 |
"description": description,
|
| 116 |
"priority": 4, # Urgent
|
|
|
|
| 159 |
else:
|
| 160 |
print(f"❌ Failed to create ticket: {response.status_code} - {response.text}")
|
| 161 |
return f"Failed to create ticket for {incident_type}: {response.status_code} - {response.text}"
|
| 162 |
+
|
| 163 |
+
|
| 164 |
# Image inference function
|
| 165 |
def detect_image(image):
|
| 166 |
try:
|