salek877 commited on
Commit
f5a484c
·
1 Parent(s): 70e12b0

gemini streamlit

Browse files
Files changed (1) hide show
  1. gemini_streamlit.py +61 -0
gemini_streamlit.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import textwrap
3
+ import google.generativeai as genai
4
+ from IPython.display import display
5
+ from IPython.display import Markdown
6
+ import PIL.Image
7
+
8
+ def load_img(file_pth):
9
+ return PIL.Image.open(file_pth)
10
+
11
+ def to_markdown(text):
12
+ text = text.replace('•', ' *')
13
+ return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))
14
+
15
+ def vision(img):
16
+ model = genai.GenerativeModel('gemini-pro-vision')
17
+ response = model.generate_content(img)
18
+ to_markdown(response.text)
19
+ response = model.generate_content(["Tell me the fruit name and ripeness level.", img], stream=True)
20
+ response.resolve()
21
+ to_markdown(response.text)
22
+ vision_output = response.text
23
+ return vision_output
24
+
25
+ def chat(inpt_frm_vsn):
26
+ model = genai.GenerativeModel('gemini-pro')
27
+ chat = model.start_chat(history=[])
28
+ chat
29
+
30
+ res = inpt_frm_vsn + 'Tell me the fruit name and nutrition'
31
+
32
+ while True:
33
+ response = chat.send_message(res, generation_config=genai.types.GenerationConfig(
34
+ max_output_tokens=200
35
+ ))
36
+ to_markdown(response.text)
37
+
38
+ st.text('Gemini: ' + response.text)
39
+ res = st.text_input('User: ')
40
+
41
+ def main():
42
+ st.title("Gemini")
43
+
44
+ img_path = 'assets/apple.jpg'
45
+ img = load_img(img_path)
46
+ st.image(img, caption='Uploaded Image.', use_column_width=True)
47
+
48
+ GOOGLE_API_KEY = 'AIzaSyA1bYLSWmPCXiN7C6LdyrLtXmtw7ylbuS0'
49
+
50
+ genai.configure(api_key=GOOGLE_API_KEY)
51
+
52
+ vision_result = vision(img)
53
+ st.text(vision_result)
54
+
55
+ check_point = st.text_input("If you want to continue chat write 'nutrition': ")
56
+
57
+ if check_point.lower() == 'nutrition':
58
+ chat(vision_result)
59
+
60
+ if __name__ == "__main__":
61
+ main()