Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -92,8 +92,6 @@ st.markdown("""
|
|
| 92 |
</div>
|
| 93 |
""", unsafe_allow_html=True)
|
| 94 |
|
| 95 |
-
# Firebase Chat Functions
|
| 96 |
-
|
| 97 |
def get_or_create_thread_id():
|
| 98 |
doc_ref = db.collection("users").document(user_id)
|
| 99 |
doc = doc_ref.get()
|
|
@@ -101,10 +99,7 @@ def get_or_create_thread_id():
|
|
| 101 |
return doc.to_dict()["thread_id"]
|
| 102 |
else:
|
| 103 |
thread = client.beta.threads.create()
|
| 104 |
-
doc_ref.set({
|
| 105 |
-
"thread_id": thread.id,
|
| 106 |
-
"created_at": firestore.SERVER_TIMESTAMP
|
| 107 |
-
})
|
| 108 |
return thread.id
|
| 109 |
|
| 110 |
def save_message(role, content):
|
|
@@ -124,10 +119,36 @@ def display_chat_history():
|
|
| 124 |
else:
|
| 125 |
st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>Carfind Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
|
| 126 |
|
| 127 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
tab1, tab2 = st.tabs(["AI Chat", "What car is that?"])
|
| 129 |
|
| 130 |
-
# AI Chat Tab
|
| 131 |
with tab1:
|
| 132 |
input_col, clear_col = st.columns([9, 1])
|
| 133 |
with input_col:
|
|
@@ -145,7 +166,6 @@ with tab1:
|
|
| 145 |
st.error(f"Failed to clear chat: {e}")
|
| 146 |
thread_id = get_or_create_thread_id()
|
| 147 |
display_chat_history()
|
| 148 |
-
|
| 149 |
if user_input:
|
| 150 |
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
|
| 151 |
save_message("user", user_input)
|
|
@@ -163,7 +183,6 @@ with tab1:
|
|
| 163 |
time.sleep(0.5)
|
| 164 |
st.rerun()
|
| 165 |
|
| 166 |
-
# Car Image Tab
|
| 167 |
with tab2:
|
| 168 |
uploaded_image = st.file_uploader("Upload an image of a car and let Ai identify it for you", type=["jpg", "jpeg", "png"])
|
| 169 |
if uploaded_image:
|
|
@@ -180,7 +199,7 @@ with tab2:
|
|
| 180 |
role="user",
|
| 181 |
content=[
|
| 182 |
{"type": "image_file", "image_file": {"file_id": file_response.id}},
|
| 183 |
-
{"type": "text", "text": "Please identify this car from the image and include a clean
|
| 184 |
]
|
| 185 |
)
|
| 186 |
run = client.beta.threads.runs.create(thread_id=image_thread.id, assistant_id=assistant_id)
|
|
@@ -192,32 +211,8 @@ with tab2:
|
|
| 192 |
time.sleep(1)
|
| 193 |
messages = client.beta.threads.messages.list(thread_id=image_thread.id)
|
| 194 |
assistant_message = messages.data[0].content[0].text.value
|
| 195 |
-
|
| 196 |
-
# Clean base message
|
| 197 |
-
cleaned_message = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', assistant_message)
|
| 198 |
-
cleaned_message = cleaned_message.replace("###", "<h4 style='margin-bottom: 10px;'>").replace("\n", "<br>")
|
| 199 |
-
|
| 200 |
-
# Table builder (only if valid Markdown table found)
|
| 201 |
-
if '|' in cleaned_message and cleaned_message.count('|') > 2:
|
| 202 |
-
lines = cleaned_message.split('<br>')
|
| 203 |
-
html_table = "<table class='spec-table'>"
|
| 204 |
-
has_table = False
|
| 205 |
-
for line in lines:
|
| 206 |
-
if '|' in line and '---' not in line and 'Year' not in line and 'Mileage' not in line:
|
| 207 |
-
cells = line.strip('|').split('|')
|
| 208 |
-
if len(cells) >= 2:
|
| 209 |
-
has_table = True
|
| 210 |
-
row_html = '<tr>' + ''.join(f'<td>{cell.strip()}</td>' for cell in cells) + '</tr>'
|
| 211 |
-
html_table += row_html
|
| 212 |
-
elif '|' not in line and has_table:
|
| 213 |
-
html_table += "</table><p>" + line + "</p><table class='spec-table'>"
|
| 214 |
-
elif '|' not in line:
|
| 215 |
-
html_table += f"<p>{line}</p>"
|
| 216 |
-
html_table += "</table>"
|
| 217 |
-
cleaned_message = html_table if has_table else cleaned_message
|
| 218 |
-
|
| 219 |
st.success("✅ Identification Complete")
|
| 220 |
-
st.markdown(f"<div class='car-spec-output'>{
|
| 221 |
-
|
| 222 |
except Exception as e:
|
| 223 |
st.error(f"❌ Error during image analysis: {str(e)}")
|
|
|
|
| 92 |
</div>
|
| 93 |
""", unsafe_allow_html=True)
|
| 94 |
|
|
|
|
|
|
|
| 95 |
def get_or_create_thread_id():
|
| 96 |
doc_ref = db.collection("users").document(user_id)
|
| 97 |
doc = doc_ref.get()
|
|
|
|
| 99 |
return doc.to_dict()["thread_id"]
|
| 100 |
else:
|
| 101 |
thread = client.beta.threads.create()
|
| 102 |
+
doc_ref.set({"thread_id": thread.id, "created_at": firestore.SERVER_TIMESTAMP})
|
|
|
|
|
|
|
|
|
|
| 103 |
return thread.id
|
| 104 |
|
| 105 |
def save_message(role, content):
|
|
|
|
| 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?"])
|
| 151 |
|
|
|
|
| 152 |
with tab1:
|
| 153 |
input_col, clear_col = st.columns([9, 1])
|
| 154 |
with input_col:
|
|
|
|
| 166 |
st.error(f"Failed to clear chat: {e}")
|
| 167 |
thread_id = get_or_create_thread_id()
|
| 168 |
display_chat_history()
|
|
|
|
| 169 |
if user_input:
|
| 170 |
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
|
| 171 |
save_message("user", user_input)
|
|
|
|
| 183 |
time.sleep(0.5)
|
| 184 |
st.rerun()
|
| 185 |
|
|
|
|
| 186 |
with tab2:
|
| 187 |
uploaded_image = st.file_uploader("Upload an image of a car and let Ai identify it for you", type=["jpg", "jpeg", "png"])
|
| 188 |
if uploaded_image:
|
|
|
|
| 199 |
role="user",
|
| 200 |
content=[
|
| 201 |
{"type": "image_file", "image_file": {"file_id": file_response.id}},
|
| 202 |
+
{"type": "text", "text": "Please identify this car from the image and include a clean styled specifications as are available."}
|
| 203 |
]
|
| 204 |
)
|
| 205 |
run = client.beta.threads.runs.create(thread_id=image_thread.id, assistant_id=assistant_id)
|
|
|
|
| 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'>{formatted_html}</div>", unsafe_allow_html=True)
|
|
|
|
| 217 |
except Exception as e:
|
| 218 |
st.error(f"❌ Error during image analysis: {str(e)}")
|