Jyothish9988 commited on
Commit
82f9fa8
·
verified ·
1 Parent(s): ed371f2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import gradio as gr
3
+ from langchain.vectorstores import FAISS
4
+ from langchain.embeddings import HuggingFaceEmbeddings
5
+ from langchain.schema import Document
6
+
7
+ # Initialize once when the app starts
8
+ def initialize_system():
9
+ # Load dataset
10
+ data = pd.read_csv("qa_dataset.csv")
11
+
12
+ # Create documents
13
+ documents = [
14
+ Document(
15
+ page_content=f"Q: {row['Question']}\nA: {row['Answer']}",
16
+ metadata={"question": row['Question'], "answer": row['Answer']}
17
+ ) for _, row in data.iterrows()
18
+ ]
19
+
20
+ # Create vector store
21
+ embeddings = HuggingFaceEmbeddings(
22
+ model_name="sentence-transformers/multi-qa-mpnet-base-dot-v1"
23
+ )
24
+
25
+ return FAISS.from_documents(documents, embeddings)
26
+
27
+ vector_store = initialize_system()
28
+
29
+ def classify_question(query: str, k: int = 3):
30
+ # Retrieve similar Q&A pairs
31
+ results = vector_store.similarity_search(query, k=k)
32
+
33
+ # Generate category from answers
34
+ answers = " ".join([doc.metadata['answer'] for doc in results])
35
+ keywords = list(dict.fromkeys(answers.split()))[:5]
36
+ category = " ".join(keywords)
37
+
38
+ # Format output
39
+ return {
40
+ "Category": category,
41
+ "Top Matches": "\n\n".join([f"Q: {doc.metadata['question']}\nA: {doc.metadata['answer']}"
42
+ for doc in results]),
43
+ "Confidence": f"{len(results)/k:.0%}"
44
+ }
45
+
46
+ # Gradio interface
47
+ interface = gr.Interface(
48
+ fn=lambda q: classify_question(q, 3),
49
+ inputs=gr.Textbox(label="Input Question", placeholder="Type your question here..."),
50
+ outputs=[
51
+ gr.Textbox(label="Predicted Category"),
52
+ gr.Textbox(label="Supporting Q&A"),
53
+ gr.Textbox(label="Confidence")
54
+ ],
55
+ title="Question Classification System",
56
+ description="Classify questions based on existing Q&A pairs using RAG"
57
+ )
58
+
59
+ if __name__ == "__main__":
60
+ interface.launch()