Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import spacy | |
| from difflib import SequenceMatcher | |
| import gradio as gr | |
| # Load your knowledge base | |
| kb_df = pd.read_csv("clean_basic_conversations.csv", delimiter='|') | |
| # Load spaCy model safely | |
| try: | |
| nlp = spacy.load("en_core_web_sm") | |
| except: | |
| from spacy.cli import download | |
| download("en_core_web_sm") | |
| nlp = spacy.load("en_core_web_sm") | |
| from chatbot_logic import get_response, find_best_match_with_thematic_roles | |
| # Main chat function with explanation | |
| def chat_with_explanation(user_input, history=[]): | |
| if user_input.strip().lower() == "bye": | |
| bot_response = "Goodbye!" | |
| explanation = "The bot detected a farewell keyword and responded accordingly." | |
| else: | |
| bot_response, explanation = get_response(user_input, kb_df) | |
| history.append((user_input, bot_response)) | |
| return history, explanation, history | |
| # Gradio Blocks UI | |
| def build_interface(): | |
| with gr.Blocks(title="Context-Aware Smart Chatbot") as demo: | |
| gr.Markdown(""" | |
| # 🤖 Smart Thematic Chatbot | |
| This chatbot uses **Thematic Role Extraction** and **String Similarity** to understand and respond. | |
| - Understands subjects, objects, verbs | |
| - Matches queries from a predefined knowledge base | |
| - Shows explanation for response selection | |
| """) | |
| chatbot = gr.Chatbot() | |
| with gr.Row(): | |
| msg = gr.Textbox(label="Enter your message") | |
| clear = gr.Button("Clear") | |
| explanation = gr.Textbox(label="Explanation", lines=4) | |
| state = gr.State([]) | |
| msg.submit(chat_with_explanation, [msg, state], [chatbot, explanation, state]) | |
| clear.click(lambda: ([], "", []), None, [chatbot, explanation, state]) | |
| return demo | |
| # Run the app | |
| if __name__ == "__main__": | |
| build_interface().launch() | |