Update app.py
Browse files
app.py
CHANGED
@@ -2,46 +2,45 @@ import os
|
|
2 |
import json
|
3 |
import gradio as gr
|
4 |
|
5 |
-
#
|
6 |
-
DATASET_FOLDER = "dataset/"
|
7 |
-
|
8 |
def load_dataset(folder_path):
|
9 |
-
"""
|
10 |
data = []
|
11 |
-
for
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
try:
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
20 |
return data
|
21 |
|
22 |
-
#
|
23 |
-
|
|
|
24 |
|
25 |
-
#
|
26 |
def chatbot_response(user_input):
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
return f"Found {len(responses)} matching records.", responses
|
34 |
-
return "No relevant information found.", []
|
35 |
|
36 |
# Gradio Interface
|
37 |
interface = gr.Interface(
|
38 |
fn=chatbot_response,
|
39 |
inputs="text",
|
40 |
-
outputs=
|
41 |
title="Education Consultant Chatbot",
|
42 |
-
description="
|
|
|
43 |
)
|
44 |
|
45 |
-
#
|
46 |
if __name__ == "__main__":
|
47 |
interface.launch()
|
|
|
2 |
import json
|
3 |
import gradio as gr
|
4 |
|
5 |
+
# Load Dataset Function
|
|
|
|
|
6 |
def load_dataset(folder_path):
|
7 |
+
"""Loads all JSON files from a folder and subfolders into a single dataset."""
|
8 |
data = []
|
9 |
+
for root, dirs, files in os.walk(folder_path):
|
10 |
+
for file_name in files:
|
11 |
+
if file_name.endswith(".json"):
|
12 |
+
file_path = os.path.join(root, file_name)
|
13 |
try:
|
14 |
+
with open(file_path, "r") as f:
|
15 |
+
file_data = json.load(f)
|
16 |
+
data.append(file_data)
|
17 |
+
except Exception as e:
|
18 |
+
print(f"Error loading {file_name}: {e}")
|
19 |
return data
|
20 |
|
21 |
+
# Initialize Dataset
|
22 |
+
DATASET_PATH = os.path.join(os.path.dirname(__file__), "dataset")
|
23 |
+
dataset = load_dataset(DATASET_PATH)
|
24 |
|
25 |
+
# Chatbot Response Function
|
26 |
def chatbot_response(user_input):
|
27 |
+
"""Generate a response based on user input and the dataset."""
|
28 |
+
for entry in dataset:
|
29 |
+
if "question" in entry and "answer" in entry:
|
30 |
+
if user_input.lower() in entry["question"].lower():
|
31 |
+
return entry["answer"]
|
32 |
+
return "I'm sorry, I couldn't find relevant information. Please provide more details."
|
|
|
|
|
33 |
|
34 |
# Gradio Interface
|
35 |
interface = gr.Interface(
|
36 |
fn=chatbot_response,
|
37 |
inputs="text",
|
38 |
+
outputs="text",
|
39 |
title="Education Consultant Chatbot",
|
40 |
+
description="Ask questions about study programs, visas, and more!",
|
41 |
+
theme="compact"
|
42 |
)
|
43 |
|
44 |
+
# Run App
|
45 |
if __name__ == "__main__":
|
46 |
interface.launch()
|