Spaces:
Sleeping
Sleeping
import os | |
import gradio as gr | |
import google.generativeai as genai | |
from dotenv import load_dotenv | |
import time | |
DESCRIPTIONx = """## GEMINI::GEN β | |
""" | |
css = ''' | |
.gradio-container{max-width: 680px !important} | |
h1{text-align:center} | |
footer { | |
visibility: hidden | |
} | |
''' | |
load_dotenv() | |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
genai.configure(api_key=GEMINI_API_KEY) | |
generation_config = { | |
"temperature": 0.7, | |
"top_p": 0.95, | |
"top_k": 64, | |
"max_output_tokens": 512, | |
"response_mime_type": "text/plain", | |
} | |
mood_prompts = { | |
"Fun": "Respond in a light-hearted, playful manner.", | |
"Serious": "Respond in a thoughtful, serious tone.", | |
"Professional": "Respond in a formal, professional manner.", | |
"Upset": "Respond in a slightly irritated, upset tone.", | |
"Empathetic": "Respond in a warm and understanding tone.", | |
"Optimistic": "Respond in a positive, hopeful manner.", | |
"Sarcastic": "Respond with a hint of sarcasm.", | |
"Motivational": "Respond with encouragement and motivation.", | |
"Curious": "Respond with a sense of wonder and curiosity.", | |
"Humorous": "Respond with a touch of humor.", | |
"Cautious": "Respond with careful consideration and caution.", | |
"Assertive": "Respond with confidence and assertiveness.", | |
"Friendly": "Respond in a warm and friendly manner.", | |
"Romantic": "Respond with affection and romance.", | |
"Nostalgic": "Respond with a sense of longing for the past.", | |
"Grateful": "Respond with gratitude and appreciation.", | |
"Inspirational": "Respond with inspiration and positivity.", | |
"Casual": "Respond in a relaxed and informal tone.", | |
"Formal": "Respond with a high level of formality.", | |
"Pessimistic": "Respond with a focus on potential negatives.", | |
"Excited": "Respond with enthusiasm and excitement.", | |
"Melancholic": "Respond with a sense of sadness or longing.", | |
"Confident": "Respond with self-assurance and confidence.", | |
"Suspicious": "Respond with caution and doubt.", | |
"Reflective": "Respond with deep thought and introspection.", | |
"Joyful": "Respond with happiness and joy.", | |
"Mysterious": "Respond with an air of mystery and intrigue.", | |
"Aggressive": "Respond with force and intensity.", | |
"Calm": "Respond with a sense of peace and tranquility.", | |
"Gloomy": "Respond with a sense of sadness or pessimism.", | |
"Encouraging": "Respond with words of support and encouragement.", | |
"Sympathetic": "Respond with understanding and compassion.", | |
"Disappointed": "Respond with a tone of disappointment.", | |
"Proud": "Respond with a sense of pride and accomplishment.", | |
"Playful": "Respond in a fun and playful manner.", | |
"Inquisitive": "Respond with curiosity and interest.", | |
"Supportive": "Respond with reassurance and support.", | |
"Reluctant": "Respond with hesitation and reluctance.", | |
"Confused": "Respond with uncertainty and confusion.", | |
"Energetic": "Respond with high energy and enthusiasm.", | |
"Relaxed": "Respond with a calm and laid-back tone.", | |
"Grumpy": "Respond with a touch of irritation.", | |
"Hopeful": "Respond with a sense of hope and optimism.", | |
"Indifferent": "Respond with a lack of strong emotion.", | |
"Surprised": "Respond with shock and astonishment.", | |
"Tense": "Respond with a sense of urgency or anxiety.", | |
"Enthusiastic": "Respond with eagerness and excitement.", | |
"Worried": "Respond with concern and apprehension." | |
} | |
def generate_response(user_input, chat_history, mood): | |
updated_system_content = f"{mood_prompts[mood]}" | |
model = genai.GenerativeModel( | |
model_name="gemini-1.5-pro", | |
generation_config=generation_config, | |
system_instruction=updated_system_content, | |
) | |
chat_history.append(user_input) | |
chat_history = chat_history[-10:] | |
try: | |
chat_session = model.start_chat() | |
response = chat_session.send_message("\n".join(chat_history)) | |
return response.text, chat_history | |
except Exception as e: | |
return f"Error: {str(e)}", chat_history | |
with gr.Blocks(css=css, theme="allenai/gradio-theme") as iface: | |
gr.Markdown(DESCRIPTIONx) | |
chat_input = gr.Textbox(lines=4, label="Chatbot", placeholder="Enter your message here...") | |
chat_history_state = gr.State([]) | |
response_output = gr.Textbox(label="Response", lines=4) | |
generate_button = gr.Button("Submit") | |
mood_selector = gr.Radio(choices=list(mood_prompts.keys()), value="Professional", label="Select Mood") | |
generate_button.click( | |
fn=generate_response, | |
inputs=[chat_input, chat_history_state, mood_selector], | |
outputs=[response_output, chat_history_state] | |
) | |
iface.launch() |