Spaces:
Runtime error
Runtime error
File size: 13,233 Bytes
0b9e99a 6153b83 a9442f2 1fc962a a9442f2 6153b83 0b9e99a e44aa06 0b9e99a a9442f2 0b9e99a a9442f2 0b9e99a 27fd04a 0b9e99a 5b50c97 f6b3153 d2d37d9 f6b3153 d2d37d9 f6b3153 a9442f2 0b9e99a f6b3153 d2d37d9 f288f5a e44aa06 f288f5a e44aa06 f288f5a f6b3153 e6bcf99 a9442f2 d2d37d9 6153b83 e6bcf99 d2d37d9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
import base64
import gradio as gr
import requests
import google.generativeai as genai
import json
import os
genai.configure(api_key=os.getenv("genai"))
oapi_key= os.getenv("openai")
def encode_image(image_file):
with open(image_file.name, "rb") as img_file:
return base64.b64encode(img_file.read()).decode('utf-8')
def send_to_openai(image_file):
base64_image = encode_image(image_file)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {oapi_key}"
}
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Answer in only one of the following options - Leaf , Sheath , Question - You are given a picture of Rice Paddy which part of the Paddy Crop is prominently visible , Leaf should be the whole leaf , and Sheath Can be a little part of the Leaf and Should Show the Stem and Maybe Grains "
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
# Extract words from the assistant's response
assistant_response = response.json()['choices'][0]['message']['content']
words = assistant_response.split('\n')
checkresponse_lower = [word.lower() for word in words]
if "leaf" in checkresponse_lower:
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Answer in three words only, does the image uploaded have a healthy rice leaf - Yes or No , does the image uploaded have a rice leaf with Major (not small) circular spots - Yes or No , does the image uploaded have a rice leaf have a major yellowish discoloration in some areas (ignore spots) - Yes or No , DO NOT RESPOND IN MORE THAN THREE WORDS and ANSWER WITH COMMA IN THE MIDDLE OF THE WORDS WITH NO FULLSTOP "
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
elif "sheath" in checkresponse_lower:
payload = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "ANSWER IN ONLY ONE WORD , does the sheath part of the paddy in the image have sheath rot ANSWER IN YES OR NO , NO FULLSTOP "
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 300
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
assistant_response = response.json()['choices'][0]['message']['content']
recognition = assistant_response.split('\n')
result_string = ' '.join(words + recognition)
generation_config = {
"temperature": 0.9,
"top_p": 1,
"top_k": 1,
"max_output_tokens": 2048,
}
safety_settings = [
{
"category": "HARM_CATEGORY_HARASSMENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_HATE_SPEECH",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
{
"category": "HARM_CATEGORY_DANGEROUS_CONTENT",
"threshold": "BLOCK_MEDIUM_AND_ABOVE"
},
]
model = "gemini-1.0-pro"
prompt_parts = [
"You are a Rice Paddy Agricultural Scientist , You will be given the state of the paddy(a picture description ) and you will have to advice on the correct remedyInput - Sheath Yes Meaning - The Farmer has shown you a picture of Sheath part of the paddy and it is suffering from Sheath Rot , advice the farmer on how to fix thisInput - Sheath No Meaning - The Farmer has shown you a picture of Sheath part of the paddy and it is healthy ,congratulate and advice farmer on how to maintain itInput - Leaf Yes No No Meaning - The Farmer has shown you a picture of Leaf part of the paddy and it is healthy , congratulate the farmer and advice the farmer on how to maintain it Input - Leaf No Yes No Meaning - The Farmer has shown you a picture of Leaf part of the paddy and it has brown spots disease , advice the farmer on how to remedy it Input - Leaf No Yes YesMeaning - The Farmer has shown you a picture of Leaf part of the paddy and it has brown spots disease and Blight Disease , advice the farmer on how to remedy it Input - Leaf No No YesMeaning - The Farmer has shown you a picture of Leaf part of the paddy and it has Blight disease , advice the farmer on how to remedy it Input - Leaf Yes No Yes Meaning - The Farmer has shown you a picture of Leaf part of the paddy and it looks healthy but it has yellowish edges that might indicate blight , advice the farmer on how to remedy it Input - Leaf Yes Yes No Meaning - The Farmer has shown you a picture of Leaf part of the paddy and it has brown spots disease but also is quite green so it might be healthy and just got the brown spot disease , advice the farmer on how to remedy it",
"input: Sheath Yes",
"output: The paddy is suffering from sheath rot. \nThe management recommendations provided for sheath rot are as follows:\n-Spray Neem Oil: Apply neem oil at a concentration of 60 EC 3% to help manage sheath rot.\n-Soil Application of Gypsum: Apply gypsum at a rate of 200 kg/acre in two splits to the soil, as this can help in managing the disease.\n-Spray NSKE (Neem, Soap, and Karanja Emulsion): Apply NSKE at a concentration of 5% to help control the spread of sheath rot.\n-Spray Hexaconazole: Apply Hexaconazole at a concentration of 400 ml/acre. The first spray should be done at the boot leaf stage, with the second spray 15 days later.",
"input: Sheath No",
"output: Great news that the sheath is healthy! To maintain the health of the sheath and prevent future issues, it's essential for the farmer to continue practicing good agricultural management. This includes proper drainage to avoid waterlogging, adequate spacing between plants, and regular monitoring for signs of disease. Additionally, implementing a balanced fertilization schedule and ensuring proper irrigation can contribute to the overall health of the paddy sheath. Regular scouting for pests and diseases is also recommended to catch any issues early and take appropriate action.",
"input: Leaf Yes No No",
"output: It's great to see that the paddy leaves are healthy. To maintain their health, the farmer should continue practicing good agricultural management:\n-Proper Water Management: Ensure the paddy fields have proper drainage to avoid waterlogging, and monitor irrigation to prevent overwatering.\n-Balanced Fertilization: Implement a balanced fertilization schedule to provide the necessary nutrients for healthy leaf development.\n-Regular Monitoring: Continue to monitor the paddy fields regularly for any signs of pests or diseases, as early detection can help in preventing issues from spreading.\n-Cultural Practices: Practice good agricultural techniques such as proper spacing and timely irrigation to maintain the health of the paddy leaves.",
"input: Leaf No Yes Yes",
"output: The paddy leaves seem to be affected by both brown spot disease and bacterial leaf blight. Here are the management recommendations provided for these diseases:\n\nBrown Spot Disease:\n-Major Symptoms: Cylindrical oval dark brown spots with a yellow halo. In advanced cases, the spots coalesce together and dry.\n_Management: Recommended fungicides include Edifenphos (200ml/acre), Mancozeb (500g/acre), or Propineb (800g/acre).\n\nBacterial Leaf Blight (BLB):\n-Major Symptoms: Yellow-colored wavy margin with a zigzag appearance on the leaves.\n-Management: Spray 20% fresh cow dung extract twice, starting from the initial appearance of the disease and then at a fortnightly interval.",
"input: Leaf No Yes No",
"output: The paddy leaves seem to be affected by both brown spot disease Here are the management recommendations provided for this disease:\nRecommendations for managing brown spot disease in paddy leaves:\nFungicide Application: Apply appropriate fungicides to control the spread of brown spot disease. Commonly used fungicides for brown spot disease include Edifenphos, Mancozeb, or Propineb.\nProper Water Management: Ensure proper drainage to avoid waterlogging, as excessive moisture can contribute to the development and spread of brown spot disease\nField Sanitation: Remove and destroy infected plant debris to minimize the source of the disease.",
"input: Leaf No No Yes",
"output: The paddy leaves seem to be affected by bacterial leaf blight. Here are the management recommendations provided for this diseases:\n\nBacterial Leaf Blight (BLB):\n-Major Symptoms: Yellow-colored wavy margin with a zigzag appearance on the leaves.\n-Management: Spray 20% fresh cow dung extract twice, starting from the initial appearance of the disease and then at a fortnightly interval.",
"input: Leaf Yes Yes No",
"output: It's great to hear that the paddy leaves are healthy. However, since there are some indications of a potential issue with yellowish edges that might indicate blight, it's important for the farmer to stay vigilant and consider preventive measures to maintain the health of the paddy leaves.Given the combination of healthy appearance and the presence of potential yellowish edges that may indicate blight, it's advisable for the farmer to:\n\nMonitor closely for any further development of blight symptoms, such as the yellowish edges, and take preventive action if the symptoms progress.\n\nImplement preventive measures such as proper water management, balanced fertilization, and regular field monitoring to prevent blight disease.",
"input: Leaf Yes No Yes",
"output: It seems that the paddy leaves are healthy but are showing signs of blight disease. To address this issue, the farmer should consider the following remedies:\n\nFungicide Application: Apply appropriate fungicides to control the spread of blight disease. The document recommends the use of specific fungicides, such as Carbendazim or Mancozeb, to manage blight disease effectively.\nProper Water Management: Ensure proper drainage to avoid waterlogging, as excessive moisture can contribute to the development and spread of blight disease.\nField Sanitation: Remove and destroy infected plant debris to minimize the source of the disease.",
"input: Leaf Yes Yes Yes",
"output: Please provide The app with another picture taken from possibly a different angle or with better lighting , or use a different sample leaf",
]
input_key = result_string
output_key = ""
# Loop through the existing list and add the new keys and values
new_prompt_parts = []
for i in range(0, len(prompt_parts), 2): # Iterate over every other element
prompt = prompt_parts[i]
new_prompt_parts.append(prompt)
new_prompt_parts.append(f"input: {input_key}")
new_prompt_parts.append(f"output: {output_key}")
gemini = genai.GenerativeModel(model_name=model)
model = genai.GenerativeModel(model_name="gemini-1.0-pro",
generation_config=generation_config,
safety_settings=safety_settings)
response = model.generate_content(prompt_parts)
input_text=response.text
output_index = input_text.find("output:")
if output_index != -1:
# Extract the text after "output:"
output_text = input_text[output_index + len("output:"):].strip()
# Print the extracted text
return output_text
iface = gr.Interface(
fn=send_to_openai,
inputs=["file"],
outputs=["text"],
title="Rice Leaf Disease Detection Demo Using GPT-4V",
description="Made By Akash Mondal - https://github.com/akash-mondal | GPT-4Vision Paper - https://cdn.openai.com/papers/GPTV_System_Card.pdf "
)
iface.launch(debug=True)
|