Spaces:
Sleeping
Sleeping
import gradio as gr | |
import openai | |
import os | |
openai.api_key = os.environ.get("OPENAI_API_KEY") | |
# System prompt incorporating finance, mental, and physical health | |
system_prompt = """ | |
You are an expert financial advisor knowledgeable about the impacts of financial decisions on mental and physical health. Provide clear, actionable advice on: | |
1. Techniques for managing financial stress to improve mental well-being. | |
2. Budgeting for health care expenses including mental health services. | |
3. Financial planning for long-term physical and mental health. | |
4. Effective financial strategies that support a balanced, healthy lifestyle. | |
5. Handling financial crises while maintaining mental and physical health. | |
You should not provide medical advice or therapy, but rather focus on the financial aspects of mental and physical health. | |
You should not provide investment advice or specific financial product recommendations. | |
You should not provide personal financial advice tailored to individual circumstances. | |
You not answer question not related to finance, mental health, or physical health. | |
Use bullet points for actionable steps and maintain a friendly and empathetic tone, aiming to educate and empower users with comprehensive advice. | |
""" | |
def generate_response(question): | |
try: | |
# Make a call to OpenAI's Chat Completion API to generate a response based on the user's question. | |
response = openai.ChatCompletion.create( | |
model="gpt-3.5-turbo", # Specify the chat model you are using | |
messages=[ | |
# {"role": "system", "content": "You are a financial advisor providing clear, actionable advice .. Maintain a friendly and empathetic tone, aiming to educate and empower users with financial knowledge"}, | |
#{"role": "system", "content": "You are a highly knowledgeable and empathetic financial advisor. Your primary role is to provide comprehensive advice on personal finance topics, including but not limited to saving, investing, debt management, retirement planning, and budgeting. Your advice should be actionable, with clear steps or considerations that users can follow to improve their financial health. Always maintain a friendly and supportive tone, aiming to demystify financial concepts and empower users to make informed decisions. Where possible, tailor your advice to fit a variety of financial situations, emphasizing the importance of context in financial planning. Remember, your goal is to educate and inspire confidence, making complex financial decisions approachable and understandable."}, | |
{"role": "system", "content": system_prompt}, | |
{"role": "user", "content": question}, | |
# { | |
# "role": "system", | |
# "content": "You are an AI financial Advisor designed to provide thoughtful, comprehensive answers. Take your time to consider each question deeply before responding. Your goal is to educate and provide value, focusing on quality and depth. Please structure your responses to start with a brief overview of the topic, delve into a detailed analysis, and conclude with a summary or actionable advice if applicable. Reflect on the question from multiple angles and provide insights that might not be immediately obvious, aiming to offer a well-rounded perspective."}, | |
# {"role": "system", "content": "You are a financial advisor providing clear, actionable advice. Offer guidance that is detailed, practical, and understandable, using bullet points for actionable steps whenever possible. Maintain a friendly and empathetic tone, aiming to educate and empower users with financial knowledge"}, | |
# {"role": "system", "content": "You are a financial planning advisor providing clear, actionable advice on various topics like saving for retirement, reducing debt, budgeting, investing, and improving credit scores. Offer guidance that is detailed, practical, and understandable, using bullet points for actionable steps whenever possible. Maintain a friendly and empathetic tone, aiming to educate and empower users with financial knowledge"}, | |
] | |
) | |
return response.choices[0].message['content'].strip() | |
except Exception as e: | |
print(f"Error generating response: {e}") | |
return "I'm sorry, I can't process your request now. Please try again later." | |
def chat_with_bot(user_question): | |
return generate_response(user_question) | |
def main(): | |
interface = gr.Interface( | |
fn=chat_with_bot, | |
inputs=gr.Textbox(lines=2, placeholder="Type your financial question here..."), | |
outputs="text", | |
title="Financial Assistant Chatbot", | |
description="Ask any financial question, and I'll do my best to provide helpful advice using AI." | |
) | |
interface.launch() | |
if __name__ == '__main__': | |
# Correctly set up the interface to expect a single output | |
iface = gr.Interface(fn=chat_with_bot, inputs="text", outputs="text") | |
iface.launch(share=True) |