DawnC commited on
Commit
150a33b
1 Parent(s): 45d344c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -124
app.py CHANGED
@@ -143,129 +143,6 @@ def preprocess_image(image):
143
  def get_akc_breeds_link():
144
  return "https://www.akc.org/dog-breeds/"
145
 
146
- # def predict(image):
147
- # try:
148
- # image_tensor = preprocess_image(image)
149
- # with torch.no_grad():
150
- # output = model(image_tensor)
151
- # if isinstance(output, tuple):
152
- # logits = output[0]
153
- # else:
154
- # logits = output
155
-
156
- # # 取得預測的top k結果
157
- # probabilities = F.softmax(logits, dim=1)
158
- # topk_probs, topk_indices = torch.topk(probabilities, k=3)
159
-
160
- # # 檢查最高的預測機率
161
- # top1_prob = topk_probs[0][0].item()
162
-
163
- # if top1_prob >= 0.5:
164
- # # 正確辨識時,返回該品種資訊
165
- # predicted = topk_indices[0][0]
166
- # breed = dog_breeds[predicted.item()]
167
- # description = get_dog_description(breed)
168
- # akc_link = get_akc_breeds_link()
169
-
170
- # if isinstance(description, dict):
171
- # description_str = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
172
- # else:
173
- # description_str = description
174
-
175
- # # 添加AKC連結
176
- # description_str += f"\n\n**Want to learn more about dog breeds?** [Visit the AKC dog breeds page]({akc_link}) and search for {breed} to find detailed information."
177
-
178
- # # 添加免責聲明
179
- # disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
180
- # "You may need to search for the specific breed on that page. "
181
- # "I am not responsible for the content on external sites. "
182
- # "Please refer to the AKC's terms of use and privacy policy.*")
183
- # description_str += disclaimer
184
-
185
- # return description_str
186
-
187
- # else:
188
- # # 不確定時,返回top 3的預測結果
189
- # topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
190
- # topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
191
-
192
- # # 用粗體返回品種和機率
193
- # topk_results = "\n\n".join([f"**{i+1}. {breed}** ({prob} confidence)" for i, (breed, prob) in enumerate(zip(topk_breeds, topk_probs_percent))])
194
-
195
- # # 提供說明
196
- # explanation = (
197
- # f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n{topk_results}\n\n"
198
- # "This can happen if the image quality is low or the breed is rare in the dataset. "
199
- # "Please try uploading a clearer image or a different angle of the dog. "
200
- # "For more accurate results, ensure the dog is the main subject of the photo."
201
- # )
202
-
203
- # return explanation
204
- # except Exception as e:
205
- # return f"An error occurred: {e}"
206
-
207
- # iface = gr.Interface(
208
- # fn=predict,
209
- # inputs=gr.Image(label="Upload a dog image", type="numpy"),
210
- # outputs=gr.Markdown(label="Prediction Results"),
211
- # title="<h1 style='font-family:Roboto; font-weight:bold; color:#2C3E50; text-align:center;'>🐶 Dog Breed Classifier 🔍</h1>",
212
- # article= 'For more details on this project and other work, feel free to visit my GitHub [Dog Breed Classifier](https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog%20Breed%20Classifier)',
213
- # description="<p style='font-family:Open Sans; color:#34495E; text-align:center;'>Upload a picture of a dog, and model will predict its breed, provide detailed information, and include an extra information link!</p>",
214
- # examples=['Border_Collie.jpg',
215
- # 'Golden_Retriever.jpeg',
216
- # 'Saint_Bernard.jpeg',
217
- # 'French_Bulldog.jpeg',
218
- # 'Samoyed.jpg'],
219
- # css = """
220
- # .container {
221
- # max-width: 900px;
222
- # margin: 0 auto;
223
- # padding: 20px;
224
- # background-color: rgba(255, 255, 255, 0.9);
225
- # border-radius: 15px;
226
- # box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
227
- # }
228
-
229
- # .gr-form {
230
- # display: flex;
231
- # flex-direction: column;
232
- # align-items: center;
233
- # }
234
-
235
- # .gr-box {
236
- # width: 100%;
237
- # max-width: 500px;
238
- # }
239
-
240
- # .output-markdown, .output-image {
241
- # margin-top: 20px;
242
- # padding: 15px;
243
- # background-color: #f5f5f5;
244
- # border-radius: 10px;
245
- # }
246
-
247
- # .examples {
248
- # display: flex;
249
- # justify-content: center;
250
- # flex-wrap: wrap;
251
- # gap: 10px;
252
- # margin-top: 20px;
253
- # }
254
-
255
- # .examples img {
256
- # width: 100px;
257
- # height: 100px;
258
- # object-fit: cover;
259
- # }
260
- # """,
261
- # theme='default')
262
-
263
-
264
-
265
- # # Launch the app
266
- # if __name__ == "__main__":
267
- # iface.launch()
268
-
269
  def predict(image):
270
  if image is None:
271
  return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
@@ -288,7 +165,7 @@ def predict(image):
288
  description = get_dog_description(breed)
289
  return format_description(description, breed), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
290
 
291
- elif top1_prob < 0.1:
292
  return ("The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.",
293
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
294
  else:
 
143
  def get_akc_breeds_link():
144
  return "https://www.akc.org/dog-breeds/"
145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
146
  def predict(image):
147
  if image is None:
148
  return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
 
165
  description = get_dog_description(breed)
166
  return format_description(description, breed), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
167
 
168
+ elif top1_prob < 0.2:
169
  return ("The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.",
170
  gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
171
  else: