Spaces:
Runtime error
Runtime error
JUNGU
commited on
Commit
β’
9660e4f
0
Parent(s):
Duplicate from JUNGU/talktosayno
Browse files- .gitattributes +35 -0
- README.md +14 -0
- app.py +110 -0
- docs.pdf +3 -0
- requirements.txt +6 -0
.gitattributes
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
docs.pdf filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Talktosayno
|
3 |
+
emoji: π
|
4 |
+
colorFrom: green
|
5 |
+
colorTo: pink
|
6 |
+
sdk: gradio
|
7 |
+
sdk_version: 3.34.0
|
8 |
+
app_file: app.py
|
9 |
+
pinned: false
|
10 |
+
license: openrail
|
11 |
+
duplicated_from: JUNGU/talktosayno
|
12 |
+
---
|
13 |
+
|
14 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
ADDED
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain.chat_models import ChatOpenAI
|
2 |
+
from langchain.document_loaders import PyPDFLoader
|
3 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
4 |
+
from langchain.embeddings.cohere import CohereEmbeddings
|
5 |
+
from langchain.text_splitter import CharacterTextSplitter
|
6 |
+
from langchain.vectorstores.elastic_vector_search import ElasticVectorSearch
|
7 |
+
from langchain.vectorstores import Chroma
|
8 |
+
from PyPDF2 import PdfWriter
|
9 |
+
import gradio as gr
|
10 |
+
import os
|
11 |
+
from dotenv import load_dotenv
|
12 |
+
import openai
|
13 |
+
|
14 |
+
load_dotenv()
|
15 |
+
#λΉλ°ν€ κ°μ Έμ€κΈ° μλμ€
|
16 |
+
# api_key = os.getenv('OPENAI_API_KEY') ## .env νμΌ μ
λ‘λνλ©΄ μ¨κ²¨μ§μ§ μμ μλ¨
|
17 |
+
# api_key = os.environ['my_secret'] ## μλΆλ¬μμ§
|
18 |
+
# api_key = os.getenv('my_secret') ## 3νΈ .env λμ secretν€λ₯Ό λΆλ¬μ€λ ννλ‘ λμ
|
19 |
+
os.environ["OPENAI_API_KEY"] = os.environ['my_secret']
|
20 |
+
|
21 |
+
loader = PyPDFLoader("/home/user/app/docs.pdf")
|
22 |
+
documents = loader.load()
|
23 |
+
|
24 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
25 |
+
texts = text_splitter.split_documents(documents)
|
26 |
+
|
27 |
+
#vector embedding
|
28 |
+
embeddings = OpenAIEmbeddings()
|
29 |
+
vector_store = Chroma.from_documents(texts, embeddings)
|
30 |
+
retriever = vector_store.as_retriever(search_kwargs={"k": 2})
|
31 |
+
|
32 |
+
from langchain.chat_models import ChatOpenAI
|
33 |
+
from langchain.chains import RetrievalQAWithSourcesChain
|
34 |
+
|
35 |
+
llm = ChatOpenAI(model_name="gpt-4", temperature=0) # Modify model_name if you have access to GPT-4
|
36 |
+
|
37 |
+
chain = RetrievalQAWithSourcesChain.from_chain_type(
|
38 |
+
llm=llm,
|
39 |
+
chain_type="stuff",
|
40 |
+
retriever = retriever,
|
41 |
+
return_source_documents=True)
|
42 |
+
|
43 |
+
from langchain.prompts.chat import (
|
44 |
+
ChatPromptTemplate,
|
45 |
+
SystemMessagePromptTemplate,
|
46 |
+
HumanMessagePromptTemplate,
|
47 |
+
)
|
48 |
+
|
49 |
+
system_template="""Use the following pieces of context to answer the users question shortly.
|
50 |
+
Given the following summaries of a long document and a question, create a final answer with references ("SOURCES"), use "SOURCES" in capital letters regardless of the number of sources.
|
51 |
+
If you don't know the answer, just say that "I don't know", don't try to make up an answer.
|
52 |
+
----------------
|
53 |
+
{summaries}
|
54 |
+
|
55 |
+
You MUST answer in Korean and in Markdown format:"""
|
56 |
+
|
57 |
+
messages = [
|
58 |
+
SystemMessagePromptTemplate.from_template(system_template),
|
59 |
+
HumanMessagePromptTemplate.from_template("{question}")
|
60 |
+
]
|
61 |
+
|
62 |
+
prompt = ChatPromptTemplate.from_messages(messages)
|
63 |
+
|
64 |
+
from langchain.chat_models import ChatOpenAI
|
65 |
+
from langchain.chains import RetrievalQAWithSourcesChain
|
66 |
+
|
67 |
+
chain_type_kwargs = {"prompt": prompt}
|
68 |
+
|
69 |
+
llm = ChatOpenAI(model_name="gpt-4", temperature=0) # Modify model_name if you have access to GPT-4
|
70 |
+
|
71 |
+
chain = RetrievalQAWithSourcesChain.from_chain_type(
|
72 |
+
llm=llm,
|
73 |
+
chain_type="stuff",
|
74 |
+
retriever = retriever,
|
75 |
+
return_source_documents=True,
|
76 |
+
chain_type_kwargs=chain_type_kwargs
|
77 |
+
)
|
78 |
+
|
79 |
+
query = "ν볡ν μΈμμ΄λ?"
|
80 |
+
result = chain(query)
|
81 |
+
|
82 |
+
|
83 |
+
for doc in result['source_documents']:
|
84 |
+
print('λ΄μ© : ' + doc.page_content[0:100].replace('\n', ' '))
|
85 |
+
print('νμΌ : ' + doc.metadata['source'])
|
86 |
+
print('νμ΄μ§ : ' + str(doc.metadata['page']))
|
87 |
+
|
88 |
+
|
89 |
+
def respond(message, chat_history): # μ±ν
λ΄μ μλ΅μ μ²λ¦¬νλ ν¨μλ₯Ό μ μν©λλ€.
|
90 |
+
|
91 |
+
result = chain(message)
|
92 |
+
|
93 |
+
bot_message = result['answer']
|
94 |
+
|
95 |
+
for i, doc in enumerate(result['source_documents']):
|
96 |
+
bot_message += '[' + str(i+1) + '] ' + doc.metadata['source'] + '(' + str(doc.metadata['page']) + ') '
|
97 |
+
|
98 |
+
chat_history.append((message, bot_message)) # μ±ν
κΈ°λ‘μ μ¬μ©μμ λ©μμ§μ λ΄μ μλ΅μ μΆκ°ν©λλ€.
|
99 |
+
|
100 |
+
return "", chat_history # μμ λ μ±ν
κΈ°λ‘μ λ°νν©λλ€.
|
101 |
+
|
102 |
+
with gr.Blocks(theme='gstaff/sketch') as demo: # gr.Blocks()λ₯Ό μ¬μ©νμ¬ μΈν°νμ΄μ€λ₯Ό μμ±ν©λλ€.
|
103 |
+
gr.Markdown("# μλ
νμΈμ. μΈμ΄λ
Έμ λνν΄λ³΄μΈμ.")
|
104 |
+
chatbot = gr.Chatbot(label="μ±ν
μ°½") # 'μ±ν
μ°½'μ΄λΌλ λ μ΄λΈμ κ°μ§ μ±ν
λ΄ μ»΄ν¬λνΈλ₯Ό μμ±ν©λλ€.
|
105 |
+
msg = gr.Textbox(label="μ
λ ₯") # 'μ
λ ₯'μ΄λΌλ λ μ΄λΈμ κ°μ§ ν
μ€νΈλ°μ€λ₯Ό μμ±ν©λλ€.
|
106 |
+
clear = gr.Button("μ΄κΈ°ν") # 'μ΄κΈ°ν'λΌλ λ μ΄λΈμ κ°μ§ λ²νΌμ μμ±ν©λλ€.
|
107 |
+
|
108 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot]) # ν
μ€νΈλ°μ€μ λ©μμ§λ₯Ό μ
λ ₯νκ³ μ μΆνλ©΄ respond ν¨μκ° νΈμΆλλλ‘ ν©λλ€.
|
109 |
+
clear.click(lambda: None, None, chatbot, queue=False) # 'μ΄κΈ°ν' λ²νΌμ ν΄λ¦νλ©΄ μ±ν
κΈ°λ‘μ μ΄κΈ°νν©λλ€.
|
110 |
+
demo.launch(debug=True) # μΈν°νμ΄μ€λ₯Ό μ€νν©λλ€. μ€ννλ©΄ μ¬μ©μλ 'μ
λ ₯' ν
μ€νΈλ°μ€μ λ©μμ§λ₯Ό μμ±νκ³ μ μΆν μ μμΌλ©°, 'μ΄κΈ°ν' λ²νΌμ ν΅ν΄ μ±ν
κΈ°λ‘μ μ΄κΈ°ν ν μ μμ΅λλ€.
|
docs.pdf
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dab840d01bd8582e930da5ccb74c032279e832ed02f7f938953e7f77730d1ad2
|
3 |
+
size 4232031
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
openai
|
2 |
+
langchain
|
3 |
+
pypdf
|
4 |
+
chromadb
|
5 |
+
tiktoken
|
6 |
+
PyPDF2
|