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