Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -5,7 +5,7 @@ from openai import OpenAI
|
|
| 5 |
# --- Configuration ---
|
| 6 |
|
| 7 |
# API key MUST be provided via an environment variable (e.g., GEMINI_API_KEY).
|
| 8 |
-
DEFAULT_API_KEY = "
|
| 9 |
GEMINI_API_KEY_ENV_VAR = os.environ.get("GEMINI_API_KEY", DEFAULT_API_KEY)
|
| 10 |
|
| 11 |
# Model configuration
|
|
@@ -14,77 +14,61 @@ GEMINI_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/"
|
|
| 14 |
|
| 15 |
def chatbot_query(message, history, rag_context):
|
| 16 |
"""
|
| 17 |
-
Handles the chat query using the provided context
|
|
|
|
| 18 |
"""
|
| 19 |
|
|
|
|
|
|
|
|
|
|
| 20 |
final_api_key = GEMINI_API_KEY_ENV_VAR
|
|
|
|
| 21 |
|
| 22 |
if not final_api_key:
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
You are an expert personal financial assistant. Your goal is to analyze the user's expenses
|
| 42 |
-
and provide advice based on the context provided in the data box below.
|
| 43 |
-
Be helpful, empathetic, and focus on practical recommendations.
|
| 44 |
-
|
| 45 |
-
--- RAG/CONTEXT DATA ---
|
| 46 |
-
{rag_context}
|
| 47 |
-
------------------------
|
| 48 |
-
|
| 49 |
-
Address the user's question based on this data. If the data is empty or irrelevant,
|
| 50 |
-
provide general financial advice.
|
| 51 |
-
"""
|
| 52 |
-
|
| 53 |
-
# Prepare chat history for API call
|
| 54 |
-
messages = [{"role": "system", "content": system_prompt}]
|
| 55 |
-
|
| 56 |
-
# Append past conversation turns
|
| 57 |
-
for user_msg, ai_msg in history:
|
| 58 |
-
messages.append({"role": "user", "content": user_msg})
|
| 59 |
-
messages.append({"role": "assistant", "content": ai_msg})
|
| 60 |
-
|
| 61 |
-
# Append current user message
|
| 62 |
-
messages.append({"role": "user", "content": message})
|
| 63 |
|
| 64 |
-
|
| 65 |
-
# API Call
|
| 66 |
-
response_stream = client.chat.completions.create(
|
| 67 |
-
model=GEMINI_MODEL_NAME,
|
| 68 |
-
messages=messages,
|
| 69 |
-
temperature=0.7,
|
| 70 |
-
stream=True # Use streaming for better user experience
|
| 71 |
-
)
|
| 72 |
-
|
| 73 |
-
full_response = ""
|
| 74 |
-
# Stream the response back to Gradio
|
| 75 |
-
for chunk in response_stream:
|
| 76 |
-
content = chunk.choices[0].delta.content
|
| 77 |
-
if content is not None:
|
| 78 |
-
full_response += content
|
| 79 |
-
history.append([message, full_response])
|
| 80 |
-
yield history
|
| 81 |
-
|
| 82 |
-
except Exception as e:
|
| 83 |
-
full_response = f"An API error occurred: {e}"
|
| 84 |
-
history.append([message, full_response])
|
| 85 |
-
yield history
|
| 86 |
-
|
| 87 |
-
# Final return (although streaming already handled the updates)
|
| 88 |
return history
|
| 89 |
|
| 90 |
|
|
@@ -107,9 +91,10 @@ with gr.Blocks(title="Simple Personal Finance Chatbot") as demo:
|
|
| 107 |
value="Monthly income: 85,000. Fixed expenses (rent, bills, SIP): 40,000. Discretionary spending: Food delivery: 10,000, Shopping: 8,000, Entertainment: 4,000. I want to save for a vacation next year."
|
| 108 |
)
|
| 109 |
|
| 110 |
-
# Chatbot Interface
|
| 111 |
chatbot = gr.Chatbot(
|
| 112 |
label="Financial Coach Chat",
|
|
|
|
| 113 |
height=350,
|
| 114 |
avatar_images=[None, "https://storage.googleapis.com/huggingface-org/spaces/gradio-pr-bot/chatbot-avatar.png"]
|
| 115 |
)
|
|
@@ -126,19 +111,22 @@ with gr.Blocks(title="Simple Personal Finance Chatbot") as demo:
|
|
| 126 |
submit_btn = gr.Button("Ask the Coach", variant="primary")
|
| 127 |
|
| 128 |
# Connect components
|
| 129 |
-
|
|
|
|
|
|
|
| 130 |
fn=chatbot_query,
|
| 131 |
inputs=[msg, chatbot, rag_context],
|
| 132 |
outputs=[chatbot],
|
| 133 |
-
# Clears the input message box after submitting
|
| 134 |
queue=False
|
| 135 |
-
).then(lambda: "", None, msg)
|
| 136 |
|
|
|
|
| 137 |
msg.submit(
|
| 138 |
fn=chatbot_query,
|
| 139 |
inputs=[msg, chatbot, rag_context],
|
| 140 |
outputs=[chatbot],
|
| 141 |
queue=False
|
| 142 |
-
).then(lambda: "", None, msg)
|
| 143 |
|
|
|
|
| 144 |
clear_btn.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
|
|
|
|
| 5 |
# --- Configuration ---
|
| 6 |
|
| 7 |
# API key MUST be provided via an environment variable (e.g., GEMINI_API_KEY).
|
| 8 |
+
DEFAULT_API_KEY = ""
|
| 9 |
GEMINI_API_KEY_ENV_VAR = os.environ.get("GEMINI_API_KEY", DEFAULT_API_KEY)
|
| 10 |
|
| 11 |
# Model configuration
|
|
|
|
| 14 |
|
| 15 |
def chatbot_query(message, history, rag_context):
|
| 16 |
"""
|
| 17 |
+
Handles the chat query, using the provided RAG context, and returns the updated history.
|
| 18 |
+
History is managed as a list of dictionaries (type='messages').
|
| 19 |
"""
|
| 20 |
|
| 21 |
+
# 1. Add the current user message to the history list
|
| 22 |
+
history.append({"role": "user", "content": message})
|
| 23 |
+
|
| 24 |
final_api_key = GEMINI_API_KEY_ENV_VAR
|
| 25 |
+
full_response = "The AI is currently unavailable."
|
| 26 |
|
| 27 |
if not final_api_key:
|
| 28 |
+
full_response = "ERROR: API Key is missing. Please ensure the 'GEMINI_API_KEY' environment variable is set."
|
| 29 |
+
else:
|
| 30 |
+
try:
|
| 31 |
+
# Initialize the client using the Gemini base URL and the environment key
|
| 32 |
+
client = OpenAI(
|
| 33 |
+
base_url=GEMINI_API_BASE_URL,
|
| 34 |
+
api_key=final_api_key,
|
| 35 |
+
timeout=60.0
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# Construct System Prompt using the RAG Context
|
| 39 |
+
system_prompt = f"""
|
| 40 |
+
You are an expert personal financial assistant. Your goal is to analyze the user's expenses
|
| 41 |
+
and provide advice based on the context provided in the data box below.
|
| 42 |
+
Be helpful, empathetic, and focus on practical recommendations.
|
| 43 |
+
|
| 44 |
+
--- RAG/CONTEXT DATA ---
|
| 45 |
+
{rag_context}
|
| 46 |
+
------------------------
|
| 47 |
+
|
| 48 |
+
Address the user's question based on this data. If the data is empty or irrelevant,
|
| 49 |
+
provide general financial advice.
|
| 50 |
+
"""
|
| 51 |
+
|
| 52 |
+
# Prepare messages list for API call: System prompt + full conversation history
|
| 53 |
+
messages = [{"role": "system", "content": system_prompt}] + history
|
| 54 |
+
|
| 55 |
+
# API Call (Non-streaming)
|
| 56 |
+
response = client.chat.completions.create(
|
| 57 |
+
model=GEMINI_MODEL_NAME,
|
| 58 |
+
messages=messages,
|
| 59 |
+
temperature=0.7,
|
| 60 |
+
stream=False
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
full_response = response.choices[0].message.content
|
| 64 |
+
|
| 65 |
+
except Exception as e:
|
| 66 |
+
full_response = f"An API error occurred: {e}"
|
| 67 |
|
| 68 |
+
# 2. Add the AI response to the history list
|
| 69 |
+
history.append({"role": "assistant", "content": full_response})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
+
# 3. Return the updated history list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return history
|
| 73 |
|
| 74 |
|
|
|
|
| 91 |
value="Monthly income: 85,000. Fixed expenses (rent, bills, SIP): 40,000. Discretionary spending: Food delivery: 10,000, Shopping: 8,000, Entertainment: 4,000. I want to save for a vacation next year."
|
| 92 |
)
|
| 93 |
|
| 94 |
+
# Chatbot Interface - FIX: Setting type='messages' to address the user warning
|
| 95 |
chatbot = gr.Chatbot(
|
| 96 |
label="Financial Coach Chat",
|
| 97 |
+
type='messages',
|
| 98 |
height=350,
|
| 99 |
avatar_images=[None, "https://storage.googleapis.com/huggingface-org/spaces/gradio-pr-bot/chatbot-avatar.png"]
|
| 100 |
)
|
|
|
|
| 111 |
submit_btn = gr.Button("Ask the Coach", variant="primary")
|
| 112 |
|
| 113 |
# Connect components
|
| 114 |
+
|
| 115 |
+
# Submits the question, updates the chatbot history, and then clears the input box
|
| 116 |
+
submit_event = submit_btn.click(
|
| 117 |
fn=chatbot_query,
|
| 118 |
inputs=[msg, chatbot, rag_context],
|
| 119 |
outputs=[chatbot],
|
|
|
|
| 120 |
queue=False
|
| 121 |
+
).then(lambda: "", None, msg, queue=False) # Clears the input box
|
| 122 |
|
| 123 |
+
# Also bind to the Enter key press
|
| 124 |
msg.submit(
|
| 125 |
fn=chatbot_query,
|
| 126 |
inputs=[msg, chatbot, rag_context],
|
| 127 |
outputs=[chatbot],
|
| 128 |
queue=False
|
| 129 |
+
).then(lambda: "", None, msg, queue=False)
|
| 130 |
|
| 131 |
+
# Clear button action
|
| 132 |
clear_btn.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
|