IAMTFRMZA commited on
Commit
c6c3d34
Β·
verified Β·
1 Parent(s): 4adb56a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -92
app.py CHANGED
@@ -35,25 +35,6 @@ st.markdown("""
35
  .stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
36
  .stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
37
  .stChatMessage[data-testid="stChatMessage-assistant"] { background: #D6E9FE; color: #000000; }
38
- @keyframes bounceIn {
39
- 0% { transform: scale(0.7); opacity: 0; }
40
- 60% { transform: scale(1.1); opacity: 1; }
41
- 80% { transform: scale(0.95); }
42
- 100% { transform: scale(1); }
43
- }
44
- .carfind-logo {
45
- animation: bounceIn 0.6s ease-out;
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)
59
 
@@ -96,78 +77,38 @@ def display_chat_history():
96
  else:
97
  st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>Carfind Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
98
 
99
- # Tabs for Chat and Car Identification
100
- tab1, tab2 = st.tabs(["AI Chat", "What car is that?"])
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
- # Tab 1: AI Chat
103
- with tab1:
104
- input_col, clear_col = st.columns([9, 1])
105
- with input_col:
106
- user_input = st.chat_input("Type your message here...")
107
- with clear_col:
108
- if st.button("πŸ—‘οΈ", key="clear-chat", help="Clear Chat"):
109
- try:
110
- user_doc_ref = db.collection("users").document(user_id)
111
- for msg in user_doc_ref.collection("messages").stream():
112
- msg.reference.delete()
113
- user_doc_ref.delete()
114
- st.session_state.clear()
115
- st.rerun()
116
- except Exception as e:
117
- st.error(f"Failed to clear chat: {e}")
118
- thread_id = get_or_create_thread_id()
119
- display_chat_history()
120
- if user_input:
121
- client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
122
- save_message("user", user_input)
123
- with st.spinner("Thinking and typing... πŸ’­"):
124
- run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
125
- while True:
126
- run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
127
- if run_status.status == "completed":
128
- break
129
- time.sleep(1)
130
- messages_response = client.beta.threads.messages.list(thread_id=thread_id)
131
- latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
132
- assistant_message = latest_response.content[0].text.value
133
- save_message("assistant", assistant_message)
134
- time.sleep(0.5)
135
- st.rerun()
136
 
137
- # Tab 2: "What Car is That?"
138
- with tab2:
139
- uploaded_image = st.file_uploader("Upload an image of a car and let AI identify it for you", type=["jpg", "jpeg", "png"])
140
- if uploaded_image:
141
- col1, col2 = st.columns([1.2, 1.8])
142
- with col1:
143
- image = Image.open(uploaded_image)
144
- st.image(image, caption="Uploaded Image", use_container_width=True)
145
- with col2:
146
- try:
147
- # Create a new thread for image processing
148
- image_thread = client.beta.threads.create()
149
- file_response = client.files.create(file=uploaded_image, purpose="assistants")
150
- client.beta.threads.messages.create(
151
- thread_id=image_thread.id,
152
- role="user",
153
- content=[{
154
- "type": "image_file",
155
- "image_file": {"file_id": file_response.id}
156
- }, {
157
- "type": "text",
158
- "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."
159
- }]
160
- )
161
- run = client.beta.threads.runs.create(thread_id=image_thread.id, assistant_id=assistant_id)
162
- with st.spinner("πŸ” Analyzing image and identifying the car..."):
163
- while True:
164
- run_status = client.beta.threads.runs.retrieve(thread_id=image_thread.id, run_id=run.id)
165
- if run_status.status == "completed":
166
- break
167
- time.sleep(1)
168
- messages = client.beta.threads.messages.list(thread_id=image_thread.id)
169
- assistant_message = messages.data[0].content[0].text.value
170
- st.success("βœ… Identification Complete")
171
- st.markdown(f"<div class='car-spec-output'>{assistant_message}</div>", unsafe_allow_html=True)
172
- except Exception as e:
173
- st.error(f"❌ Error during image analysis: {str(e)}")
 
35
  .stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
36
  .stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
37
  .stChatMessage[data-testid="stChatMessage-assistant"] { background: #D6E9FE; color: #000000; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  </style>
39
  """, unsafe_allow_html=True)
40
 
 
77
  else:
78
  st.markdown(f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>Carfind Assistant:</strong> {data['content']}</div>", unsafe_allow_html=True)
79
 
80
+ # πŸš€ Main Assistant Chat Interface
81
+ input_col, clear_col = st.columns([9, 1])
82
+ with input_col:
83
+ user_input = st.chat_input("Type your message here...")
84
+ with clear_col:
85
+ if st.button("πŸ—‘οΈ", key="clear-chat", help="Clear Chat"):
86
+ try:
87
+ user_doc_ref = db.collection("users").document(user_id)
88
+ for msg in user_doc_ref.collection("messages").stream():
89
+ msg.reference.delete()
90
+ user_doc_ref.delete()
91
+ st.session_state.clear()
92
+ st.rerun()
93
+ except Exception as e:
94
+ st.error(f"Failed to clear chat: {e}")
95
 
96
+ thread_id = get_or_create_thread_id()
97
+ display_chat_history()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
+ if user_input:
100
+ client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
101
+ save_message("user", user_input)
102
+ with st.spinner("Thinking and typing... πŸ’­"):
103
+ run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
104
+ while True:
105
+ run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
106
+ if run_status.status == "completed":
107
+ break
108
+ time.sleep(1)
109
+ messages_response = client.beta.threads.messages.list(thread_id=thread_id)
110
+ latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
111
+ assistant_message = latest_response.content[0].text.value
112
+ save_message("assistant", assistant_message)
113
+ time.sleep(0.5)
114
+ st.rerun()