aminaj commited on
Commit
fb4af47
1 Parent(s): 02f5539

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +67 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import PIL.Image
2
+ import gradio as gr
3
+ import base64
4
+ import time
5
+ import os
6
+ import google.generativeai as genai
7
+
8
+ # Set Google API key
9
+ os.environ['GOOGLE_API_KEY'] = 'YOUR_API_KEY'
10
+ genai.configure(api_key=os.environ['GOOGLE_API_KEY'])
11
+
12
+ # Create the Model
13
+ txt_model = genai.GenerativeModel('gemini-pro')
14
+ vis_model = genai.GenerativeModel('gemini-pro-vision')
15
+
16
+ # Image to Base 64 Converter
17
+ def image_to_base64(image_path):
18
+ with open(image_path, 'rb') as img:
19
+ encoded_string = base64.b64encode(img.read())
20
+ return encoded_string.decode('utf-8')
21
+
22
+ # Function that takes user Inputs and displays it on ChatUI
23
+ def query_message(history, txt, img):
24
+ if not img:
25
+ history += [(txt, None)]
26
+ return history
27
+ base64 = image_to_base64(img)
28
+ data_url = f"data:image/jpeg;base64,{base64}"
29
+ history += [(f"{txt} ![]({data_url})", None)]
30
+ return history
31
+
32
+ # Function that takes user Inputs, generates Response and displays it on ChatUI
33
+ def llm_response(history, text, img):
34
+ if not img:
35
+ response = txt_model.generate_content(text)
36
+ history += [(None, response.text)]
37
+ return history
38
+ else:
39
+ img = PIL.Image.open(img)
40
+ response = vis_model.generate_content([text, img])
41
+ history += [(None, response.text)]
42
+ return history
43
+
44
+ # Interface Code
45
+ with gr.Blocks(theme=gr.themes.Default()) as app:
46
+ with gr.Row():
47
+ image_box = gr.Image(type="filepath")
48
+
49
+ chatbot = gr.Chatbot(
50
+ scale = 2,
51
+ height = 550
52
+ )
53
+
54
+ text_box = gr.Textbox(
55
+ placeholder = "Enter text and press enter, or upload an image",
56
+ container = False,
57
+ )
58
+
59
+ btn = gr.Button("Submit")
60
+ clicked = btn.click(query_message,
61
+ [chatbot, text_box, image_box],
62
+ chatbot).then(llm_response,
63
+ [chatbot, text_box, image_box],
64
+ chatbot)
65
+
66
+ app.queue()
67
+ app.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gradio
2
+ google.generativeai