Amelia-James commited on
Commit
3740b36
Β·
verified Β·
1 Parent(s): 3a449b3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -39
app.py CHANGED
@@ -1,50 +1,77 @@
1
  import os
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
- if not data:
20
- print("No valid JSON files found in the dataset folder.")
21
- return [{"question": "No dataset available.", "answer": "Please upload a valid dataset."}]
22
  return data
23
 
 
 
24
 
25
- # Initialize Dataset
26
- DATASET_PATH = os.path.join(os.path.dirname(__file__), "dataset")
27
- dataset = load_dataset(DATASET_PATH)
28
-
29
- # Chatbot Response Function
30
- def chatbot_response(user_input):
31
- """Generate a response based on user input and the dataset."""
32
  for entry in dataset:
33
- if "question" in entry and "answer" in entry:
34
- if user_input.lower() in entry["question"].lower():
35
- return entry["answer"]
36
- return "I'm sorry, I couldn't find relevant information. Please provide more details."
37
-
38
- # Gradio Interface
39
- interface = gr.Interface(
40
- fn=chatbot_response,
41
- inputs="text",
42
- outputs="text",
43
- title="Education Consultant Chatbot",
44
- description="Ask questions about study programs, visas, and more!",
45
- theme="compact"
46
- )
47
-
48
- # Run App
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
  if __name__ == "__main__":
50
- interface.launch()
 
 
1
  import os
2
  import json
3
  import gradio as gr
4
+ from transformers import pipeline
5
 
6
+ # Step 1: Load JSON Dataset
7
  def load_dataset(folder_path):
 
8
  data = []
9
+ for filename in os.listdir(folder_path):
10
+ if filename.endswith(".json"):
11
+ with open(os.path.join(folder_path, filename), "r") as file:
12
+ data.extend(json.load(file)) # Assuming each file is a list of entries
 
 
 
 
 
 
 
 
 
13
  return data
14
 
15
+ # Step 2: Initialize OpenAI or Hugging Face Model
16
+ model = pipeline("question-answering", model="deepset/roberta-base-squad2") # Replace with your preferred model
17
 
18
+ # Step 3: Query Handler
19
+ def query_chatbot(query, name, email, contact):
20
+ # Load the dataset
21
+ dataset = load_dataset("dataset/")
22
+
23
+ # Retrieve relevant information using a simple RAG technique
24
+ responses = []
25
  for entry in dataset:
26
+ context = entry.get("content", "") # Extract relevant content from the JSON file
27
+ if query.lower() in context.lower():
28
+ response = model(question=query, context=context)
29
+ responses.append(response["answer"])
30
+
31
+ # Compile the result
32
+ response = (
33
+ f"Hello {name}!\n\n"
34
+ f"Based on your query: '{query}', here are some relevant insights:\n\n"
35
+ + "\n".join(responses)[:3] # Limit to top 3 responses
36
+ )
37
+
38
+ # Create a profile (Optional enhancement)
39
+ profile = {
40
+ "name": name,
41
+ "email": email,
42
+ "contact": contact,
43
+ "query": query,
44
+ "responses": responses,
45
+ }
46
+
47
+ # Optionally, you can send this profile to an email or save it for further analysis.
48
+ return response
49
+
50
+ # Step 4: Gradio Interface
51
+ def chatbot_ui():
52
+ with gr.Blocks() as app:
53
+ gr.Markdown("# πŸŽ“ Education Consultant Chatbot")
54
+
55
+ with gr.Row():
56
+ with gr.Column():
57
+ query_input = gr.Textbox(label="Your Query", placeholder="Ask about courses, visas, or programs...")
58
+ name_input = gr.Textbox(label="Name", placeholder="Your Full Name")
59
+ email_input = gr.Textbox(label="Email", placeholder="Your Email Address")
60
+ contact_input = gr.Textbox(label="Contact (Optional)", placeholder="Your Contact Number")
61
+ submit_btn = gr.Button("Submit")
62
+
63
+ with gr.Column():
64
+ output_text = gr.Textbox(label="Chatbot Response")
65
+
66
+ submit_btn.click(
67
+ query_chatbot,
68
+ inputs=[query_input, name_input, email_input, contact_input],
69
+ outputs=output_text,
70
+ )
71
+
72
+ return app
73
+
74
+ # Step 5: Launch App
75
  if __name__ == "__main__":
76
+ chatbot = chatbot_ui()
77
+ chatbot.launch()