Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -110,4 +110,103 @@ interface = gr.Interface(
|
|
| 110 |
|
| 111 |
interface.launch()
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
|
|
|
|
| 110 |
|
| 111 |
interface.launch()
|
| 112 |
|
| 113 |
+
import gradio as gr
|
| 114 |
+
from transformers import pipeline
|
| 115 |
+
|
| 116 |
+
# Use a pre-trained chat model (no coding or training needed)
|
| 117 |
+
chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
|
| 118 |
+
|
| 119 |
+
# Simple customer knowledge base
|
| 120 |
+
knowledge = {
|
| 121 |
+
"reset password": "To reset your password, go to Settings > Account > Reset Password.",
|
| 122 |
+
"refund": "Refunds take 3–5 business days after approval.",
|
| 123 |
+
"support": "You can contact support at support@example.com."
|
| 124 |
+
}
|
| 125 |
+
|
| 126 |
+
# Simple rule-based guardrails
|
| 127 |
+
def apply_guardrails(answer):
|
| 128 |
+
if any(bad in answer.lower() for bad in ["credit card", "ssn", "password"]):
|
| 129 |
+
return "For your safety, I can’t discuss personal account details here."
|
| 130 |
+
return answer
|
| 131 |
+
|
| 132 |
+
# Main chatbot function
|
| 133 |
+
def chat_fn(message):
|
| 134 |
+
# Step 1: Look for matching info in knowledge base
|
| 135 |
+
for key, info in knowledge.items():
|
| 136 |
+
if key in message.lower():
|
| 137 |
+
answer = info
|
| 138 |
+
break
|
| 139 |
+
else:
|
| 140 |
+
# Step 2: Use AI model if we don’t know the answer
|
| 141 |
+
prompt = f"Customer question: {message}\nAnswer politely and professionally:"
|
| 142 |
+
answer = chatbot(prompt, max_new_tokens=100)[0]["generated_text"]
|
| 143 |
+
|
| 144 |
+
# Step 3: Guardrails check
|
| 145 |
+
safe_answer = apply_guardrails(answer)
|
| 146 |
+
return safe_answer
|
| 147 |
+
|
| 148 |
+
# ✨ Custom colorful CSS style ✨
|
| 149 |
+
custom_css = """
|
| 150 |
+
body {
|
| 151 |
+
background: linear-gradient(135deg, #6EE7B7, #3B82F6, #9333EA);
|
| 152 |
+
background-size: 400% 400%;
|
| 153 |
+
animation: gradientShift 10s ease infinite;
|
| 154 |
+
font-family: 'Poppins', sans-serif;
|
| 155 |
+
color: white;
|
| 156 |
+
}
|
| 157 |
+
@keyframes gradientShift {
|
| 158 |
+
0% { background-position: 0% 50%; }
|
| 159 |
+
50% { background-position: 100% 50%; }
|
| 160 |
+
100% { background-position: 0% 50%; }
|
| 161 |
+
}
|
| 162 |
+
.gradio-container {
|
| 163 |
+
background-color: rgba(255, 255, 255, 0.15) !important;
|
| 164 |
+
border-radius: 18px;
|
| 165 |
+
padding: 25px;
|
| 166 |
+
box-shadow: 0px 0px 30px rgba(255,255,255,0.4);
|
| 167 |
+
}
|
| 168 |
+
h1 {
|
| 169 |
+
background: linear-gradient(to right, #FF0080, #FF8C00, #40E0D0);
|
| 170 |
+
-webkit-background-clip: text;
|
| 171 |
+
-webkit-text-fill-color: transparent;
|
| 172 |
+
text-align: center;
|
| 173 |
+
font-weight: 800;
|
| 174 |
+
font-size: 2.4em;
|
| 175 |
+
letter-spacing: 1px;
|
| 176 |
+
}
|
| 177 |
+
textarea, input {
|
| 178 |
+
border-radius: 12px !important;
|
| 179 |
+
border: 2px solid #FFD700 !important;
|
| 180 |
+
background-color: rgba(255, 255, 255, 0.2) !important;
|
| 181 |
+
color: white !important;
|
| 182 |
+
font-size: 1em !important;
|
| 183 |
+
}
|
| 184 |
+
button {
|
| 185 |
+
background: linear-gradient(90deg, #FF0080, #FF8C00);
|
| 186 |
+
border: none !important;
|
| 187 |
+
border-radius: 10px !important;
|
| 188 |
+
color: white !important;
|
| 189 |
+
font-weight: bold;
|
| 190 |
+
font-size: 1em;
|
| 191 |
+
transition: all 0.3s ease;
|
| 192 |
+
}
|
| 193 |
+
button:hover {
|
| 194 |
+
transform: scale(1.05);
|
| 195 |
+
background: linear-gradient(90deg, #40E0D0, #FF8C00);
|
| 196 |
+
}
|
| 197 |
+
"""
|
| 198 |
+
|
| 199 |
+
# 🎨 Gradio interface (colorful + modern)
|
| 200 |
+
interface = gr.Interface(
|
| 201 |
+
fn=chat_fn,
|
| 202 |
+
inputs="text",
|
| 203 |
+
outputs=gr.Textbox(lines=8, label="💬 Chatbot Response"),
|
| 204 |
+
title="💎 Account & Fund Support Chatbot 💬",
|
| 205 |
+
description="Ask questions about your account, refunds, or support — your AI assistant is here to help!",
|
| 206 |
+
css=custom_css
|
| 207 |
+
)
|
| 208 |
+
|
| 209 |
+
interface.launch()
|
| 210 |
+
|
| 211 |
+
|
| 212 |
|