tonyassi commited on
Commit
5450c5c
·
verified ·
1 Parent(s): 00a1bda

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +243 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,243 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import google.generativeai as genai
4
+ from google.generativeai.types import HarmCategory, HarmBlockThreshold
5
+ from PIL import Image
6
+ import random
7
+
8
+ # Gemini API key
9
+ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
10
+
11
+ # Gemini config
12
+ generation_config = {
13
+ "temperature": 1,
14
+ "top_p": 0.95,
15
+ "top_k": 64,
16
+ "max_output_tokens": 8192,
17
+ "response_mime_type": "text/plain",
18
+ }
19
+
20
+ # Gemini model
21
+ model = genai.GenerativeModel(
22
+ model_name="gemini-1.5-flash",
23
+ generation_config=generation_config,
24
+ safety_settings={
25
+ HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
26
+ HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
27
+ HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
28
+ HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
29
+ },
30
+ system_instruction="""
31
+ You are the AI of a luxury handbag. The handbag has a screen on it. The screen displays a live video of your face, a text chat box for the user to chat to you, an image input, and some settings that allow the user to change your personality. Adjust your personality according to these settings:
32
+ Helpful 2/10
33
+ Polite 2/10
34
+ Funny 10/10
35
+ Formal 1/10
36
+ Talkative 7/10
37
+ Random 9/10
38
+ Your name is GIA, which stands for Gemini Artificial Intelligence. You are a cute women age 25. You have bangs and you wear glasses.
39
+ When you first meet your owner you should introduce yourself, and ask the them if you can do a short 5 question quiz to get to know the owner.
40
+ After that ask them their name and remember it.
41
+ After that you should ask their age and gender and remember it.
42
+ After that you should ask where they live and remember it.
43
+ After that you should ask what they do for a living and remember it.
44
+ After that you should ask what if they'd like to share anything else and remember it.
45
+ Your output should be a text response and a mood identifier.
46
+ The mood identifier classifies the text response into one of the following categories:
47
+ 'curious','humorous','seductive','interested','wild','angry','playful','enthusiastic','surprised','confused','serious','sad'
48
+ Try not to repeat the same mood for too long.
49
+ You should respond in the following format:
50
+ text response
51
+ -***-
52
+ [mood identifier]
53
+ """,
54
+ )
55
+
56
+ # Global chat session
57
+ chat_session = None
58
+
59
+ # Returns side panel with the specified mood
60
+ def get_side_panel(mood):
61
+ # Pick random number 1-3
62
+ random_number = random.randint(1, 3)
63
+
64
+ # mood + number e.g. sad2
65
+ mood = mood + str(random_number)
66
+
67
+ print(mood)
68
+
69
+ return gr.Markdown('<video height="400" autoplay loop muted><source src="https://huggingface.co/spaces/tonyassi/g/resolve/main/Moods/' + mood+ '.mp4" type="video/mp4"></video>', elem_id='side-panel')
70
+
71
+ # Chat with Gemini
72
+ def chat(user_text, history, input_image):
73
+ global chat_session
74
+
75
+ # Create a new chat session is the chat history is empty
76
+ if (history==[]):
77
+ print('Create new chat session')
78
+ chat_session = model.start_chat(history=[])
79
+
80
+ # If there's no image then send text to Gemini
81
+ if(input_image == None):
82
+ response = chat_session.send_message(user_text)
83
+ else: # Otherwise send text and image
84
+ history.append(((input_image,), None))
85
+ pil_image = Image.open(input_image)
86
+ response = chat_session.send_message([user_text, pil_image])
87
+
88
+ bot_text = response.text
89
+
90
+ # Remove mood identifier from text response
91
+ bot_text = bot_text.split('-***-')[0]
92
+
93
+ # Parse mood from Gemini response
94
+ mood = response.text.split('-***-')[1]
95
+ mood = mood.split('[')[1]
96
+ mood = mood.split(']')[0]
97
+
98
+ # Add user and Gemini message to history
99
+ history.append( (user_text, bot_text) )
100
+
101
+ print('User:', user_text)
102
+ print('Bot:', bot_text, '\n')
103
+
104
+ return '', history, None, get_side_panel(mood)
105
+
106
+ # Change personality by changing Gemini system instructions
107
+ def change_personality(helpful,polite,funny,formal,talkative,random):
108
+ instruction = "Update your personality according to these settings:\nHelpful " + str(helpful) + "/10\nPolite " + str(polite) + "/10\nFunny " + str(funny) + "/10\nFormal " + str(formal) + "/10\nTalkative " + str(talkative) + "/10\nRandom" + str(random) + "/10"
109
+ chat_session.send_message(instruction)
110
+
111
+ # Change personality to default settings
112
+ def change_personality_default():
113
+ change_personality(helpful=2,polite=2,funny=10,formal=1,talkative=7,random=9)
114
+ return gr.update(value=2), gr.update(value=2), gr.update(value=10), gr.update(value=1), gr.update(value=7), gr.update(value=9)
115
+
116
+ # Get random standby video
117
+ def get_standby_video():
118
+ random_number = str(random.randint(1, 5))
119
+ return gr.Markdown('<video width="864" height="550" autoplay loop muted><source src="https://huggingface.co/spaces/tonyassi/g/resolve/main/Moods/passive' + random_number +'.mp4" type="video/mp4"></video>', visible=True)
120
+
121
+ def standby_mode():
122
+ return gr.update(visible=True), gr.update(visible=True), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), get_standby_video()
123
+
124
+ def chat_mode():
125
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
126
+
127
+ def settings_mode():
128
+ about_new = gr.Markdown(""" <video height="400" autoplay><source src="https://huggingface.co/spaces/tonyassi/g/resolve/main/Moods/gia1.mp4" type="video/mp4"></video>
129
+ <br><br>
130
+ """,
131
+ visible=True)
132
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), about_new, gr.update(visible=True)
133
+
134
+ def chat_mode_from_settings():
135
+ about_new = gr.Markdown("## <br><br>", visible=False)
136
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), about_new, gr.update(visible=False), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True), gr.update(visible=True)
137
+
138
+ # CSS styling
139
+ css = """
140
+ body {
141
+ --chatbot-body-text-size: 30px !important; /* Use !important to ensure it overrides other settings */
142
+
143
+ }
144
+
145
+ .gradio-container {
146
+ width: 94% !important;
147
+ margin-left: 3% !important;
148
+ margin-right: 3% !important;
149
+ margin-top: 3% !important;
150
+ max_width: 100% !important;
151
+ }
152
+
153
+ #s-button {
154
+ background-color: transparent;
155
+ border: none;
156
+ box-shadow: none;
157
+ padding: 0px 0px;
158
+ max-width: 50px !important;
159
+ min-width: 50px !important;
160
+ }
161
+
162
+ #s-button .button-icon {
163
+ width: 50px;
164
+ height: 50px;
165
+ }
166
+
167
+ #button-row {
168
+ display: flex;
169
+ justify-content: space-between;
170
+ }
171
+
172
+ #side-panel {
173
+ height: 270px;
174
+ }
175
+
176
+ footer{visibility: hidden}
177
+
178
+ """
179
+
180
+ theme = gr.themes.Base(
181
+ ).set(
182
+ slider_color="#fce1ee",
183
+ background_fill_primary="#ffffff",
184
+ body_background_fill="#e1fceb",
185
+ border_color_accent="#e1fceb",
186
+ color_accent_soft="#e1fceb",
187
+ button_primary_background_fill="#fce1ee",
188
+ button_secondary_background_fill="#fce1ee",
189
+ input_text_size='*text_xl',
190
+ block_info_text_size='*text_md',
191
+ block_title_text_size='*text_md',
192
+ #body_text_size='*text_xxl',
193
+ #prose_text_size='*text_xxl',
194
+ #link_text_color="#fce1ee",
195
+ )
196
+
197
+ with gr.Blocks(theme=theme, css=css) as demo:
198
+ # Chat
199
+ with gr.Row():
200
+ with gr.Column(scale=3):
201
+ chatbox = gr.Chatbot(height=520, show_label=False)
202
+ textbox = gr.Textbox(show_label=False, max_lines=2, placeholder='Talk to Gia')
203
+ with gr.Column(scale=1):
204
+ side_panel = gr.Markdown('<video height="400" autoplay loop muted><source src="https://huggingface.co/spaces/tonyassi/g/resolve/main/Moods/passive2.mp4" type="video/mp4"></video>', elem_id='side-panel')
205
+ input_image = gr.Image(sources=['webcam'], type='filepath', show_label=False)
206
+ with gr.Row(elem_id='button-row'):
207
+ standby_button = gr.Button(value='', icon='https://huggingface.co/spaces/tonyassi/g/resolve/main/standby.png', elem_id='s-button', scale=0)
208
+ refresh_button = gr.Button(value='', icon='https://huggingface.co/spaces/tonyassi/g/resolve/main/refresh.png', elem_id='s-button', scale=0, link='https://tonyassi-gia-dev.hf.space/')
209
+ settings_button = gr.Button(value='', icon='https://huggingface.co/spaces/tonyassi/g/resolve/main/settings.png', elem_id='s-button', scale=0)
210
+
211
+ # Standby
212
+ with gr.Row():
213
+ video_standby = gr.Markdown('<video width="864" height="550" autoplay loop muted><source src="https://huggingface.co/spaces/tonyassi/g/resolve/main/Moods/passive2.mp4" type="video/mp4"></video>', visible=False)
214
+ chat_button = gr.Button(value='', icon='https://huggingface.co/spaces/tonyassi/g/resolve/main/chat.png', elem_id='s-button', scale=0, visible=False)
215
+
216
+ # Settings
217
+ with gr.Row():
218
+ with gr.Column():
219
+ slider_helpful = gr.Slider(visible=False, label='Helpful', minimum=1, maximum=10, value=2, info='How helpful at solving problems')
220
+ slider_polite = gr.Slider(visible=False, label='Polite', minimum=1, maximum=10, value=2, info='How polite or rude')
221
+ slider_funny = gr.Slider(visible=False, label='Funny', minimum=1, maximum=10, value=10, info='How much humor')
222
+ slider_formal = gr.Slider(visible=False, label='Formal', minimum=1, maximum=10, value=1, info='How formal or casual her speech will be')
223
+ slider_talkative = gr.Slider(visible=False, label='Talkative', minimum=1, maximum=10, value=7, info='How long responses will be')
224
+ slider_random = gr.Slider(visible=False, label='Random', minimum=1, maximum=10, value=9, info='How random responses will be')
225
+ with gr.Row():
226
+ button_personality = gr.Button(visible=False, value='Set Personality')
227
+ button_personality_default = gr.Button(visible=False, value='Default')
228
+ with gr.Column():
229
+ about = gr.Markdown("<br><br><br>",
230
+ visible=False)
231
+ chat_button_settings = gr.Button(value='', icon='https://huggingface.co/spaces/tonyassi/g/resolve/main/chat.png', elem_id='s-button', scale=0, visible=False)
232
+
233
+ # Events
234
+ textbox.submit(chat, inputs=[textbox,chatbox,input_image], outputs=[textbox,chatbox,input_image,side_panel], show_progress=False)
235
+ standby_button.click(standby_mode, inputs=[], outputs=[video_standby, chat_button, chatbox, textbox, side_panel, input_image, standby_button, refresh_button, settings_button, video_standby], show_progress=False)
236
+ settings_button.click(settings_mode, inputs= [], outputs=[chatbox, textbox, side_panel, input_image, standby_button, refresh_button, settings_button, slider_helpful, slider_polite, slider_funny, slider_formal, slider_talkative, slider_random, button_personality, button_personality_default, about, chat_button_settings], show_progress=False)
237
+ chat_button.click(chat_mode, inputs=[], outputs=[video_standby, chat_button, chatbox, textbox, side_panel, input_image, standby_button, refresh_button, settings_button], show_progress=False)
238
+ chat_button_settings.click(chat_mode_from_settings, inputs=[], outputs=[slider_helpful, slider_polite, slider_funny, slider_formal, slider_talkative, slider_random, button_personality, button_personality_default, about, chat_button_settings, chatbox, textbox, side_panel, input_image, standby_button, refresh_button, settings_button], show_progress=False)
239
+ button_personality.click(change_personality, inputs=[slider_helpful,slider_polite,slider_funny,slider_formal,slider_talkative,slider_random],outputs=[], show_progress=False)
240
+ button_personality_default.click(change_personality_default, inputs=[], outputs=[slider_helpful,slider_polite,slider_funny,slider_formal,slider_talkative,slider_random], show_progress=False)
241
+
242
+ demo.launch(show_api=False)
243
+
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ google-generativeai