Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from openai import OpenAI
|
| 3 |
+
from langchain_community.document_loaders import PyPDFLoader
|
| 4 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 5 |
+
from langchain_community.vectorstores import Chroma
|
| 6 |
+
from langchain_openai import OpenAIEmbeddings
|
| 7 |
+
from langchain_openai import ChatOpenAI
|
| 8 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 9 |
+
from langchain_core.runnables import RunnablePassthrough
|
| 10 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 11 |
+
from langchain.schema import AIMessage, HumanMessage
|
| 12 |
+
import gradio as gr
|
| 13 |
+
|
| 14 |
+
# 1. PDF ๋ก๋ ๋ฐ ์ ์ฒ๋ฆฌ
|
| 15 |
+
pdf_filepath = '3.pdf'
|
| 16 |
+
loader = PyPDFLoader(pdf_filepath)
|
| 17 |
+
pages = loader.load()
|
| 18 |
+
|
| 19 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=400)
|
| 20 |
+
splits = text_splitter.split_documents(pages)
|
| 21 |
+
|
| 22 |
+
# 2. ๋ฒกํฐ ์คํ ์ด ์์ฑ ๋ฐ ์๋ฒ ๋ฉ
|
| 23 |
+
vectorstore = Chroma.from_documents(documents=splits,
|
| 24 |
+
embedding=OpenAIEmbeddings())
|
| 25 |
+
|
| 26 |
+
# 3. RAG ์ฒด์ธ ์ค์
|
| 27 |
+
template = '''Answer the question based only on the following context:
|
| 28 |
+
{context}
|
| 29 |
+
|
| 30 |
+
Question: {question}
|
| 31 |
+
'''
|
| 32 |
+
prompt = ChatPromptTemplate.from_template(template)
|
| 33 |
+
|
| 34 |
+
model = ChatOpenAI(model='gpt-4o-mini', temperature=0)
|
| 35 |
+
|
| 36 |
+
retriever = vectorstore.as_retriever()
|
| 37 |
+
|
| 38 |
+
def format_docs(docs):
|
| 39 |
+
return '\n\n'.join(doc.page_content for doc in docs)
|
| 40 |
+
|
| 41 |
+
rag_chain = (
|
| 42 |
+
{'context': retriever | format_docs, 'question': RunnablePassthrough()}
|
| 43 |
+
| prompt
|
| 44 |
+
| model
|
| 45 |
+
| StrOutputParser()
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
# 4. Gradio ์ธํฐํ์ด์ค predict ํจ์ ์์
|
| 49 |
+
def predict(message, history):
|
| 50 |
+
# RAG ์ฒด์ธ์ ์ฌ์ฉํ์ฌ ๋ต๋ณ ์์ฑ
|
| 51 |
+
response = rag_chain.invoke(message)
|
| 52 |
+
return response
|
| 53 |
+
|
| 54 |
+
# 5. Gradio ์ธํฐํ์ด์ค ์ค์ ๋ฐ ์คํ
|
| 55 |
+
demo = gr.ChatInterface(
|
| 56 |
+
predict,
|
| 57 |
+
title="์์ด ํ์ต ์ฑ๋ด (Powered by RAG & LangChain)",
|
| 58 |
+
description="์์ด ๊ต์ฌ(1.pdf) ๋ด์ฉ์ ๊ธฐ๋ฐ์ผ๋ก ์ง๋ฌธ์ ๋ต๋ณํฉ๋๋ค."
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
demo.launch()
|