Thilak118 commited on
Commit
2159f24
·
verified ·
1 Parent(s): 289fc18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -33
app.py CHANGED
@@ -9,26 +9,21 @@ import torch
9
  import gradio as gr
10
 
11
  # Load dataset
12
- try:
13
- dataset = pd.read_csv("dataset.csv")
14
- except FileNotFoundError:
15
- raise FileNotFoundError("❌ dataset.csv not found. Please upload it alongside app.py.")
16
 
17
- # Convert dataset to LangChain Documents
18
- def dataframe_to_documents(df, content_col="answer", metadata_cols=["question"]):
19
- return [
20
- Document(
21
- page_content=str(row[content_col]),
22
- metadata={col: str(row[col]) for col in metadata_cols}
23
- )
24
- for _, row in df.iterrows()
25
- ]
26
 
27
- documents = dataframe_to_documents(dataset)
28
-
29
- # Initialize components
30
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
31
 
 
32
  model_name = "google/flan-t5-base"
33
  tokenizer = AutoTokenizer.from_pretrained(model_name)
34
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
@@ -36,44 +31,39 @@ pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer, max_le
36
  device=0 if torch.cuda.is_available() else -1)
37
  llm = HuggingFacePipeline(pipeline=pipe)
38
 
39
- # Create vector store & retriever
40
  vector_store = FAISS.from_documents(documents, embeddings)
41
  retriever = vector_store.as_retriever(search_kwargs={"k": 3})
42
 
43
- # Create QA Chain
44
  qa_chain = RetrievalQA.from_chain_type(
45
  llm=llm,
46
  retriever=retriever,
47
  return_source_documents=True
48
  )
49
 
50
- # Chatbot logic
51
  def chatbot_interface(question: str) -> str:
52
- if not question.strip():
53
- return " Please enter a question."
54
-
55
  try:
56
  response = qa_chain.invoke({"query": question})
57
- answer = response.get("result", "No answer found.")
58
  sources = response.get("source_documents", [])
59
  source_texts = [doc.page_content for doc in sources]
60
-
61
- # Format response
62
- formatted_sources = "\n".join(f"- {src}" for src in source_texts) if source_texts else "No sources found."
63
- return f"✅ **Answer:** {answer}\n\n📚 **Sources:**\n{formatted_sources}"
64
-
65
  except Exception as e:
66
- return f"Error: {e}"
67
 
68
  # Gradio UI
69
  interface = gr.Interface(
70
  fn=chatbot_interface,
71
- inputs=gr.Textbox(label="Ask a question", placeholder="e.g., What is Artificial Intelligence?"),
72
  outputs=gr.Textbox(label="Response"),
73
- title="RAG Chatbot 🤖",
74
- description="Ask me anything about AI, RAG, NLP, and more!",
75
  theme="default"
76
  )
77
 
78
- # Run the app
79
  interface.launch()
 
9
  import gradio as gr
10
 
11
  # Load dataset
12
+ dataset = pd.read_csv("dataset.csv")
 
 
 
13
 
14
+ # Convert to LangChain Documents
15
+ documents = [
16
+ Document(
17
+ page_content=str(row["answer"]),
18
+ metadata={"question": str(row["question"])}
19
+ )
20
+ for _, row in dataset.iterrows()
21
+ ]
 
22
 
23
+ # Setup embeddings
 
 
24
  embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
25
 
26
+ # Load LLM
27
  model_name = "google/flan-t5-base"
28
  tokenizer = AutoTokenizer.from_pretrained(model_name)
29
  model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
 
31
  device=0 if torch.cuda.is_available() else -1)
32
  llm = HuggingFacePipeline(pipeline=pipe)
33
 
34
+ # Create vector store
35
  vector_store = FAISS.from_documents(documents, embeddings)
36
  retriever = vector_store.as_retriever(search_kwargs={"k": 3})
37
 
38
+ # Create QA chain
39
  qa_chain = RetrievalQA.from_chain_type(
40
  llm=llm,
41
  retriever=retriever,
42
  return_source_documents=True
43
  )
44
 
45
+ # Chatbot function
46
  def chatbot_interface(question: str) -> str:
47
+ if not qa_chain:
48
+ return "Chatbot backend not initialized properly."
 
49
  try:
50
  response = qa_chain.invoke({"query": question})
51
+ answer = response.get("result", "No answer found.")
52
  sources = response.get("source_documents", [])
53
  source_texts = [doc.page_content for doc in sources]
54
+ return f"Answer: {answer}\n\nSources:\n" + "\n".join(f"- {text}" for text in source_texts)
 
 
 
 
55
  except Exception as e:
56
+ return f"Error: {e}"
57
 
58
  # Gradio UI
59
  interface = gr.Interface(
60
  fn=chatbot_interface,
61
+ inputs=gr.Textbox(label="Enter your question"),
62
  outputs=gr.Textbox(label="Response"),
63
+ title="RAG Chatbot",
64
+ description="Ask questions about AI, ChatBots, NLP, and more.",
65
  theme="default"
66
  )
67
 
68
+ # Launch the interface
69
  interface.launch()