Mood-Swings / app.py
prithivMLmods's picture
Update app.py
5558c42 verified
raw
history blame
2.45 kB
import os
import gradio as gr
import google.generativeai as genai
from dotenv import load_dotenv
import time
css = '''
.gradio-container{max-width: 950px !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."
}
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:]
retry_attempts = 10
for attempt in range(retry_attempts):
try:
chat_session = model.start_chat()
response = chat_session.send_message("\n".join(chat_history))
return response.text, chat_history
except Exception as e:
if attempt < retry_attempts - 1:
time.sleep(2)
continue
else:
return f"Error after {retry_attempts} attempts: {str(e)}", chat_history
with gr.Blocks(css=css, theme="bethecloud/storj_theme") as iface:
chat_input = gr.Textbox(lines=2, label="Chatbot", placeholder="Enter your message here...")
chat_history_state = gr.State([])
response_output = gr.Textbox(label="Response")
mood_selector = gr.Radio(choices=list(mood_prompts.keys()), value="Professional", label="Select Mood")
generate_button = gr.Button("Generate Response")
generate_button.click(
fn=generate_response,
inputs=[chat_input, chat_history_state, mood_selector],
outputs=[response_output, chat_history_state]
)
iface.launch()