Spaces:
Running
Running
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() # μΈν°νμ΄μ€λ₯Ό μ€νν©λλ€. μ€ννλ©΄ μ¬μ©μλ 'μ λ ₯' ν μ€νΈλ°μ€μ λ©μμ§λ₯Ό μμ±νκ³ μ μΆν μ μμΌλ©°, 'μ΄κΈ°ν' λ²νΌμ ν΅ν΄ μ±ν κΈ°λ‘μ μ΄κΈ°ν ν μ μμ΅λλ€. |