File size: 4,890 Bytes
a25e9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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 = """<h1 align="center">Your Personal Health Coach</h1>"""
SUBTITLE = """<h2 align="center">Upload an image of your food to knows its calories, macronutrients or ask questions about heath and exercise.</h2>"""
DES = """
<div style="text-align: center; display: flex; justify-content: center; align-items: center;">
    <span>You need to enter your FREE GEMINI KEY in the first text box to connect to Gemini Models. You can find your key here:
        <a href="https://makersuite.google.com/app/apikey">GOOGLE API KEY</a>. <br><br>
        <b> If you wish to ask a question unrelated to the image you have uploaded, just cross the image (top right corner of image) and then submit your question in the textbox.
    </span>
</div>
"""

def generate_model_response(api_key, history, text, img):
  genai.configure(api_key=api_key)
  if not img:
        text = "You are an expert nutritionist and fitness coach. You are accurate, you always stick to the facts, and never make up new facts. \
            For the questions asked by the user, answer accurately and to the point, in a friendly tone." + text
        response = textmodel.generate_content(text)

  else:
        text = "From the image uploaded by the user answer with following information: Food items in the image, \
            percentage of each macronutrient in the food in image and approximate number of calories in the food in image. If there is any additional question, answer that too." + text
        img = Image.open(img)
        response = visionmodel.generate_content([text,img])
  history += [(None, response.text)]
  return history


with gr.Blocks() as app:

  gr.HTML(TITLE)
  gr.HTML(SUBTITLE)
  gr.HTML(DES)

  api_key_box = gr.Textbox(placeholder = "Enter your GEMINI API KEY", label="Your GEMINI API KEY", type="password")

  with gr.Row():
    image_box = gr.Image(type="filepath")
    chatbot = gr.Chatbot(
      scale=3,
      height=750
    )

  text_box = gr.Textbox(
    placeholder="Ask something about the image your uploaded or ask for any health and fitness advice without uploading an image too",
    container=False,
  )

  btn = gr.Button("Submit")

  btn_clicked = btn.click(user_inputs,
                     [chatbot, text_box, image_box],
                     chatbot).then(generate_model_response,[api_key_box, chatbot, text_box, image_box], chatbot)

app.queue()
app.launch(debug=True)