Spaces:
Running
Running
Update chatbot.py
Browse files- chatbot.py +36 -3
chatbot.py
CHANGED
@@ -26,7 +26,8 @@ class ChatbotManager:
|
|
26 |
|
27 |
if api_key:
|
28 |
genai.configure(api_key=api_key)
|
29 |
-
|
|
|
30 |
self.chat = self.model.start_chat(history=[])
|
31 |
else:
|
32 |
self.model = None
|
@@ -77,6 +78,8 @@ class ChatbotManager:
|
|
77 |
else:
|
78 |
# Fallback response if Gemini isn't available
|
79 |
return self._generate_fallback_response(prompt)
|
|
|
|
|
80 |
except Exception as e:
|
81 |
return f"⚠️ Error generating response: {str(e)}"
|
82 |
|
@@ -90,6 +93,7 @@ Rules:
|
|
90 |
- Provide concrete examples when possible
|
91 |
- Structure responses with clear sections when appropriate
|
92 |
- Suggest next steps or additional considerations
|
|
|
93 |
|
94 |
User query: {user_input}
|
95 |
|
@@ -100,7 +104,7 @@ Please provide a comprehensive response that addresses the user's needs:"""
|
|
100 |
business_topics = {
|
101 |
"strategy": "For business strategy, consider analyzing your market position, competitors, and unique value proposition.",
|
102 |
"marketing": "Marketing tips: Focus on your target audience, create valuable content, and measure campaign performance.",
|
103 |
-
"finance": "
|
104 |
"product": "Product development should start with customer needs validation before building."
|
105 |
}
|
106 |
|
@@ -109,4 +113,33 @@ Please provide a comprehensive response that addresses the user's needs:"""
|
|
109 |
if topic in prompt_lower:
|
110 |
return response
|
111 |
|
112 |
-
return "I can provide advice on business strategy, marketing,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
if api_key:
|
28 |
genai.configure(api_key=api_key)
|
29 |
+
# Updated model name to 'gemini-1.0-pro'
|
30 |
+
self.model = genai.GenerativeModel('gemini-1.0-pro')
|
31 |
self.chat = self.model.start_chat(history=[])
|
32 |
else:
|
33 |
self.model = None
|
|
|
78 |
else:
|
79 |
# Fallback response if Gemini isn't available
|
80 |
return self._generate_fallback_response(prompt)
|
81 |
+
except genai.types.generation_types.BlockedPromptException:
|
82 |
+
return "⚠️ My response was blocked for safety reasons. Please try rephrasing your question."
|
83 |
except Exception as e:
|
84 |
return f"⚠️ Error generating response: {str(e)}"
|
85 |
|
|
|
93 |
- Provide concrete examples when possible
|
94 |
- Structure responses with clear sections when appropriate
|
95 |
- Suggest next steps or additional considerations
|
96 |
+
- Never provide financial, legal, or medical advice
|
97 |
|
98 |
User query: {user_input}
|
99 |
|
|
|
104 |
business_topics = {
|
105 |
"strategy": "For business strategy, consider analyzing your market position, competitors, and unique value proposition.",
|
106 |
"marketing": "Marketing tips: Focus on your target audience, create valuable content, and measure campaign performance.",
|
107 |
+
"finance": "For financial questions, consult with a qualified financial advisor.",
|
108 |
"product": "Product development should start with customer needs validation before building."
|
109 |
}
|
110 |
|
|
|
113 |
if topic in prompt_lower:
|
114 |
return response
|
115 |
|
116 |
+
return "I can provide advice on business strategy, marketing, and product development. Please ask a specific question about one of these areas."
|
117 |
+
|
118 |
+
def render_chat_interface(self):
|
119 |
+
"""Render the complete chat interface"""
|
120 |
+
st.header("💬 AI Business Mentor")
|
121 |
+
|
122 |
+
# Display chat history
|
123 |
+
for message in self.get_chat_history():
|
124 |
+
with st.chat_message(message["role"]):
|
125 |
+
st.markdown(message["content"])
|
126 |
+
|
127 |
+
# User input
|
128 |
+
if prompt := st.chat_input("Ask about business strategy, marketing, or products..."):
|
129 |
+
self.add_message("user", prompt)
|
130 |
+
|
131 |
+
# Display user message immediately
|
132 |
+
with st.chat_message("user"):
|
133 |
+
st.markdown(prompt)
|
134 |
+
|
135 |
+
# Generate and display assistant response
|
136 |
+
with st.chat_message("assistant"):
|
137 |
+
with st.spinner("Analyzing..."):
|
138 |
+
response = self.generate_business_response(prompt)
|
139 |
+
st.markdown(response)
|
140 |
+
|
141 |
+
# Add assistant response to history
|
142 |
+
self.add_message("assistant", response)
|
143 |
+
|
144 |
+
# Auto-rerun to update the display
|
145 |
+
st.rerun()
|