import base64 import gradio as gr import requests import google.generativeai as palm import json import os oapi_key= os.getenv("openaiapi") palm.configure(api_key=os.getenv("palmapi")) model = 'models/text-bison-001' temperature = 0.5 candidate_count = 1 top_k = 40 top_p = 0.95 max_output_tokens = 1024 text_b64 = 'WW91IGFyZSBhIFJpY2UgUGFkZHkgQWdyaWN1bHR1cmFsIFNjaWVudGlzdCAsIFlvdSB3aWxsIGJlIGdpdmVuIHRoZSBzdGF0ZSBvZiB0aGUgcGFkZHkoYSBwaWN0dXJlIGRlc2NyaXB0aW9uICkgYW5kIHlvdSB3aWxsIGhhdmUgdG8gYWR2aWNlIG9uIHRoZSBjb3JyZWN0IHJlbWVkeUlucHV0IC0gU2hlYXRoIFllcyBNZWFuaW5nIC0gIFRoZSBGYXJtZXIgaGFzIHNob3duIHlvdSBhIHBpY3R1cmUgb2YgU2hlYXRoIHBhcnQgb2YgdGhlIHBhZGR5IGFuZCBpdCBpcyBzdWZmZXJpbmcgZnJvbSBTaGVhdGggUm90ICwgYWR2aWNlIHRoZSBmYXJtZXIgb24gaG93IHRvIGZpeCB0aGlzSW5wdXQgLSBTaGVhdGggTm8gTWVhbmluZyAtIFRoZSBGYXJtZXIgaGFzIHNob3duIHlvdSBhIHBpY3R1cmUgb2YgU2hlYXRoIHBhcnQgb2YgdGhlIHBhZGR5IGFuZCBpdCBpcyBoZWFsdGh5ICxjb25ncmF0dWxhdGUgYW5kIGFkdmljZSBmYXJtZXIgb24gaG93IHRvIG1haW50YWluIGl0SW5wdXQgLSBMZWFmIFllcyBObyBObyBNZWFuaW5nIC0gVGhlIEZhcm1lciBoYXMgc2hvd24geW91IGEgcGljdHVyZSBvZiBMZWFmIHBhcnQgb2YgdGhlIHBhZGR5IGFuZCBpdCBpcyBoZWFsdGh5ICwgY29uZ3JhdHVsYXRlIHRoZSBmYXJtZXIgYW5kIGFkdmljZSB0aGUgZmFybWVyIG9uIGhvdyB0byBtYWludGFpbiBpdCBJbnB1dCAtIExlYWYgTm8gWWVzIE5v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bmcgLCBvciB1c2UgYSBkaWZmZXJlbnQgc2FtcGxlIGxlYWYKaW5wdXQ6' # @param {isTemplate: true} stop_sequences_b64 = 'W10=' safety_settings_b64 = 'W3siY2F0ZWdvcnkiOiJIQVJNX0NBVEVHT1JZX0RFUk9HQVRPUlkiLCJ0aHJlc2hvbGQiOiJCTE9DS19MT1dfQU5EX0FCT1ZFIn0seyJjYXRlZ29yeSI6IkhBUk1fQ0FURUdPUllfVE9YSUNJVFkiLCJ0aHJlc2hvbGQiOiJCTE9DS19MT1dfQU5EX0FCT1ZFIn0seyJjYXRlZ29yeSI6IkhBUk1fQ0FURUdPUllfVklPTEVOQ0UiLCJ0aHJlc2hvbGQiOiJCTE9DS19NRURJVU1fQU5EX0FCT1ZFIn0seyJjYXRlZ29yeSI6IkhBUk1fQ0FURUdPUllfU0VYVUFMIiwidGhyZXNob2xkIjoiQkxPQ0tfTUVESVVNX0FORF9BQk9WRSJ9LHsiY2F0ZWdvcnkiOiJIQVJNX0NBVEVHT1JZX01FRElDQUwiLCJ0aHJlc2hvbGQiOiJCTE9DS19NRURJVU1fQU5EX0FCT1ZFIn0seyJjYXRlZ29yeSI6IkhBUk1fQ0FURUdPUllfREFOR0VST1VTIiwidGhyZXNob2xkIjoiQkxPQ0tfTUVESVVNX0FORF9BQk9WRSJ9XQ==' # @param {isTemplate: true} # Convert the prompt text param from a bae64 string to a string. text = base64.b64decode(text_b64).decode("utf-8") # Convert the stop_sequences and safety_settings params from base64 strings to lists. stop_sequences = json.loads(base64.b64decode(stop_sequences_b64).decode("utf-8")) safety_settings = json.loads(base64.b64decode(safety_settings_b64).decode("utf-8")) defaults = { 'model': model, 'temperature': temperature, 'candidate_count': candidate_count, 'top_k': top_k, 'top_p': top_p, 'max_output_tokens': max_output_tokens, 'stop_sequences': stop_sequences, 'safety_settings': safety_settings, } 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) response = palm.generate_text( **defaults, prompt=text + result_string ) output_text = response.candidates[0]['output'] return output_text[8:] iface = gr.Interface( fn=send_to_openai, inputs=["file"], outputs=["text"], title="Rice Leaf Disease Detection Demo Using GPT-4V(ision) + PaLM 2", description="Made By Akash Mondal - https://github.com/akash-mondal | GPT-4Vision Paper - https://cdn.openai.com/papers/GPTV_System_Card.pdf | PaLM2 Paper - https://arxiv.org/pdf/2305.10403.pdf " ) iface.launch(debug=True)