Spaces:
Sleeping
Sleeping
KOkeke94
commited on
Commit
Β·
3d87318
1
Parent(s):
2937073
Fix: Update deprecated imports, add tiktoken, migrate to langchain_community
Browse files
app.py
CHANGED
|
@@ -1,17 +1,17 @@
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
-
from
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
-
from
|
| 7 |
-
from
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
-
from
|
| 10 |
from transformers.pipelines import pipeline
|
| 11 |
|
| 12 |
-
# β
Load API key from
|
| 13 |
openai_key = os.environ.get("OPENAI_API_KEY")
|
| 14 |
-
llm = ChatOpenAI(
|
| 15 |
|
| 16 |
# β
Build RAG agent
|
| 17 |
def build_rag_agent(pdf_path):
|
|
@@ -19,7 +19,8 @@ def build_rag_agent(pdf_path):
|
|
| 19 |
docs = loader.load()
|
| 20 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 21 |
chunks = splitter.split_documents(docs)
|
| 22 |
-
|
|
|
|
| 23 |
retriever = vectorstore.as_retriever()
|
| 24 |
return RetrievalQA.from_chain_type(llm=llm, retriever=retriever, chain_type="stuff")
|
| 25 |
|
|
@@ -27,10 +28,10 @@ def build_rag_agent(pdf_path):
|
|
| 27 |
stat6371_agent = build_rag_agent("PDFs/DS 6371 Syllabus Ver 6.pdf")
|
| 28 |
ds7333_agent = build_rag_agent("PDFs/ds-7333_syllabus.pdf")
|
| 29 |
|
| 30 |
-
# β
Load
|
| 31 |
general_stat_agent = pipeline("text2text-generation", model="BivinSadler/llama3-finetuned-Statistics")
|
| 32 |
|
| 33 |
-
# β
Routing
|
| 34 |
def route_question_llm(question):
|
| 35 |
prompt = f"""
|
| 36 |
You are a classification agent that helps route questions to the appropriate expert.
|
|
@@ -44,10 +45,10 @@ Classify the following question into one of those three categories by answering
|
|
| 44 |
|
| 45 |
Question: "{question}"
|
| 46 |
Answer:"""
|
| 47 |
-
|
| 48 |
-
if
|
| 49 |
return "stat6371"
|
| 50 |
-
elif
|
| 51 |
return "ds7333"
|
| 52 |
else:
|
| 53 |
return "general"
|
|
@@ -64,7 +65,7 @@ Write your response in 2β3 sentences. Avoid technical jargon.
|
|
| 64 |
"""
|
| 65 |
return llm.invoke(prompt).content
|
| 66 |
|
| 67 |
-
# β
|
| 68 |
def multiagent_system(question):
|
| 69 |
print(f"π§ Routing: {question}")
|
| 70 |
route = route_question_llm(question)
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 5 |
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 6 |
+
from langchain_community.embeddings import OpenAIEmbeddings
|
| 7 |
+
from langchain_community.vectorstores import FAISS
|
| 8 |
from langchain.chains import RetrievalQA
|
| 9 |
+
from langchain_openai import ChatOpenAI
|
| 10 |
from transformers.pipelines import pipeline
|
| 11 |
|
| 12 |
+
# β
Load API key from Hugging Face secret
|
| 13 |
openai_key = os.environ.get("OPENAI_API_KEY")
|
| 14 |
+
llm = ChatOpenAI(api_key=openai_key, model="gpt-3.5-turbo", temperature=0)
|
| 15 |
|
| 16 |
# β
Build RAG agent
|
| 17 |
def build_rag_agent(pdf_path):
|
|
|
|
| 19 |
docs = loader.load()
|
| 20 |
splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
|
| 21 |
chunks = splitter.split_documents(docs)
|
| 22 |
+
embeddings = OpenAIEmbeddings(api_key=openai_key)
|
| 23 |
+
vectorstore = FAISS.from_documents(chunks, embeddings)
|
| 24 |
retriever = vectorstore.as_retriever()
|
| 25 |
return RetrievalQA.from_chain_type(llm=llm, retriever=retriever, chain_type="stuff")
|
| 26 |
|
|
|
|
| 28 |
stat6371_agent = build_rag_agent("PDFs/DS 6371 Syllabus Ver 6.pdf")
|
| 29 |
ds7333_agent = build_rag_agent("PDFs/ds-7333_syllabus.pdf")
|
| 30 |
|
| 31 |
+
# β
Load Hugging Face fine-tuned model
|
| 32 |
general_stat_agent = pipeline("text2text-generation", model="BivinSadler/llama3-finetuned-Statistics")
|
| 33 |
|
| 34 |
+
# β
Routing logic
|
| 35 |
def route_question_llm(question):
|
| 36 |
prompt = f"""
|
| 37 |
You are a classification agent that helps route questions to the appropriate expert.
|
|
|
|
| 45 |
|
| 46 |
Question: "{question}"
|
| 47 |
Answer:"""
|
| 48 |
+
response = llm.invoke(prompt).content.strip().upper()
|
| 49 |
+
if response.startswith("A"):
|
| 50 |
return "stat6371"
|
| 51 |
+
elif response.startswith("B"):
|
| 52 |
return "ds7333"
|
| 53 |
else:
|
| 54 |
return "general"
|
|
|
|
| 65 |
"""
|
| 66 |
return llm.invoke(prompt).content
|
| 67 |
|
| 68 |
+
# β
Main app logic
|
| 69 |
def multiagent_system(question):
|
| 70 |
print(f"π§ Routing: {question}")
|
| 71 |
route = route_question_llm(question)
|