Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,75 +1,84 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import uuid
|
| 3 |
-
import gradio as gr
|
| 4 |
-
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
| 5 |
-
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
-
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
-
from langchain_chroma import Chroma
|
| 8 |
-
from
|
| 9 |
-
from
|
| 10 |
-
from langchain_core.
|
| 11 |
-
from langchain_core.
|
| 12 |
-
from langchain_core.
|
| 13 |
-
|
| 14 |
-
# 1. LLM ์ด๊ธฐํ
|
| 15 |
-
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 16 |
-
|
| 17 |
-
# 2. ๋ฌธ์ ๋ก๋ ๋ฐ ๋ฒกํฐ DB ๊ตฌ์ถ
|
| 18 |
-
loader = PyPDFLoader("Maximizing Muscle Hypertrophy.pdf")
|
| 19 |
-
pages = loader.load_and_split()
|
| 20 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 21 |
-
splits = text_splitter.split_documents(pages)
|
| 22 |
-
|
| 23 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
|
| 24 |
-
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
|
| 25 |
-
retriever = vectorstore.as_retriever()
|
| 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 |
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import uuid
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
|
| 5 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 6 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
| 7 |
+
from langchain_chroma import Chroma
|
| 8 |
+
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 9 |
+
from langchain_core.chat_history import BaseChatMessageHistory, InMemoryChatMessageHistory
|
| 10 |
+
from langchain_core.runnables.history import RunnableWithMessageHistory
|
| 11 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 12 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 13 |
+
|
| 14 |
+
# 1. LLM ์ด๊ธฐํ
|
| 15 |
+
llm = ChatGoogleGenerativeAI(model="gemini-2.0-flash", temperature=0)
|
| 16 |
+
|
| 17 |
+
# 2. ๋ฌธ์ ๋ก๋ ๋ฐ ๋ฒกํฐ DB ๊ตฌ์ถ
|
| 18 |
+
loader = PyPDFLoader("Maximizing Muscle Hypertrophy.pdf")
|
| 19 |
+
pages = loader.load_and_split()
|
| 20 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
| 21 |
+
splits = text_splitter.split_documents(pages)
|
| 22 |
+
|
| 23 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
|
| 24 |
+
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
|
| 25 |
+
retriever = vectorstore.as_retriever()
|
| 26 |
+
|
| 27 |
+
# ๊ฒ์๋ ๋ฌธ์๋ฅผ ํ๋์ ๋ฌธ์์ด๋ก ๊ฒฐํฉํ๋ ํฌํผ ํจ์
|
| 28 |
+
def format_docs(docs):
|
| 29 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
| 30 |
+
|
| 31 |
+
# 3. ํ๋กฌํํธ ์ ์
|
| 32 |
+
qa_prompt = ChatPromptTemplate.from_messages([
|
| 33 |
+
("system", """๋
ผ๋ฌธ ๋ฆฌ๋ทฐ ์ ๋ฌธ๊ฐ์
๋๋ค. ์ ๊ณต๋ ๋ฌธ์๋ฅผ ๋ฐํ์ผ๋ก ํ๊ตญ์ด๋ก ๋ต๋ณํ์ธ์.
|
| 34 |
+
๋ฌธ์์ ์๋ ๋ด์ฉ์ ๋ชจ๋ฅธ๋ค๊ณ ๋ตํ์ธ์.
|
| 35 |
+
|
| 36 |
+
{context}"""),
|
| 37 |
+
MessagesPlaceholder("chat_history"),
|
| 38 |
+
("human", "{input}"),
|
| 39 |
+
])
|
| 40 |
+
|
| 41 |
+
# 4. ์๋ฌ๊ฐ ๋๋ chains ๋ชจ๋์ ๋ฒ๋ฆฌ๊ณ LCEL(ํ์ดํ๋ผ์ธ) ๋ฌธ๋ฒ์ผ๋ก RAG ์ฒด์ธ ๊ตฌ์ถ
|
| 42 |
+
rag_chain = (
|
| 43 |
+
RunnablePassthrough.assign(context=(lambda x: format_docs(retriever.invoke(x["input"]))))
|
| 44 |
+
| qa_prompt
|
| 45 |
+
| llm
|
| 46 |
+
| StrOutputParser()
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# 5. ๋ฉ๋ชจ๋ฆฌ(๋ํ ๊ธฐ๋ก) ์ฐ๋
|
| 50 |
+
store = {}
|
| 51 |
+
def get_session_history(session_id: str) -> BaseChatMessageHistory:
|
| 52 |
+
if session_id not in store:
|
| 53 |
+
store[session_id] = InMemoryChatMessageHistory()
|
| 54 |
+
return store[session_id]
|
| 55 |
+
|
| 56 |
+
conversational_rag_chain = RunnableWithMessageHistory(
|
| 57 |
+
rag_chain,
|
| 58 |
+
get_session_history,
|
| 59 |
+
input_messages_key="input",
|
| 60 |
+
history_messages_key="chat_history",
|
| 61 |
+
)
|
| 62 |
+
|
| 63 |
+
# 6. Gradio ์ฐ๋ ํจ์
|
| 64 |
+
def chat_response(message, history, session_id):
|
| 65 |
+
# LCEL ์ฒด์ธ์ ๋์
๋๋ฆฌ๊ฐ ์๋ ๋ฌธ์์ด์ ๋ฐ๋ก ๋ฐํํ๋ฏ๋ก ["answer"] ์ถ์ถ์ด ํ์ ์์
|
| 66 |
+
response = conversational_rag_chain.invoke(
|
| 67 |
+
{"input": message},
|
| 68 |
+
config={"configurable": {"session_id": session_id}}
|
| 69 |
+
)
|
| 70 |
+
return response
|
| 71 |
+
|
| 72 |
+
# 7. ๋ค์ค ์ฌ์ฉ์ ํ๊ฒฝ UI ์คํ
|
| 73 |
+
with gr.Blocks() as demo:
|
| 74 |
+
session_state = gr.State(lambda: str(uuid.uuid4()))
|
| 75 |
+
|
| 76 |
+
gr.ChatInterface(
|
| 77 |
+
fn=chat_response,
|
| 78 |
+
additional_inputs=[session_state],
|
| 79 |
+
title="๐ช ๊ทผ๋น๋ ๊ทน๋ํ ๋
ผ๋ฌธ Q&A ๋ด",
|
| 80 |
+
description="'Maximizing Muscle Hypertrophy' ๋
ผ๋ฌธ์ ๋ํด ๊ถ๊ธํ ์ ์ ๋ฌผ์ด๋ณด์ธ์!"
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
if __name__ == "__main__":
|
| 84 |
demo.launch()
|