Satyam-Singh commited on
Commit
de52306
·
verified ·
1 Parent(s): b0da07d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -1
app.py CHANGED
@@ -1,4 +1,74 @@
 
 
 
 
 
1
  import google.generativeai as genai
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  import gradio as gr
3
  import os
4
 
@@ -127,4 +197,4 @@ gr.ChatInterface(
127
  title="PaLM-2",
128
  description="This is unofficial demo of ```PaLM-2``` based on ```Google API```. ```History/context``` memory does not work in this demo.",
129
  concurrency_limit=20,
130
- ).launch(show_api=False)
 
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
+ genai.configure(api_key = os.environ['GOOGLE_PALM_KEY'])
10
+
11
+ # Create the Model
12
+ txt_model = genai.GenerativeModel('gemini-pro')
13
+ vis_model = genai.GenerativeModel('gemini-pro-vision')
14
+
15
+ # Image to Base 64 Converter
16
+ def image_to_base64(image_path):
17
+ with open(image_path, 'rb') as img:
18
+ encoded_string = base64.b64encode(img.read())
19
+ return encoded_string.decode('utf-8')
20
+
21
+ # Function that takes User Inputs and displays it on ChatUI
22
+ def query_message(history,txt,img):
23
+ if not img:
24
+ history += [(txt,None)]
25
+ return history
26
+ base64 = image_to_base64(img)
27
+ data_url = f"data:image/jpeg;base64,{base64}"
28
+ history += [(f"{txt} ![]({data_url})", None)]
29
+ return history
30
+
31
+ # Function that takes User Inputs, generates Response and displays on Chat UI
32
+ def llm_response(history,text,img):
33
+ if not img:
34
+ response = txt_model.generate_content(text)
35
+ history += [(None,response.text)]
36
+ return history
37
+
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() as app:
46
+ with gr.Row():
47
+ image_box = gr.Image(type="filepath")
48
+
49
+ chatbot = gr.Chatbot(
50
+ scale = 2,
51
+ height=750
52
+ )
53
+ text_box = gr.Textbox(
54
+ placeholder="Enter text and press enter, or upload an image",
55
+ container=False,
56
+ )
57
+
58
+ btn = gr.Button("Submit")
59
+ clicked = btn.click(query_message,
60
+ [chatbot,text_box,image_box],
61
+ chatbot
62
+ ).then(llm_response,
63
+ [chatbot,text_box,image_box],
64
+ chatbot
65
+ )
66
+ app.queue()
67
+ app.launch(debug=True)
68
+
69
+
70
+
71
+ ''''import google.generativeai as genai
72
  import gradio as gr
73
  import os
74
 
 
197
  title="PaLM-2",
198
  description="This is unofficial demo of ```PaLM-2``` based on ```Google API```. ```History/context``` memory does not work in this demo.",
199
  concurrency_limit=20,
200
+ ).launch(show_api=False)'''