Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,44 +13,38 @@ load_dotenv()
|
|
| 13 |
# ===============================
|
| 14 |
# AZURE CONFIG
|
| 15 |
# ===============================
|
| 16 |
-
AZURE_API_KEY = os.getenv("AZURE_OPENAI_API_KEY")
|
| 17 |
-
AZURE_ENDPOINT = os.getenv("AZURE_OPENAI_ENDPOINT")
|
| 18 |
-
AZURE_DEPLOYMENT = os.getenv("AZURE_OPENAI_DEPLOYMENT")
|
| 19 |
-
AZURE_API_VERSION = os.getenv(
|
| 20 |
-
"AZURE_OPENAI_API_VERSION",
|
| 21 |
-
"2024-02-15-preview"
|
| 22 |
-
)
|
| 23 |
-
|
| 24 |
client = AzureOpenAI(
|
| 25 |
-
api_key=
|
| 26 |
-
api_version=
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
| 28 |
)
|
| 29 |
|
|
|
|
|
|
|
| 30 |
# ===============================
|
| 31 |
-
#
|
| 32 |
# ===============================
|
| 33 |
-
last_crop_result = None
|
| 34 |
crop_cache = {}
|
| 35 |
|
| 36 |
|
| 37 |
-
def
|
| 38 |
return hashlib.md5(image_bytes).hexdigest()
|
| 39 |
|
| 40 |
|
| 41 |
# ===============================
|
| 42 |
-
#
|
| 43 |
# ===============================
|
| 44 |
-
def identify_crop(image_file):
|
| 45 |
-
global last_crop_result
|
| 46 |
|
| 47 |
if image_file is None:
|
| 48 |
-
return "โ Please upload a crop image."
|
| 49 |
|
| 50 |
try:
|
| 51 |
img = Image.open(image_file)
|
| 52 |
|
| 53 |
-
# Resize large images
|
| 54 |
if img.width > 1000 or img.height > 1000:
|
| 55 |
img.thumbnail((1000, 1000))
|
| 56 |
|
|
@@ -61,80 +55,78 @@ def identify_crop(image_file):
|
|
| 61 |
img.save(buffer, format="JPEG", quality=85)
|
| 62 |
|
| 63 |
image_bytes = buffer.getvalue()
|
| 64 |
-
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
# โ
IMAGE CACHE
|
| 69 |
if image_hash in crop_cache:
|
| 70 |
-
|
| 71 |
-
return f"๐พ Cached Crop Result:\n\n{
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
# Azure Vision Call
|
| 74 |
response = client.chat.completions.create(
|
| 75 |
model=AZURE_DEPLOYMENT,
|
| 76 |
messages=[
|
| 77 |
{
|
| 78 |
"role": "system",
|
| 79 |
-
"content":
|
|
|
|
| 80 |
},
|
| 81 |
{
|
| 82 |
"role": "user",
|
| 83 |
"content": [
|
| 84 |
{
|
| 85 |
"type": "text",
|
| 86 |
-
"text":
|
|
|
|
| 87 |
},
|
| 88 |
{
|
| 89 |
"type": "image_url",
|
| 90 |
"image_url": {
|
| 91 |
-
"url":
|
|
|
|
| 92 |
},
|
| 93 |
},
|
| 94 |
],
|
| 95 |
},
|
| 96 |
],
|
| 97 |
-
max_tokens=
|
| 98 |
)
|
| 99 |
|
| 100 |
result = response.choices[0].message.content
|
| 101 |
-
|
| 102 |
crop_cache[image_hash] = result
|
| 103 |
-
last_crop_result = result
|
| 104 |
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
except Exception:
|
| 108 |
-
return
|
| 109 |
|
| 110 |
|
| 111 |
# ===============================
|
| 112 |
-
# CHATBOT
|
| 113 |
# ===============================
|
| 114 |
-
def ask_chatbot(
|
| 115 |
-
global last_crop_result
|
| 116 |
|
| 117 |
-
if
|
| 118 |
return "โ ๏ธ Please upload and identify a crop image first."
|
| 119 |
|
| 120 |
-
context = f"\
|
| 121 |
|
| 122 |
response = client.chat.completions.create(
|
| 123 |
model=AZURE_DEPLOYMENT,
|
| 124 |
messages=[
|
| 125 |
{
|
| 126 |
"role": "system",
|
| 127 |
-
"content":
|
| 128 |
-
|
| 129 |
-
"Answer using simple farmer-friendly language."
|
| 130 |
-
),
|
| 131 |
},
|
| 132 |
{
|
| 133 |
"role": "user",
|
| 134 |
-
"content": context +
|
| 135 |
-
}
|
| 136 |
],
|
| 137 |
-
max_tokens=
|
| 138 |
)
|
| 139 |
|
| 140 |
return response.choices[0].message.content
|
|
@@ -143,31 +135,35 @@ def ask_chatbot(user_message):
|
|
| 143 |
# ===============================
|
| 144 |
# CHAT UI
|
| 145 |
# ===============================
|
| 146 |
-
def chat_ui(message, history):
|
| 147 |
|
| 148 |
if history is None:
|
| 149 |
history = []
|
| 150 |
|
| 151 |
if not message:
|
| 152 |
-
return history, ""
|
| 153 |
|
| 154 |
-
reply = ask_chatbot(message)
|
| 155 |
|
| 156 |
history.append([message, reply])
|
| 157 |
|
| 158 |
-
return history, ""
|
| 159 |
|
| 160 |
|
| 161 |
# ===============================
|
| 162 |
-
#
|
| 163 |
# ===============================
|
| 164 |
with gr.Blocks(title="Crop Prediction") as demo:
|
| 165 |
|
| 166 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
|
| 168 |
with gr.Row():
|
| 169 |
|
| 170 |
-
# LEFT SIDE
|
| 171 |
with gr.Column():
|
| 172 |
image_input = gr.Image(
|
| 173 |
type="filepath",
|
|
@@ -181,36 +177,32 @@ with gr.Blocks(title="Crop Prediction") as demo:
|
|
| 181 |
label="Result"
|
| 182 |
)
|
| 183 |
|
| 184 |
-
# RIGHT SIDE
|
| 185 |
with gr.Column():
|
| 186 |
chatbot = gr.Chatbot(height=400)
|
| 187 |
msg = gr.Textbox(
|
| 188 |
-
placeholder="Ask about soil,
|
| 189 |
)
|
| 190 |
send = gr.Button("Send")
|
| 191 |
|
| 192 |
identify_btn.click(
|
| 193 |
identify_crop,
|
| 194 |
-
image_input,
|
| 195 |
-
image_output
|
| 196 |
)
|
| 197 |
|
| 198 |
send.click(
|
| 199 |
chat_ui,
|
| 200 |
-
[msg, chatbot],
|
| 201 |
-
[chatbot, msg]
|
| 202 |
)
|
| 203 |
|
| 204 |
msg.submit(
|
| 205 |
chat_ui,
|
| 206 |
-
[msg, chatbot],
|
| 207 |
-
[chatbot, msg]
|
| 208 |
)
|
| 209 |
|
| 210 |
|
| 211 |
-
# ===============================
|
| 212 |
-
# LAUNCH
|
| 213 |
-
# ===============================
|
| 214 |
if __name__ == "__main__":
|
| 215 |
demo.launch(
|
| 216 |
server_name="0.0.0.0",
|
|
|
|
| 13 |
# ===============================
|
| 14 |
# AZURE CONFIG
|
| 15 |
# ===============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
client = AzureOpenAI(
|
| 17 |
+
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
|
| 18 |
+
api_version=os.getenv(
|
| 19 |
+
"AZURE_OPENAI_API_VERSION",
|
| 20 |
+
"2024-02-15-preview"
|
| 21 |
+
),
|
| 22 |
+
azure_endpoint=os.getenv("AZURE_OPENAI_ENDPOINT")
|
| 23 |
)
|
| 24 |
|
| 25 |
+
AZURE_DEPLOYMENT = os.getenv("AZURE_OPENAI_DEPLOYMENT")
|
| 26 |
+
|
| 27 |
# ===============================
|
| 28 |
+
# IMAGE CACHE ONLY
|
| 29 |
# ===============================
|
|
|
|
| 30 |
crop_cache = {}
|
| 31 |
|
| 32 |
|
| 33 |
+
def get_hash(image_bytes):
|
| 34 |
return hashlib.md5(image_bytes).hexdigest()
|
| 35 |
|
| 36 |
|
| 37 |
# ===============================
|
| 38 |
+
# IDENTIFY CROP
|
| 39 |
# ===============================
|
| 40 |
+
def identify_crop(image_file, crop_state):
|
|
|
|
| 41 |
|
| 42 |
if image_file is None:
|
| 43 |
+
return "โ Please upload a crop image.", crop_state
|
| 44 |
|
| 45 |
try:
|
| 46 |
img = Image.open(image_file)
|
| 47 |
|
|
|
|
| 48 |
if img.width > 1000 or img.height > 1000:
|
| 49 |
img.thumbnail((1000, 1000))
|
| 50 |
|
|
|
|
| 55 |
img.save(buffer, format="JPEG", quality=85)
|
| 56 |
|
| 57 |
image_bytes = buffer.getvalue()
|
| 58 |
+
image_hash = get_hash(image_bytes)
|
| 59 |
|
| 60 |
+
# โ
cache
|
|
|
|
|
|
|
| 61 |
if image_hash in crop_cache:
|
| 62 |
+
result = crop_cache[image_hash]
|
| 63 |
+
return f"๐พ Cached Crop Result:\n\n{result}", result
|
| 64 |
+
|
| 65 |
+
image_base64 = base64.b64encode(image_bytes).decode()
|
| 66 |
|
|
|
|
| 67 |
response = client.chat.completions.create(
|
| 68 |
model=AZURE_DEPLOYMENT,
|
| 69 |
messages=[
|
| 70 |
{
|
| 71 |
"role": "system",
|
| 72 |
+
"content":
|
| 73 |
+
"You are an expert agricultural scientist."
|
| 74 |
},
|
| 75 |
{
|
| 76 |
"role": "user",
|
| 77 |
"content": [
|
| 78 |
{
|
| 79 |
"type": "text",
|
| 80 |
+
"text":
|
| 81 |
+
"Identify this crop briefly."
|
| 82 |
},
|
| 83 |
{
|
| 84 |
"type": "image_url",
|
| 85 |
"image_url": {
|
| 86 |
+
"url":
|
| 87 |
+
f"data:image/jpeg;base64,{image_base64}"
|
| 88 |
},
|
| 89 |
},
|
| 90 |
],
|
| 91 |
},
|
| 92 |
],
|
| 93 |
+
max_tokens=300,
|
| 94 |
)
|
| 95 |
|
| 96 |
result = response.choices[0].message.content
|
|
|
|
| 97 |
crop_cache[image_hash] = result
|
|
|
|
| 98 |
|
| 99 |
+
# โ
SAVE ONLY IN SESSION
|
| 100 |
+
return f"๐พ Crop Identification:\n\n{result}", result
|
| 101 |
|
| 102 |
except Exception:
|
| 103 |
+
return traceback.format_exc(), crop_state
|
| 104 |
|
| 105 |
|
| 106 |
# ===============================
|
| 107 |
+
# CHATBOT
|
| 108 |
# ===============================
|
| 109 |
+
def ask_chatbot(message, crop_state):
|
|
|
|
| 110 |
|
| 111 |
+
if not crop_state:
|
| 112 |
return "โ ๏ธ Please upload and identify a crop image first."
|
| 113 |
|
| 114 |
+
context = f"\nCrop Info:\n{crop_state}\n"
|
| 115 |
|
| 116 |
response = client.chat.completions.create(
|
| 117 |
model=AZURE_DEPLOYMENT,
|
| 118 |
messages=[
|
| 119 |
{
|
| 120 |
"role": "system",
|
| 121 |
+
"content":
|
| 122 |
+
"You are a farming advisor. Give direct practical answers."
|
|
|
|
|
|
|
| 123 |
},
|
| 124 |
{
|
| 125 |
"role": "user",
|
| 126 |
+
"content": context + message
|
| 127 |
+
}
|
| 128 |
],
|
| 129 |
+
max_tokens=400,
|
| 130 |
)
|
| 131 |
|
| 132 |
return response.choices[0].message.content
|
|
|
|
| 135 |
# ===============================
|
| 136 |
# CHAT UI
|
| 137 |
# ===============================
|
| 138 |
+
def chat_ui(message, history, crop_state):
|
| 139 |
|
| 140 |
if history is None:
|
| 141 |
history = []
|
| 142 |
|
| 143 |
if not message:
|
| 144 |
+
return history, "", crop_state
|
| 145 |
|
| 146 |
+
reply = ask_chatbot(message, crop_state)
|
| 147 |
|
| 148 |
history.append([message, reply])
|
| 149 |
|
| 150 |
+
return history, "", crop_state
|
| 151 |
|
| 152 |
|
| 153 |
# ===============================
|
| 154 |
+
# UI
|
| 155 |
# ===============================
|
| 156 |
with gr.Blocks(title="Crop Prediction") as demo:
|
| 157 |
|
| 158 |
+
gr.Markdown(
|
| 159 |
+
"# ๐พ Smart Crop Identification & Farming Assistant"
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
# โ
SESSION MEMORY
|
| 163 |
+
crop_state = gr.State(None)
|
| 164 |
|
| 165 |
with gr.Row():
|
| 166 |
|
|
|
|
| 167 |
with gr.Column():
|
| 168 |
image_input = gr.Image(
|
| 169 |
type="filepath",
|
|
|
|
| 177 |
label="Result"
|
| 178 |
)
|
| 179 |
|
|
|
|
| 180 |
with gr.Column():
|
| 181 |
chatbot = gr.Chatbot(height=400)
|
| 182 |
msg = gr.Textbox(
|
| 183 |
+
placeholder="Ask about soil, disease..."
|
| 184 |
)
|
| 185 |
send = gr.Button("Send")
|
| 186 |
|
| 187 |
identify_btn.click(
|
| 188 |
identify_crop,
|
| 189 |
+
[image_input, crop_state],
|
| 190 |
+
[image_output, crop_state]
|
| 191 |
)
|
| 192 |
|
| 193 |
send.click(
|
| 194 |
chat_ui,
|
| 195 |
+
[msg, chatbot, crop_state],
|
| 196 |
+
[chatbot, msg, crop_state]
|
| 197 |
)
|
| 198 |
|
| 199 |
msg.submit(
|
| 200 |
chat_ui,
|
| 201 |
+
[msg, chatbot, crop_state],
|
| 202 |
+
[chatbot, msg, crop_state]
|
| 203 |
)
|
| 204 |
|
| 205 |
|
|
|
|
|
|
|
|
|
|
| 206 |
if __name__ == "__main__":
|
| 207 |
demo.launch(
|
| 208 |
server_name="0.0.0.0",
|