Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import chainlit as cl
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 🔑 Configure Gemini API
|
| 6 |
+
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 7 |
+
|
| 8 |
+
# 🧠 Simple memory per user
|
| 9 |
+
memory = {}
|
| 10 |
+
|
| 11 |
+
# 🎭 Role tone presets
|
| 12 |
+
ROLES = {
|
| 13 |
+
"customer": "You are NovaTech’s kind and empathetic customer support agent.",
|
| 14 |
+
"employee": "You are NovaTech’s helpful internal assistant for company staff.",
|
| 15 |
+
"manager": "You are NovaTech’s smart business insights assistant. Be concise and professional."
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
@cl.on_chat_start
|
| 19 |
+
async def start():
|
| 20 |
+
await cl.Message(
|
| 21 |
+
content="👋 Welcome to **NovaTech Solutions Virtual Assistant!**\nPlease tell me your role: customer, employee, or manager."
|
| 22 |
+
).send()
|
| 23 |
+
|
| 24 |
+
@cl.on_message
|
| 25 |
+
async def main(message: cl.Message):
|
| 26 |
+
user = message.author or "guest"
|
| 27 |
+
user_mem = memory.get(user, {"role": "customer", "history": ""})
|
| 28 |
+
|
| 29 |
+
text = message.content.strip().lower()
|
| 30 |
+
|
| 31 |
+
# Role detection / setting
|
| 32 |
+
if text in ROLES:
|
| 33 |
+
user_mem["role"] = text
|
| 34 |
+
memory[user] = user_mem
|
| 35 |
+
await cl.Message(
|
| 36 |
+
content=f"✅ Got it! You are a **{text}**. How can I assist you today?"
|
| 37 |
+
).send()
|
| 38 |
+
return
|
| 39 |
+
|
| 40 |
+
# Build prompt
|
| 41 |
+
prompt = f"""{ROLES[user_mem['role']]}
|
| 42 |
+
Previous chat: {user_mem['history']}
|
| 43 |
+
User: {message.content}
|
| 44 |
+
Reply as NovaTech’s AI assistant with professionalism and empathy."""
|
| 45 |
+
|
| 46 |
+
model = genai.GenerativeModel("gemini-1.5-flash")
|
| 47 |
+
response = model.generate_content(prompt)
|
| 48 |
+
reply = response.text
|
| 49 |
+
|
| 50 |
+
# Update memory
|
| 51 |
+
user_mem["history"] += f"User: {message.content}\nAI: {reply}\n"
|
| 52 |
+
memory[user] = user_mem
|
| 53 |
+
|
| 54 |
+
await cl.Message(content=reply).send()
|