Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,7 +6,6 @@ import os
|
|
| 6 |
import uuid
|
| 7 |
import firebase_admin
|
| 8 |
from firebase_admin import credentials, firestore
|
| 9 |
-
import re
|
| 10 |
|
| 11 |
# π Firebase setup
|
| 12 |
if not firebase_admin._apps:
|
|
@@ -47,38 +46,13 @@ st.markdown("""
|
|
| 47 |
}
|
| 48 |
.car-spec-output {
|
| 49 |
font-family: "Segoe UI", sans-serif;
|
| 50 |
-
font-size:
|
| 51 |
background-color: #ffffff;
|
| 52 |
color: #003B6F;
|
| 53 |
-
padding:
|
| 54 |
border-radius: 10px;
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
line-height: 1.5;
|
| 58 |
-
}
|
| 59 |
-
.spec-table {
|
| 60 |
-
width: 100%;
|
| 61 |
-
border-collapse: collapse;
|
| 62 |
-
margin-top: 12px;
|
| 63 |
-
margin-bottom: 16px;
|
| 64 |
-
font-size: 10px;
|
| 65 |
-
}
|
| 66 |
-
.spec-table th, .spec-table td {
|
| 67 |
-
border: 1px solid #ddd;
|
| 68 |
-
padding: 6px 8px;
|
| 69 |
-
text-align: left;
|
| 70 |
-
}
|
| 71 |
-
.spec-table th {
|
| 72 |
-
background-color: #0071BC;
|
| 73 |
-
color: white;
|
| 74 |
-
}
|
| 75 |
-
.spec-table td {
|
| 76 |
-
background-color: #f7f9fc;
|
| 77 |
-
}
|
| 78 |
-
.icon {
|
| 79 |
-
width: 14px;
|
| 80 |
-
vertical-align: middle;
|
| 81 |
-
margin-right: 5px;
|
| 82 |
}
|
| 83 |
</style>
|
| 84 |
""", unsafe_allow_html=True)
|
|
@@ -119,32 +93,6 @@ def display_chat_history():
|
|
| 119 |
else:
|
| 120 |
st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>Carfind Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
|
| 121 |
|
| 122 |
-
def format_car_response(raw_text):
|
| 123 |
-
cleaned = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', raw_text)
|
| 124 |
-
cleaned = cleaned.replace("###", "<h4>").replace("\n", "<br>")
|
| 125 |
-
lines = raw_text.split("\n")
|
| 126 |
-
table_rows = []
|
| 127 |
-
intro = ""
|
| 128 |
-
for line in lines:
|
| 129 |
-
if "|" in line and not set(line.strip()) <= {"|", "-"}:
|
| 130 |
-
parts = [part.strip() for part in line.split("|") if part.strip()]
|
| 131 |
-
if len(parts) == 2:
|
| 132 |
-
label, value = parts
|
| 133 |
-
if value.lower() not in ["n/a", "null", "[specify mileage]", "[specify year]", "", "-"]:
|
| 134 |
-
table_rows.append((label, value))
|
| 135 |
-
elif not intro and line.strip():
|
| 136 |
-
intro = line.strip()
|
| 137 |
-
|
| 138 |
-
html = f"<div style='margin-bottom: 10px; font-size: 12px; color: #003B6F;'>{intro}</div>"
|
| 139 |
-
if len(table_rows) >= 3:
|
| 140 |
-
html += "<table class='spec-table'><tr><th>Specification</th><th>Details</th></tr>"
|
| 141 |
-
for label, value in table_rows:
|
| 142 |
-
html += f"<tr><td>{label}</td><td>{value}</td></tr>"
|
| 143 |
-
html += "</table>"
|
| 144 |
-
else:
|
| 145 |
-
html += "<div style='color: grey;'>No detailed specifications available.</div>"
|
| 146 |
-
return html
|
| 147 |
-
|
| 148 |
# Tabs
|
| 149 |
|
| 150 |
tab1, tab2 = st.tabs(["AI Chat", "What car is that?"])
|
|
@@ -199,7 +147,7 @@ with tab2:
|
|
| 199 |
role="user",
|
| 200 |
content=[
|
| 201 |
{"type": "image_file", "image_file": {"file_id": file_response.id}},
|
| 202 |
-
{"type": "text", "text": "
|
| 203 |
]
|
| 204 |
)
|
| 205 |
run = client.beta.threads.runs.create(thread_id=image_thread.id, assistant_id=assistant_id)
|
|
@@ -211,8 +159,7 @@ with tab2:
|
|
| 211 |
time.sleep(1)
|
| 212 |
messages = client.beta.threads.messages.list(thread_id=image_thread.id)
|
| 213 |
assistant_message = messages.data[0].content[0].text.value
|
| 214 |
-
formatted_html = format_car_response(assistant_message)
|
| 215 |
st.success("β
Identification Complete")
|
| 216 |
-
st.markdown(f"<div class='car-spec-output'>{
|
| 217 |
except Exception as e:
|
| 218 |
st.error(f"β Error during image analysis: {str(e)}")
|
|
|
|
| 6 |
import uuid
|
| 7 |
import firebase_admin
|
| 8 |
from firebase_admin import credentials, firestore
|
|
|
|
| 9 |
|
| 10 |
# π Firebase setup
|
| 11 |
if not firebase_admin._apps:
|
|
|
|
| 46 |
}
|
| 47 |
.car-spec-output {
|
| 48 |
font-family: "Segoe UI", sans-serif;
|
| 49 |
+
font-size: 13px;
|
| 50 |
background-color: #ffffff;
|
| 51 |
color: #003B6F;
|
| 52 |
+
padding: 20px;
|
| 53 |
border-radius: 10px;
|
| 54 |
+
border-left: 5px solid #0071BC;
|
| 55 |
+
line-height: 1.6;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
}
|
| 57 |
</style>
|
| 58 |
""", unsafe_allow_html=True)
|
|
|
|
| 93 |
else:
|
| 94 |
st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>Carfind Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
|
| 95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
# Tabs
|
| 97 |
|
| 98 |
tab1, tab2 = st.tabs(["AI Chat", "What car is that?"])
|
|
|
|
| 147 |
role="user",
|
| 148 |
content=[
|
| 149 |
{"type": "image_file", "image_file": {"file_id": file_response.id}},
|
| 150 |
+
{"type": "text", "text": "Identify this car image and respond using the markdown output format shown in your instructions β include all sections (π’ Identified Vehicle, π Details, π Overview, π Recommended For) without follow-ups or links."}
|
| 151 |
]
|
| 152 |
)
|
| 153 |
run = client.beta.threads.runs.create(thread_id=image_thread.id, assistant_id=assistant_id)
|
|
|
|
| 159 |
time.sleep(1)
|
| 160 |
messages = client.beta.threads.messages.list(thread_id=image_thread.id)
|
| 161 |
assistant_message = messages.data[0].content[0].text.value
|
|
|
|
| 162 |
st.success("β
Identification Complete")
|
| 163 |
+
st.markdown(f"<div class='car-spec-output'>{assistant_message}</div>", unsafe_allow_html=True)
|
| 164 |
except Exception as e:
|
| 165 |
st.error(f"β Error during image analysis: {str(e)}")
|