Spaces:
Runtime error
Runtime error
File size: 3,203 Bytes
c045b61 fee1871 ec978d4 fee1871 ec978d4 53b9316 fee1871 53b9316 ec978d4 53b9316 ec978d4 fee1871 ec978d4 53b9316 fee1871 53b9316 ec978d4 53b9316 c045b61 53b9316 fee1871 f18ee53 53b9316 ec978d4 fee1871 53b9316 ec978d4 53b9316 ec978d4 53b9316 ec978d4 53b9316 fee1871 53b9316 f18ee53 53b9316 c045b61 53b9316 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
import gradio as gr
import spacy
import json
from datetime import datetime
# Load spaCy language models
try:
nlp_ar = spacy.blank("ar") # Arabic
nlp_en = spacy.blank("en") # English
except Exception as e:
print(f"Error loading spaCy models: {e}")
nlp_ar = None
nlp_en = None
# Function to detect language using spaCy
def detect_language(text):
if not text.strip():
return "unknown"
# Check if the text contains Arabic characters using spaCy
if nlp_ar and any(token.is_alpha for token in nlp_ar(text)):
return "ar"
# Check for English
if nlp_en and any(token.is_alpha for token in nlp_en(text)):
return "en"
return "unknown"
# Placeholder customer service functions
def get_enhanced_response(intent, lang):
responses = {
"balance": {"ar": "رصيدك هو 1000 جنيه سوداني.", "en": "Your balance is 1000 SDG."},
"lost_card": {"ar": "يرجى الاتصال بالبنك Ùورًا للإبلاغ عن بطاقة Ù…Ùقودة.", "en": "Please contact the bank immediately to report a lost card."}
}
return responses.get(intent, {}).get(lang, "I'm not sure how to answer that.")
# Log interactions
def log_interaction(user_message, bot_response, intent, language):
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
log_entry = {"timestamp": timestamp, "user_message": user_message, "bot_response": bot_response, "intent": intent, "language": language}
with open("/mnt/data/chat_logs.jsonl", "a", encoding="utf-8") as f:
f.write(json.dumps(log_entry, ensure_ascii=False) + "\n")
# Intent classification function
def classify_intent(message):
keywords = {
"balance": ["balance", "رصيد"],
"lost_card": ["lost", "card", "بطاقة", "ضائعة"]
}
for intent, words in keywords.items():
if any(word in message.lower() for word in words):
return intent
return "unknown"
# Response function
def respond(message):
language = detect_language(message)
intent = classify_intent(message)
response = get_enhanced_response(intent, language)
log_interaction(message, response, intent, language)
return response
# Chatbot interface with Gradio
def chatbot_interface(user_input, chat_history):
if not user_input.strip():
return "", chat_history
response = respond(user_input)
chat_history.append(("User", user_input))
chat_history.append(("Bot", response))
return "", chat_history
# Gradio UI
with gr.Blocks() as demo:
gr.Markdown("# Banking Chatbot - Now with spaCy")
chat_history = gr.State([])
chatbot = gr.Chatbot()
user_input = gr.Textbox(placeholder="Type your message...")
send_btn = gr.Button("Send")
send_btn.click(fn=chatbot_interface, inputs=[user_input, chat_history], outputs=[user_input, chatbot])
user_input.submit(fn=chatbot_interface, inputs=[user_input, chat_history], outputs=[user_input, chatbot])
if __name__ == "__main__":
demo.launch() |