Amelia-James commited on
Commit
12e22a0
Β·
verified Β·
1 Parent(s): 2fdfa6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -26
app.py CHANGED
@@ -2,46 +2,45 @@ import os
2
  import json
3
  import gradio as gr
4
 
5
- # Path to the dataset folder
6
- DATASET_FOLDER = "dataset/"
7
-
8
  def load_dataset(folder_path):
9
- """Load all JSON files from the dataset folder."""
10
  data = []
11
- for file_name in os.listdir(folder_path):
12
- if file_name.endswith(".json"):
13
- file_path = os.path.join(folder_path, file_name)
14
- with open(file_path, "r") as f:
15
  try:
16
- file_data = json.load(f)
17
- data.append(file_data)
18
- except json.JSONDecodeError:
19
- print(f"Error decoding {file_path}. Skipping.")
 
20
  return data
21
 
22
- # Load the dataset
23
- dataset = load_dataset(DATASET_FOLDER)
 
24
 
25
- # Define your chatbot logic
26
  def chatbot_response(user_input):
27
- # Example: Search dataset for relevant information
28
- responses = []
29
- for item in dataset:
30
- if user_input.lower() in json.dumps(item).lower():
31
- responses.append(item)
32
- if responses:
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=["text", "json"],
41
  title="Education Consultant Chatbot",
42
- description="A chatbot for education consultancy."
 
43
  )
44
 
45
- # Launch the app
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()