Spaces:
Running
Running
File size: 2,582 Bytes
fbda4e9 6b82681 a9b5444 fbda4e9 6b82681 ed44345 59572a7 6b82681 59572a7 6b82681 a9b5444 6b82681 a9b5444 6b82681 a9b5444 6b82681 a9b5444 6b82681 a9b5444 6b82681 a9b5444 eb221c7 ed44345 f89a671 fbda4e9 a9b5444 ed44345 6b82681 fbda4e9 6b82681 fbda4e9 f89a671 6b82681 f89a671 6b82681 ed44345 6b82681 a9b5444 6b82681 a9b5444 3a9ccc3 ed44345 3a9ccc3 ed44345 a9b5444 6b82681 f89a671 fbda4e9 6b82681 f89a671 fbda4e9 d9fe2bd | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import os
import gradio as gr
from langchain_community.document_loaders import TextLoader
from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.prompts import ChatPromptTemplate
from langchain_huggingface import ChatHuggingFace # Add this to your imports
# 2026 Imports: These now live in the classic/retrieval modules
from langchain_classic.chains import create_retrieval_chain
from langchain_classic.chains.combine_documents import create_stuff_documents_chain
# 1. Setup API Token from Secrets
sec_token = os.getenv("HUGGINGFACEHUB_API_TOKEN")
# 2. Load and Process Knowledge Base
# Make sure "knowledge.txt" exists in your Space files!
loader = TextLoader("knowledge.txt")
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
# 3. Create Vector Store
embeddings = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
vectorstore = FAISS.from_documents(documents=splits, embedding=embeddings)
retriever = vectorstore.as_retriever()
# 4. Initialize LLM (The Fix: Task="conversational")
llm = HuggingFaceEndpoint(
repo_id="Qwen/Qwen2.5-7B-Instruct",
task="conversational", # Matches the provider's requirement
huggingfacehub_api_token=sec_token,
temperature=0.1,
)
chat_model = ChatHuggingFace(llm=llm)
# 5. Define the RAG Prompt
system_prompt = (
"You are a helpful assistant for Examfriendly tuition. "
"Use the following pieces of retrieved context to answer the question. "
"If you don't know the answer, say you don't know. "
"\n\n"
"{context}"
)
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
# 6. Build the Chain
question_answer_chain = create_stuff_documents_chain(chat_model, prompt)
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
# 7. Define the Chat Function for Gradio
def chat(message, history):
try:
# ChatHuggingFace expects "input" inside the dictionary
result = rag_chain.invoke({"input": message})
return result["answer"]
except Exception as e:
return f"System Error: {str(e)}"
# 8. Launch the Interface
demo = gr.ChatInterface(
fn=chat,
title="Examfriendly AI Tutor",
description="Ask me anything about your tuition courses!",
)
if __name__ == "__main__":
demo.launch(share=False) |