!pip install google-generativeai !pip install gradio huggingface_hub from pathlib import Path import google.generativeai as genai import re from PIL import Image import os #from google.colab import userdata #os.environ['GOOGLE_API_KEY'] = userdata.get('GOOGLE_API_KEY') #GOOGLE_API_KEY = os.environ['GOOGLE_API_KEY'] #genai.configure(api_key=os.environ["GOOGLE_API_KEY"]) #or use this for personal notebook genai.configure(api_key="AIzaSyD----") # Configuration for our Gemini Models textgeneration_config = { "temperature": 0.9, "top_p": 1, "top_k": 1, "max_output_tokens": 2048,} visiongeneration_config = { "temperature": 0.9, "top_p": 1, "top_k": 10, "max_output_tokens": 1024, } 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" }, ] # Two models - vision and text textmodel = genai.GenerativeModel('gemini-1.0-pro', generation_config=textgeneration_config, safety_settings=safety_settings) #imagemodel = genai.GenerativeModel('gemini-pro-vision') visionmodel = genai.GenerativeModel(model_name="gemini-1.0-pro-vision-latest", generation_config=visiongeneration_config, safety_settings=safety_settings) # Utility Functions # Convert an image to base64 string format import base64 def img2base64(image): with open(image, 'rb') as img: encoded_string = base64.b64encode(img.read()) return encoded_string.decode('utf-8') # Check image format and display user messages in GUI def user_inputs(history, txt, img): if not img: history += [(txt, None)] return history # Open the image for format verification try: with Image.open(img) as image: # Get image format (e.g., PNG, JPEG) image_format = image.format.upper() except (IOError, OSError): return history if image_format not in ('JPEG','JPG','PNG'): print(f"Warning: Unsupported image format: {image_format}") return history base64 = img2base64(img) data_url = f"data:image/{image_format.lower()};base64,{base64}" history += [(f"{txt} ![]({data_url})", None)] import gradio as gr TITLE = """