File size: 1,853 Bytes
f5a484c
 
 
 
 
56a7c11
 
f5a484c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56a7c11
f5a484c
 
 
 
56a7c11
 
 
f5a484c
 
 
 
 
20fcc49
f5a484c
 
56a7c11
 
 
 
 
 
 
 
f5a484c
56a7c11
f5a484c
56a7c11
 
f5a484c
 
 
 
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
import streamlit as st
import textwrap
import google.generativeai as genai
from IPython.display import Markdown
import PIL.Image
#from PIL import Image



def to_markdown(text):
    text = text.replace('•', '  *')
    return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))

def vision(img):
    model = genai.GenerativeModel('gemini-pro-vision')
    response = model.generate_content(img)
    to_markdown(response.text)
    response = model.generate_content(["Tell me the fruit name and ripeness level.", img], stream=True)
    response.resolve()
    to_markdown(response.text)
    vision_output = response.text
    return vision_output

def chat(inpt_frm_vsn):
    model = genai.GenerativeModel('gemini-pro')
    chat = model.start_chat(history=[])
    chat

    res = inpt_frm_vsn + 'Tell me the fruit name and nutrition'

    while True:
        response = chat.send_message(res, generation_config=genai.types.GenerationConfig(
            max_output_tokens=100
        ))
        to_markdown(response.text)

        st.text('Gemini: ' + response.text)
        res = st.chat_input('Please write your message here...')
        if res:
            st.write(f"User: {res}")

def main():
    st.title("Gemini")


    GOOGLE_API_KEY = 'AIzaSyDMDS5TghIYsEV4nD6FsC8PDCtqGvkuUls'

    genai.configure(api_key=GOOGLE_API_KEY)
    uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

    if uploaded_file is not None:
        img = PIL.Image.open(uploaded_file)
        st.image(img, caption='Uploaded Image', use_column_width=True)

        vision_result = vision(img)
        st.text(vision_result)

        check_point = st.text_input("If you want to continue chat write 'Nutrition': ")

        if check_point.lower() == 'nutrition':
            chat(vision_result)


if __name__ == "__main__":
    main()