File size: 2,329 Bytes
f5a484c
 
 
 
 
56a7c11
f5a484c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
556216e
 
f5a484c
 
 
 
ba2db48
 
556216e
 
 
 
 
 
 
 
 
0f1c975
556216e
 
 
 
f5a484c
ba2db48
56a7c11
f5a484c
 
 
556216e
ba2db48
 
f5a484c
 
 
 
 
20fcc49
f5a484c
 
56a7c11
 
 
 
ba2db48
56a7c11
 
 
f5a484c
ba2db48
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
67
68
69
70
71
72
73
74
75
76
77
78
79
import streamlit as st
import textwrap
import google.generativeai as genai
from IPython.display import Markdown
import PIL.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=[])

    message_count = 0

    continue_chat = True

    while continue_chat:
        if message_count == 0:
            input_msg = inpt_frm_vsn + ' Tell me the fruit name and nutrition. Complete answer in 55 words. Never show the word count.'
        else:
            input_key = f"chat_input_{message_count}"
            user_input = st.chat_input('Please write your message here...', key=input_key)
            if user_input:
                st.write(f"User: {user_input}")
                input_msg = user_input + ' Complete answer in 55 words. Never show the word count.'
            else:
                continue_chat = False
                break

        response = chat.send_message(input_msg, generation_config=genai.types.GenerationConfig(
            max_output_tokens=100
        ))
        to_markdown(response.text)
        st.text('Gemini: ' + response.text)
        message_count += 1

        

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()