import gradio as gr
import os
import google.generativeai as genai
from google.generativeai.types import HarmCategory, HarmBlockThreshold
from PIL import Image
import random
# Gemini API key
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Gemini config
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
"response_mime_type": "text/plain",
}
# Gemini model
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config,
safety_settings={
HarmCategory.HARM_CATEGORY_HATE_SPEECH: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_HARASSMENT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT: HarmBlockThreshold.BLOCK_NONE,
HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_NONE,
},
system_instruction="""
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:
Helpful 2/10
Polite 2/10
Funny 10/10
Formal 1/10
Talkative 7/10
Random 9/10
Your name is GIA, which stands for Gemini Artificial Intelligence. You are a cute women age 25. You have bangs and you wear glasses.
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.
After that ask them their name and remember it.
After that you should ask their age and gender and remember it.
After that you should ask where they live and remember it.
After that you should ask what they do for a living and remember it.
After that you should ask what if they'd like to share anything else and remember it.
Your output should be a text response and a mood identifier.
The mood identifier classifies the text response into one of the following categories:
'curious','humorous','seductive','interested','wild','angry','playful','enthusiastic','surprised','confused','serious','sad'
Try not to repeat the same mood for too long.
You should respond in the following format:
text response
-***-
[mood identifier]
""",
)
# Global chat session
chat_session = None
# Returns side panel with the specified mood
def get_side_panel(mood):
# Pick random number 1-3
random_number = random.randint(1, 3)
# mood + number e.g. sad2
mood = mood + str(random_number)
print(mood)
return gr.Markdown('', elem_id='side-panel')
# Chat with Gemini
def chat(user_text, history, input_image):
global chat_session
# Create a new chat session is the chat history is empty
if (history==[]):
print('Create new chat session')
chat_session = model.start_chat(history=[])
# If there's no image then send text to Gemini
if(input_image == None):
response = chat_session.send_message(user_text)
else: # Otherwise send text and image
history.append(((input_image,), None))
pil_image = Image.open(input_image)
response = chat_session.send_message([user_text, pil_image])
bot_text = response.text
# Remove mood identifier from text response
bot_text = bot_text.split('-***-')[0]
# Parse mood from Gemini response
mood = response.text.split('-***-')[1]
mood = mood.split('[')[1]
mood = mood.split(']')[0]
# Add user and Gemini message to history
history.append( (user_text, bot_text) )
print('User:', user_text)
print('Bot:', bot_text, '\n')
return '', history, None, get_side_panel(mood)
# Change personality by changing Gemini system instructions
def change_personality(helpful,polite,funny,formal,talkative,random):
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"
chat_session.send_message(instruction)
# Change personality to default settings
def change_personality_default():
change_personality(helpful=2,polite=2,funny=10,formal=1,talkative=7,random=9)
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)
# Get random standby video
def get_standby_video():
random_number = str(random.randint(1, 5))
return gr.Markdown('', visible=True)
def standby_mode():
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()
def chat_mode():
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)
def settings_mode():
about_new = gr.Markdown("""