Spaces:
Runtime error
Runtime error
File size: 4,291 Bytes
0b9e99a 6153b83 a9442f2 1fc962a a9442f2 6153b83 0b9e99a a9442f2 0b9e99a a9442f2 0b9e99a 27fd04a 0b9e99a 5b50c97 f6b3153 d2d37d9 f6b3153 d2d37d9 f6b3153 a9442f2 0b9e99a f6b3153 d2d37d9 6153b83 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 |
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)
return result_string
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)
|