hsienchen commited on
Commit
6130c7f
1 Parent(s): 930f89e

Upload app (1).py

Browse files
Files changed (1) hide show
  1. app (1).py +150 -0
app (1).py ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ import pathlib
9
+
10
+ txt_model = genai.GenerativeModel('gemini-pro')
11
+ vis_model = genai.GenerativeModel('gemini-pro-vision')
12
+
13
+ txt_prompt_1 = """The image contains the contents of a letter. I'd like to follow the request mentioned in the letter. Please provide 3 actionable items to assist me. When responding, use the following format:
14
+
15
+ # Sender and Subject #
16
+ 1- Action 1 (no more than 20 words)
17
+ 2- Action 2 (no more than 20 words)
18
+ 3- Action 3 (no more than 20 words)
19
+
20
+ For example:
21
+ # From Richard regarding 'Shipping to Customer ABC' #
22
+ 1- Pack Product A
23
+ 2- Ship before 3:00 PM today
24
+ 3- Notify Richard after shipment
25
+ """
26
+
27
+ txt_display_1 = 'content of email'
28
+
29
+ import os
30
+
31
+ GOOGLE_API_KEY=os.getenv('GOOGLE_API_KEY')
32
+
33
+ genai.configure(api_key=GOOGLE_API_KEY)
34
+
35
+ # Image to Base 64 Converter
36
+ def image_to_base64(image_path):
37
+ with open(image_path, 'rb') as img:
38
+ encoded_string = base64.b64encode(img.read())
39
+ return encoded_string.decode('utf-8')
40
+
41
+ # Function that takes User Inputs and displays it on ChatUI
42
+ def app2_query(history,txt,img):
43
+ if not img:
44
+ history += [(txt,None)]
45
+ return history
46
+ base64 = image_to_base64(img)
47
+ data_url = f"data:image/jpeg;base64,{base64}"
48
+ history += [(f"{txt} ![]({data_url})", None)]
49
+ return history
50
+
51
+ # Function that takes User Inputs, generates Response and displays on Chat UI
52
+ def app2_response(history,text,img):
53
+ if not img:
54
+ response = txt_model.generate_content(text)
55
+ history += [(None,response.text)]
56
+ return history
57
+
58
+ else:
59
+ img = PIL.Image.open(img)
60
+ response = vis_model.generate_content([text,img])
61
+ history += [(None,response.text)]
62
+ return history
63
+
64
+ # Function that takes User Inputs and displays it on ChatUI
65
+
66
+ def app1_query(img):
67
+ if not img:
68
+ return txt_prompt_1
69
+ base64 = image_to_base64(img)
70
+ data_url = f"data:image/jpeg;base64,{base64}"
71
+ outputText = [(f"{txt_display_1} ![]({data_url})", None)]
72
+ return outputText
73
+
74
+ # Function that takes User Inputs, generates Response and displays on Chat UI
75
+ def app1_response(img):
76
+ if not img:
77
+ response = txt_model.generate_content(txt_prompt_1)
78
+ return response
79
+
80
+ else:
81
+ img = PIL.Image.open(img)
82
+ response = vis_model.generate_content([txt_prompt_1,img])
83
+ return response.text
84
+
85
+
86
+ # Interface Code- Selector method
87
+
88
+ def sentence_builder(animal, place):
89
+ return f"""how many {animal}s from the {place} are shown in the picture?"""
90
+
91
+ # gradio block
92
+
93
+ with gr.Blocks(theme='snehilsanyal/scikit-learn') as app1:
94
+ with gr.Column():
95
+ outputbox = gr.Textbox(label="here are the plans...")
96
+ image_box = gr.Image(type="filepath")
97
+
98
+ btn = gr.Button("Make a Plan")
99
+ clicked = btn.click(app1_query,
100
+ [image_box],
101
+ outputbox
102
+ ).then(app1_response,
103
+ [image_box],
104
+ outputbox
105
+ )
106
+ gr.Markdown("""
107
+ # Make a Plan #
108
+
109
+ - screen capture (Win + shift + S)
110
+ - click **Make a Plan** to upload
111
+ - await LLM Bot (Gemini, in this case) response
112
+ - receive THREE actionable items
113
+
114
+
115
+ [demo](https://youtu.be/lJ4jIAEVRNY)
116
+
117
+ """)
118
+
119
+ with gr.Blocks(theme='snehilsanyal/scikit-learn') as app2:
120
+ gr.Markdown("check the image...")
121
+ with gr.Row():
122
+ image_box = gr.Image(type="filepath")
123
+
124
+ chatbot = gr.Chatbot(
125
+ scale = 2,
126
+ height=750
127
+ )
128
+ text_box = gr.Dropdown(
129
+ ["what is in the image",
130
+ "provide alternative title for the image",
131
+ "how many parts can be seen in the picture?",
132
+ "check ID and expiration date"],
133
+ label="Select--",
134
+ info="ask Bot"
135
+ )
136
+
137
+ btn = gr.Button("Submit")
138
+ clicked = btn.click(app2_query,
139
+ [chatbot,text_box,image_box],
140
+ chatbot
141
+ ).then(app2_response,
142
+ [chatbot,text_box],
143
+ chatbot
144
+ )
145
+ with gr.Blocks(theme='snehilsanyal/scikit-learn') as demo:
146
+ gr.Markdown("## Workflow Bot ##")
147
+ gr.TabbedInterface([app1, app2], ["Make a Plan!", "Check This!"])
148
+
149
+ demo.queue()
150
+ demo.launch()