kinderrecord / app.py
powerwarez's picture
μ•„μ΄μ½˜ μΆ”κ°€
ee52640 verified
from langchain_openai import OpenAIEmbeddings
from langchain_chroma import Chroma
from langchain_openai import ChatOpenAI
from langchain.chains import RetrievalQAWithSourcesChain
import gradio as gr
import os
from dotenv import load_dotenv
from supabase import create_client
load_dotenv()
url = os.getenv("SUPABASE_URL")
key = os.getenv("SUPABASE_KEY")
supabase = create_client(url, key)
openai_api_key = os.getenv("OPENAI_API_KEY")
persist_directory = "db"
embeddings = OpenAIEmbeddings()
vector_store = Chroma(
persist_directory=persist_directory,
embedding_function=embeddings
)
retriever = vector_store.as_retriever(search_kwargs={"k": 3})
llm = ChatOpenAI(model_name="gpt-4o", temperature=0.1) # Modify model_name if you have access to GPT-4
chain = RetrievalQAWithSourcesChain.from_chain_type(
llm=llm,
chain_type="stuff",
retriever = retriever,
return_source_documents=True)
html_content = """
<div style="display: flex; align-items: center;">
<img src="https://huggingface.co/spaces/powerwarez/gailabicon/resolve/main/gailab08.png" alt="image" style="width:50px;height:auto;margin-right:10px;">
<span>μ œμž‘: ꡐ사 μ„œλ™μ„±</span>
</div>
"""
def respond(message, chat_history): # μ±„νŒ…λ΄‡μ˜ 응닡을 μ²˜λ¦¬ν•˜λŠ” ν•¨μˆ˜λ₯Ό μ •μ˜ν•©λ‹ˆλ‹€.
result = chain.invoke(message)
data = supabase.table("kinderrecordusage").insert({"prompt":message}).execute()
bot_message = '<strong>' + result['answer'] + '</strong><br><br>'
for i, doc in enumerate(result['source_documents']):
text = doc.page_content[0:500].replace('\n', ' ')
page = str(doc.metadata['page'])
bot_message += '[' + 'κ΄€λ ¨λ‚΄μš©:' + doc.page_content[0:500].replace('\n', ' ') + '...' + '] '
# 빨간색 μŠ€νƒ€μΌ 적용
bot_message += '<span style="color:red;">' + doc.metadata['source'] + '</span>'
# νŒŒλž€μƒ‰ μŠ€νƒ€μΌ 적용
bot_message += '<span style="color:blue;">' + '(' + page + 'μͺ½)</span> '
chat_history.append((message, bot_message)) # μ±„νŒ… 기둝에 μ‚¬μš©μžμ˜ λ©”μ‹œμ§€μ™€ λ΄‡μ˜ 응닡을 μΆ”κ°€ν•©λ‹ˆλ‹€.
return "", chat_history
with gr.Blocks() as demo: # gr.Blocks()λ₯Ό μ‚¬μš©ν•˜μ—¬ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
gr.Markdown("<div style='font-size: 30px; font-weight: bold;'>2023 μœ μΉ˜μ› μƒν™œκΈ°λ‘λΆ€ QnA chatbot</div>")
chatbot = gr.Chatbot(label="μ±„νŒ…μ°½") # 'μ±„νŒ…μ°½'μ΄λΌλŠ” λ ˆμ΄λΈ”μ„ 가진 μ±„νŒ…λ΄‡ μ»΄ν¬λ„ŒνŠΈλ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
msg = gr.Textbox(label="μž…λ ₯") # 'μž…λ ₯'μ΄λΌλŠ” λ ˆμ΄λΈ”μ„ 가진 ν…μŠ€νŠΈλ°•μŠ€λ₯Ό μƒμ„±ν•©λ‹ˆλ‹€.
clear = gr.Button("μ΄ˆκΈ°ν™”") # 'μ΄ˆκΈ°ν™”'λΌλŠ” λ ˆμ΄λΈ”μ„ 가진 λ²„νŠΌμ„ μƒμ„±ν•©λ‹ˆλ‹€.
msg.submit(respond, [msg, chatbot], [msg, chatbot]) # ν…μŠ€νŠΈλ°•μŠ€μ— λ©”μ‹œμ§€λ₯Ό μž…λ ₯ν•˜κ³  μ œμΆœν•˜λ©΄ respond ν•¨μˆ˜κ°€ ν˜ΈμΆœλ˜λ„λ‘ ν•©λ‹ˆλ‹€.
clear.click(lambda: None, None, chatbot, queue=False) # 'μ΄ˆκΈ°ν™”' λ²„νŠΌμ„ ν΄λ¦­ν•˜λ©΄ μ±„νŒ… 기둝을 μ΄ˆκΈ°ν™”ν•©λ‹ˆλ‹€.
gr.Markdown("2023 μœ μΉ˜μ› μƒν™œκΈ°λ‘λΆ€ κΈ°μž¬μš”λ Ή <a href='https://www.moe.go.kr/sn3hcv/doc.html?fn=65fcc64258de266d005f7156e03f04f2&rs=/upload/synap/202407/'>미리보기</a>")
gr.Markdown("2023 μœ μΉ˜μ› μƒν™œκΈ°λ‘λΆ€ κΈ°μž¬μš”λ Ή λ‹€μš΄λ‘œλ“œ <a href='https://www.moe.go.kr/boardCnts/viewRenew.do?boardID=312&boardSeq=97281&lev=0&searchType=null&statusYN=W&page=1&s=moe&m=0301&opType=N'>μ΄λ™ν•˜κΈ°</a>")
gr.Markdown("μ •ν™•ν•œ λ‚΄μš©μ€ 원본을 κΌ­ ν™•μΈν•˜μ‹œκΈ° λ°”λžλ‹ˆλ‹€.")
gr.HTML(html_content)
demo.launch() # μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‹€ν–‰ν•©λ‹ˆλ‹€. μ‹€ν–‰ν•˜λ©΄ μ‚¬μš©μžλŠ” 'μž…λ ₯' ν…μŠ€νŠΈλ°•μŠ€μ— λ©”μ‹œμ§€λ₯Ό μž‘μ„±ν•˜κ³  μ œμΆœν•  수 있으며, 'μ΄ˆκΈ°ν™”' λ²„νŠΌμ„ 톡해 μ±„νŒ… 기둝을 μ΄ˆκΈ°ν™” ν•  수 μžˆμŠ΅λ‹ˆλ‹€.