Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import pandas as pd
|
2 |
from sentence_transformers import SentenceTransformer
|
3 |
import faiss
|
4 |
from datasets import load_dataset
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
# Load the
|
7 |
support_data = load_dataset("rjac/e-commerce-customer-support-qa")
|
|
|
|
|
8 |
faq_data = pd.read_csv("Ecommerce_FAQs.csv")
|
9 |
|
10 |
-
# Data
|
11 |
faq_data.rename(columns={'prompt': 'Question', 'response': 'Answer'}, inplace=True)
|
12 |
faq_data = faq_data[['Question', 'Answer']]
|
13 |
support_data_df = pd.DataFrame(support_data['train'])
|
14 |
|
|
|
15 |
def extract_conversation(data):
|
16 |
try:
|
17 |
parts = data.split("\n\n")
|
@@ -21,29 +35,55 @@ def extract_conversation(data):
|
|
21 |
except IndexError:
|
22 |
return pd.Series({"Question": "", "Answer": ""})
|
23 |
|
|
|
24 |
support_data_df[['Question', 'Answer']] = support_data_df['conversation'].apply(extract_conversation)
|
|
|
|
|
25 |
combined_data = pd.concat([faq_data, support_data_df[['Question', 'Answer']]], ignore_index=True)
|
26 |
|
27 |
# Initialize SBERT Model
|
28 |
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
|
29 |
|
30 |
-
# Generate and Index Embeddings
|
31 |
questions = combined_data['Question'].tolist()
|
32 |
embeddings = model.encode(questions, convert_to_tensor=True)
|
|
|
|
|
33 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
34 |
index.add(embeddings.cpu().numpy())
|
35 |
|
36 |
-
|
|
|
37 |
question_embedding = model.encode([question], convert_to_tensor=True)
|
38 |
question_embedding_np = question_embedding.cpu().numpy()
|
39 |
_, closest_index = index.search(question_embedding_np, k=1)
|
40 |
best_match_idx = closest_index[0][0]
|
41 |
answer = combined_data.iloc[best_match_idx]['Answer']
|
42 |
-
return answer
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
-
# Example usage
|
45 |
if __name__ == "__main__":
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
1 |
+
# app.py
|
2 |
+
|
3 |
+
# Install necessary libraries (Run this separately in your environment if needed)
|
4 |
+
# !pip install pandas sentence-transformers transformers datasets faiss-cpu gradio fastapi uvicorn
|
5 |
+
|
6 |
+
# Import libraries
|
7 |
import pandas as pd
|
8 |
from sentence_transformers import SentenceTransformer
|
9 |
import faiss
|
10 |
from datasets import load_dataset
|
11 |
+
from fastapi import FastAPI
|
12 |
+
import gradio as gr
|
13 |
+
|
14 |
+
# Initialize FastAPI app
|
15 |
+
app = FastAPI()
|
16 |
|
17 |
+
# Load the Dataset from Hugging Face and FAQ CSV
|
18 |
support_data = load_dataset("rjac/e-commerce-customer-support-qa")
|
19 |
+
|
20 |
+
# Load FAQ data from uploaded file (change as needed for local path)
|
21 |
faq_data = pd.read_csv("Ecommerce_FAQs.csv")
|
22 |
|
23 |
+
# Preprocess and Clean Data
|
24 |
faq_data.rename(columns={'prompt': 'Question', 'response': 'Answer'}, inplace=True)
|
25 |
faq_data = faq_data[['Question', 'Answer']]
|
26 |
support_data_df = pd.DataFrame(support_data['train'])
|
27 |
|
28 |
+
# Extract question-answer pairs from the conversation field
|
29 |
def extract_conversation(data):
|
30 |
try:
|
31 |
parts = data.split("\n\n")
|
|
|
35 |
except IndexError:
|
36 |
return pd.Series({"Question": "", "Answer": ""})
|
37 |
|
38 |
+
# Apply extraction function
|
39 |
support_data_df[['Question', 'Answer']] = support_data_df['conversation'].apply(extract_conversation)
|
40 |
+
|
41 |
+
# Combine FAQ data with support data
|
42 |
combined_data = pd.concat([faq_data, support_data_df[['Question', 'Answer']]], ignore_index=True)
|
43 |
|
44 |
# Initialize SBERT Model
|
45 |
model = SentenceTransformer('sentence-transformers/all-mpnet-base-v2')
|
46 |
|
47 |
+
# Generate and Index Embeddings for Combined Data
|
48 |
questions = combined_data['Question'].tolist()
|
49 |
embeddings = model.encode(questions, convert_to_tensor=True)
|
50 |
+
|
51 |
+
# Create FAISS index
|
52 |
index = faiss.IndexFlatL2(embeddings.shape[1])
|
53 |
index.add(embeddings.cpu().numpy())
|
54 |
|
55 |
+
# Define Retrieval Function
|
56 |
+
def retrieve_answer(question):
|
57 |
question_embedding = model.encode([question], convert_to_tensor=True)
|
58 |
question_embedding_np = question_embedding.cpu().numpy()
|
59 |
_, closest_index = index.search(question_embedding_np, k=1)
|
60 |
best_match_idx = closest_index[0][0]
|
61 |
answer = combined_data.iloc[best_match_idx]['Answer']
|
62 |
+
return answer
|
63 |
+
|
64 |
+
# Gradio Interface
|
65 |
+
def chatbot_interface(user_input):
|
66 |
+
response = retrieve_answer(user_input)
|
67 |
+
return f"Bot: {response}"
|
68 |
+
|
69 |
+
# Define a route to serve the Gradio interface
|
70 |
+
@app.get("/")
|
71 |
+
def home():
|
72 |
+
return {"message": "Welcome to the E-commerce Support Chatbot!"}
|
73 |
+
|
74 |
+
# Launch Gradio Interface in a separate thread
|
75 |
+
def launch_gradio():
|
76 |
+
iface = gr.Interface(
|
77 |
+
fn=chatbot_interface,
|
78 |
+
inputs=gr.Textbox(lines=2, placeholder="Type your question here..."),
|
79 |
+
outputs="text",
|
80 |
+
title="E-commerce Support Chatbot",
|
81 |
+
description="Ask questions about order tracking, returns, account help, and more!"
|
82 |
+
)
|
83 |
+
iface.launch(share=True)
|
84 |
|
|
|
85 |
if __name__ == "__main__":
|
86 |
+
import threading
|
87 |
+
threading.Thread(target=launch_gradio).start()
|
88 |
+
import uvicorn
|
89 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|