Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ from langchain_huggingface import HuggingFaceEmbeddings, HuggingFaceEndpoint
|
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
from langchain_core.prompts import ChatPromptTemplate
|
|
|
|
| 8 |
|
| 9 |
# 2026 Imports: These now live in the classic/retrieval modules
|
| 10 |
from langchain_classic.chains import create_retrieval_chain
|
|
@@ -28,13 +29,13 @@ retriever = vectorstore.as_retriever()
|
|
| 28 |
# 4. Initialize LLM (The Fix: Task="conversational")
|
| 29 |
llm = HuggingFaceEndpoint(
|
| 30 |
repo_id="Qwen/Qwen2.5-7B-Instruct",
|
| 31 |
-
task="conversational", #
|
| 32 |
huggingfacehub_api_token=sec_token,
|
| 33 |
temperature=0.1,
|
| 34 |
-
max_new_tokens=512,
|
| 35 |
-
timeout=300
|
| 36 |
)
|
| 37 |
|
|
|
|
|
|
|
| 38 |
# 5. Define the RAG Prompt
|
| 39 |
system_prompt = (
|
| 40 |
"You are a helpful assistant for Examfriendly tuition. "
|
|
@@ -52,18 +53,17 @@ prompt = ChatPromptTemplate.from_messages(
|
|
| 52 |
)
|
| 53 |
|
| 54 |
# 6. Build the Chain
|
| 55 |
-
question_answer_chain = create_stuff_documents_chain(
|
| 56 |
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 57 |
|
| 58 |
# 7. Define the Chat Function for Gradio
|
| 59 |
def chat(message, history):
|
| 60 |
try:
|
| 61 |
-
#
|
| 62 |
result = rag_chain.invoke({"input": message})
|
| 63 |
return result["answer"]
|
| 64 |
except Exception as e:
|
| 65 |
-
|
| 66 |
-
return f"Error: {str(e)}"
|
| 67 |
|
| 68 |
# 8. Launch the Interface
|
| 69 |
demo = gr.ChatInterface(
|
|
|
|
| 5 |
from langchain_community.vectorstores import FAISS
|
| 6 |
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
from langchain_core.prompts import ChatPromptTemplate
|
| 8 |
+
from langchain_huggingface import ChatHuggingFace # Add this to your imports
|
| 9 |
|
| 10 |
# 2026 Imports: These now live in the classic/retrieval modules
|
| 11 |
from langchain_classic.chains import create_retrieval_chain
|
|
|
|
| 29 |
# 4. Initialize LLM (The Fix: Task="conversational")
|
| 30 |
llm = HuggingFaceEndpoint(
|
| 31 |
repo_id="Qwen/Qwen2.5-7B-Instruct",
|
| 32 |
+
task="conversational", # Matches the provider's requirement
|
| 33 |
huggingfacehub_api_token=sec_token,
|
| 34 |
temperature=0.1,
|
|
|
|
|
|
|
| 35 |
)
|
| 36 |
|
| 37 |
+
chat_model = ChatHuggingFace(llm=llm)
|
| 38 |
+
|
| 39 |
# 5. Define the RAG Prompt
|
| 40 |
system_prompt = (
|
| 41 |
"You are a helpful assistant for Examfriendly tuition. "
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
# 6. Build the Chain
|
| 56 |
+
question_answer_chain = create_stuff_documents_chain(chat_model, prompt)
|
| 57 |
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 58 |
|
| 59 |
# 7. Define the Chat Function for Gradio
|
| 60 |
def chat(message, history):
|
| 61 |
try:
|
| 62 |
+
# ChatHuggingFace expects "input" inside the dictionary
|
| 63 |
result = rag_chain.invoke({"input": message})
|
| 64 |
return result["answer"]
|
| 65 |
except Exception as e:
|
| 66 |
+
return f"System Error: {str(e)}"
|
|
|
|
| 67 |
|
| 68 |
# 8. Launch the Interface
|
| 69 |
demo = gr.ChatInterface(
|