isunnyrock commited on
Commit
73f9898
1 Parent(s): df64bf1

chatbot updates

Browse files
Files changed (1) hide show
  1. app.py +57 -12
app.py CHANGED
@@ -56,8 +56,8 @@ print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
56
  def load_models():
57
  #OpenAI elements
58
  #secrets = toml.load(".vscode/streamlit/secrets.toml")
59
- #client_d = OpenAI(api_key = secrets["OPENAI_API_KEY"])
60
  client_d = OpenAI(api_key = st.secrets["OPENAI_API_KEY"])
 
61
 
62
  module_handle = "https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1"
63
  detector_d = hub.load(module_handle).signatures['default'];
@@ -350,6 +350,17 @@ def draw_boxes(image, boxes, class_names, scores, max_boxes=3, min_score=0.1):
350
  # ----------------------------------------------------------------------------------------------------//
351
  # Streamlit app
352
 
 
 
 
 
 
 
 
 
 
 
 
353
 
354
  def openai_remedy(searchval):
355
  completion = client.chat.completions.create(
@@ -365,7 +376,23 @@ def openai_remedy(searchval):
365
  #st.markdown(completion.choices[0].delta.content)
366
  return
367
 
368
- st.title("Image-based plant Disease Identification")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
369
 
370
  tab1, tab2, tab3 = st.tabs(["Home", "Solution", "Team"])
371
 
@@ -389,20 +416,30 @@ Early detection of plant diseases is paramount for farmers to protect their crop
389
 
390
  st.write("With more than 50% of the population in India still relying on agriculture and with the average farm sizes and incomes being very small, we believe that cost effective solutions for early detection and treatment solutions for disease could significantly improve the quality of produce and lives of farmers. With smartphones being ubiquitous, we believe providing solutions to farmers over a smartphone is the most penetrative form.")
391
 
 
 
392
 
 
 
393
 
394
 
 
395
  #Second Tab: Image upload and disease detection and remidy susgestions
396
  with tab2:
397
  # Load and display the image
398
- uploaded_file = st.file_uploader("Upload Leaf Image...", type=["jpg", "jpeg", "png"], key="uploader")
399
-
400
- if uploaded_file is not None:
401
- print("Image successfully uploaded!")
402
- # Read the uploaded image file
403
- #st.image(uploaded_file, caption='Uploaded Image', use_column_width=True,width=100)
404
- with st.columns(3)[0]:
405
- st.image(uploaded_file, caption='Image uploaded! Trying to detect objects in it...', width=300)
 
 
 
 
 
406
  image = Image.open(uploaded_file)
407
 
408
  image_for_drawing = image.copy()
@@ -436,7 +473,8 @@ with tab2:
436
  entity = detection_class_entities[idx].decode('utf-8')
437
 
438
  if "Plant" == entity:
439
- plant_detection_count = 1;
 
440
  plant_score = detection_scores[idx]
441
  st.write(f"Plant Probability score using Faster R-CNN Inception Resnet V2 Object detection model : {plant_score:.2%}")
442
  result1 = classify_image(image)
@@ -448,13 +486,20 @@ with tab2:
448
  newresult2 = newresult.replace("-"," ")
449
  st.markdown("Fetching disease management steps for " + ":red[" + newresult2 + "]... :eyes:")
450
  openai_remedy(newresult2)
 
 
451
 
452
  if plant_detection_count == 0:
453
  st.markdown("This is not a plant / leaf image")
454
 
455
  else:
456
  print("No file uploaded.")
457
-
 
 
 
 
 
458
  # Disclaimer
459
  st.write("""
460
  ### Disclaimer
 
56
  def load_models():
57
  #OpenAI elements
58
  #secrets = toml.load(".vscode/streamlit/secrets.toml")
 
59
  client_d = OpenAI(api_key = st.secrets["OPENAI_API_KEY"])
60
+ #client_chat = OpenAI(api_key = st.secrets["OPENAI_API_KEY"])
61
 
62
  module_handle = "https://tfhub.dev/google/faster_rcnn/openimages_v4/inception_resnet_v2/1"
63
  detector_d = hub.load(module_handle).signatures['default'];
 
350
  # ----------------------------------------------------------------------------------------------------//
351
  # Streamlit app
352
 
353
+ st.title("Image-based plant Disease Identification")
354
+
355
+ if 'plant_detection_count' not in st.session_state:
356
+ st.session_state['plant_detection_count'] = 0
357
+
358
+ if 'plant_detection_count' not in st.session_state:
359
+ st.session_state['plant_disease_class_value'] = ""
360
+
361
+ if 'chat_mode_on' not in st.session_state:
362
+ st.session_state['chat_mode_on'] = 0
363
+
364
 
365
  def openai_remedy(searchval):
366
  completion = client.chat.completions.create(
 
376
  #st.markdown(completion.choices[0].delta.content)
377
  return
378
 
379
+ def chat_help(plant_disease_class):
380
+ if "messages" not in st.session_state:
381
+ st.session_state["messages"] = [{"role": "assistant", "content": "How can I help you further with " + plant_disease_class + "?"}]
382
+
383
+ for msg in st.session_state.messages:
384
+ st.chat_message(msg["role"]).write(msg["content"])
385
+
386
+ if prompt := st.chat_input():
387
+ st.session_state.messages.append({"role": "system", "content": "Limit responses to " + plant_disease_class})
388
+ st.session_state.messages.append({"role": "user", "content": prompt})
389
+ st.chat_message("user").write(prompt)
390
+ response = client_d.chat.completions.create(model="gpt-4-turbo", messages=st.session_state.messages)
391
+ msg = response.choices[0].message.content
392
+ st.session_state.messages.append({"role": "assistant", "content": msg})
393
+ st.chat_message("assistant").write(msg)
394
+
395
+
396
 
397
  tab1, tab2, tab3 = st.tabs(["Home", "Solution", "Team"])
398
 
 
416
 
417
  st.write("With more than 50% of the population in India still relying on agriculture and with the average farm sizes and incomes being very small, we believe that cost effective solutions for early detection and treatment solutions for disease could significantly improve the quality of produce and lives of farmers. With smartphones being ubiquitous, we believe providing solutions to farmers over a smartphone is the most penetrative form.")
418
 
419
+ st.write("""
420
+ ### Training Dataset
421
 
422
+ Publicly available PlantVillage dataset was used. It consists of 54303 healthy and unhealthy leaf images divided into 38 categories by species and disease.
423
+ """)
424
 
425
 
426
+
427
  #Second Tab: Image upload and disease detection and remidy susgestions
428
  with tab2:
429
  # Load and display the image
430
+ #uploaded_file = st.file_uploader("Upload Leaf Image...", type=["jpg", "jpeg", "png"], key="uploader")
431
+
432
+ with st.form("my-form", clear_on_submit=True):
433
+ uploaded_file = st.file_uploader("Upload Leaf Image...", type=["jpg", "jpeg", "png"], key="uploader")
434
+ submitted = st.form_submit_button("UPLOAD!")
435
+
436
+ if uploaded_file is not None and st.session_state['chat_mode_on'] == 0:
437
+ #st.markdown("Image successfully uploaded!")
438
+ st.session_state['plant_detection_count'] = 0
439
+ st.session_state['plant_disease_class_value'] = ""
440
+
441
+ #with st.columns(3)[0]:
442
+ st.image(uploaded_file, caption='Image uploaded! Trying to detect objects in it...', width=300)
443
  image = Image.open(uploaded_file)
444
 
445
  image_for_drawing = image.copy()
 
473
  entity = detection_class_entities[idx].decode('utf-8')
474
 
475
  if "Plant" == entity:
476
+ plant_detection_count_p = 1
477
+ st.session_state['plant_detection_count'] = 1
478
  plant_score = detection_scores[idx]
479
  st.write(f"Plant Probability score using Faster R-CNN Inception Resnet V2 Object detection model : {plant_score:.2%}")
480
  result1 = classify_image(image)
 
486
  newresult2 = newresult.replace("-"," ")
487
  st.markdown("Fetching disease management steps for " + ":red[" + newresult2 + "]... :eyes:")
488
  openai_remedy(newresult2)
489
+ st.session_state['plant_disease_class_value'] = newresult2
490
+ uploaded_file = None
491
 
492
  if plant_detection_count == 0:
493
  st.markdown("This is not a plant / leaf image")
494
 
495
  else:
496
  print("No file uploaded.")
497
+
498
+ if st.session_state['plant_detection_count'] == 1 and st.session_state['plant_disease_class_value'] != "" :
499
+ st.session_state['chat_mode_on'] = 1
500
+ chat_help(st.session_state['plant_disease_class_value'])
501
+
502
+
503
  # Disclaimer
504
  st.write("""
505
  ### Disclaimer