Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,21 +9,26 @@ import torch
|
|
| 9 |
import gradio as gr
|
| 10 |
|
| 11 |
# Load dataset
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
# Convert to LangChain Documents
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 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,56 +36,44 @@ pipe = pipeline("text2text-generation", model=model, tokenizer=tokenizer, max_le
|
|
| 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
|
| 39 |
qa_chain = RetrievalQA.from_chain_type(
|
| 40 |
llm=llm,
|
| 41 |
retriever=retriever,
|
| 42 |
return_source_documents=True
|
| 43 |
)
|
| 44 |
|
| 45 |
-
# Chatbot
|
| 46 |
def chatbot_interface(question: str) -> str:
|
| 47 |
-
if not
|
| 48 |
-
return "
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 59 |
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
|
| 62 |
-
"""
|
| 63 |
-
Processes a question through the Chatbot and returns the response for Gradio.
|
| 64 |
-
|
| 65 |
-
Args:
|
| 66 |
-
question (str): User input question.
|
| 67 |
-
|
| 68 |
-
Returns:
|
| 69 |
-
str: Chatbot response with answer and sources.
|
| 70 |
-
"""
|
| 71 |
-
if not question.strip():
|
| 72 |
-
return "Please enter a question."
|
| 73 |
-
return chatbot.ask(question)
|
| 74 |
-
|
| 75 |
-
# Create Gradio interface
|
| 76 |
interface = gr.Interface(
|
| 77 |
fn=chatbot_interface,
|
| 78 |
-
inputs=gr.Textbox(label="
|
| 79 |
outputs=gr.Textbox(label="Response"),
|
| 80 |
-
title="RAG Chatbot",
|
| 81 |
-
description="Ask
|
| 82 |
theme="default"
|
| 83 |
)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
-
interface.launch(
|
|
|
|
| 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 |
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()
|