Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,77 @@
|
|
1 |
import os
|
2 |
import json
|
3 |
import gradio as gr
|
|
|
4 |
|
5 |
-
# Load Dataset
|
6 |
def load_dataset(folder_path):
|
7 |
-
"""Loads all JSON files from a folder and subfolders into a single dataset."""
|
8 |
data = []
|
9 |
-
for
|
10 |
-
|
11 |
-
|
12 |
-
|
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 |
-
#
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
for entry in dataset:
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
#
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
if __name__ == "__main__":
|
50 |
-
|
|
|
|
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()
|