Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- README.md +3 -9
- app.py +146 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,12 +1,6 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji: ๐
|
| 4 |
-
colorFrom: yellow
|
| 5 |
-
colorTo: green
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version: 4.44.0
|
| 8 |
app_file: app.py
|
| 9 |
-
|
|
|
|
| 10 |
---
|
| 11 |
-
|
| 12 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: langchain_try_kki
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
app_file: app.py
|
| 4 |
+
sdk: gradio
|
| 5 |
+
sdk_version: 4.39.0
|
| 6 |
---
|
|
|
|
|
|
app.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import weaviate # vector DB
|
| 2 |
+
from openai import OpenAI # LLM
|
| 3 |
+
import PyPDF2 # pdf -> text
|
| 4 |
+
import numpy as np
|
| 5 |
+
from transformers import AutoModel, AutoTokenizer # Model, Tokenzier Load
|
| 6 |
+
import gradio as gr # front-end(ui & ux)
|
| 7 |
+
from sentence_transformers import SentenceTransformer # embedding
|
| 8 |
+
|
| 9 |
+
# Weaviate์ ์ฌ์ฉํ ํด๋์ค ์คํค๋ง ์ ์
|
| 10 |
+
def create_schema():
|
| 11 |
+
class_obj = {
|
| 12 |
+
"class": "PdfSentence", # ํด๋์ค๋ช
|
| 13 |
+
"properties": [
|
| 14 |
+
{
|
| 15 |
+
"name": "sentence",
|
| 16 |
+
"dataType": ["text"]
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
"name": "embedding",
|
| 20 |
+
"dataType": ["number[]"] # ๋ฒกํฐ ํ์
|
| 21 |
+
}
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
# ์คํค๋ง ์์ฑ
|
| 25 |
+
db_client.schema.create_class(class_obj)
|
| 26 |
+
|
| 27 |
+
# ์คํค๋ง ํ์ธ ๋ฐ ์์ฑ / ๊ธฐ์กด์ ์ ์ธํ ์คํค๋ง๊ฐ ์๋ ๊ฒฝ์ฐ -> pass(๋์ด๊ฐ๋ค.)
|
| 28 |
+
def ensure_schema():
|
| 29 |
+
schema = db_client.schema.get()
|
| 30 |
+
classes = [cls["class"] for cls in schema["classes"]]
|
| 31 |
+
print(classes)
|
| 32 |
+
if "PdfSentence" not in classes:
|
| 33 |
+
create_schema()
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
# ์คํค๋ง๊ฐ ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ์์ฑ
|
| 37 |
+
# if not client.schema.contains({"class": "PdfSentence"}):
|
| 38 |
+
# create_schema()
|
| 39 |
+
|
| 40 |
+
# PDF ํ
์คํธ ์ถ์ถ ํจ์
|
| 41 |
+
def extract_text_from_pdf(pdf):
|
| 42 |
+
pdf_reader = PyPDF2.PdfReader(pdf)
|
| 43 |
+
text = ""
|
| 44 |
+
for page in pdf_reader.pages:
|
| 45 |
+
text += page.extract_text()
|
| 46 |
+
return text
|
| 47 |
+
|
| 48 |
+
# ํ
์คํธ ์๋ฒ ๋ฉ ์์ฑ ํจ์
|
| 49 |
+
def create_embeddings(text, model):
|
| 50 |
+
result = model.encode(text)
|
| 51 |
+
return result.astype(np.float64).tolist()
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
# Weaviate์ ๋ฐ์ดํฐ ์ ์ฅ
|
| 55 |
+
def store_vectors_in_weaviate(sentences, embed_model):
|
| 56 |
+
with db_client.batch as batch:
|
| 57 |
+
for sentence in sentences:
|
| 58 |
+
try:
|
| 59 |
+
embedding = create_embeddings(sentence, embed_model)
|
| 60 |
+
# print("embedding", embedding)
|
| 61 |
+
|
| 62 |
+
# Weaviate์ ๋ฌธ์ฅ๊ณผ ๋ฒกํฐ ์ ์ฅ
|
| 63 |
+
data_object = {
|
| 64 |
+
"sentence": sentence,
|
| 65 |
+
"embedding": embedding
|
| 66 |
+
}
|
| 67 |
+
batch.add_data_object(data_object, "PdfSentence")
|
| 68 |
+
print("success")
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(e)
|
| 71 |
+
|
| 72 |
+
# ์ง๋ฌธ์ ๊ฐ์ฅ ์ ์ฌํ ๋ฌธ์ฅ ์ฐพ๊ธฐ
|
| 73 |
+
def find_similar_sentence_in_weaviate(question_embedding):
|
| 74 |
+
near_vector = {
|
| 75 |
+
"vector": question_embedding
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
result = db_client.query.get("PdfSentence", ["sentence", "embedding"]) \
|
| 79 |
+
.with_near_vector(near_vector) \
|
| 80 |
+
.do()
|
| 81 |
+
# .with_limit(1) \
|
| 82 |
+
|
| 83 |
+
return result # ๊ฐ์ฅ ๊ฐ๊น์ด ์๋ ๋ฌธ์ฅ์ด ๋ฐํ
|
| 84 |
+
# return result['data']['Get']['PdfSentence'][0]['sentence']
|
| 85 |
+
|
| 86 |
+
def generate_answer(pdf, question):
|
| 87 |
+
global embed_model
|
| 88 |
+
# PDF ํ
์คํธ ์ถ์ถ
|
| 89 |
+
text = extract_text_from_pdf(pdf)
|
| 90 |
+
|
| 91 |
+
# ํ
์คํธ๋ฅผ ๋ฌธ์ฅ๋ณ๋ก ๋๋๊ธฐ
|
| 92 |
+
sentences = text.split('. ')
|
| 93 |
+
|
| 94 |
+
# ๋ฌธ์ฅ๋ค์ Weaviate์ ์ ์ฅ
|
| 95 |
+
store_vectors_in_weaviate(sentences, embed_model)
|
| 96 |
+
|
| 97 |
+
# ์ง๋ฌธ์ ๋ํ ์๋ฒ ๋ฉ ์์ฑ
|
| 98 |
+
question_embedding = create_embeddings(question, embed_model)
|
| 99 |
+
|
| 100 |
+
# Weaviate์์ ๊ฐ์ฅ ์ ์ฌํ ๋ฌธ์ฅ ์ฐพ๊ธฐ
|
| 101 |
+
most_similar_sentence = find_similar_sentence_in_weaviate(question_embedding)
|
| 102 |
+
print("debug03")
|
| 103 |
+
print(most_similar_sentence)
|
| 104 |
+
|
| 105 |
+
# OpenAI API๋ก ์๋ต ์์ฑ
|
| 106 |
+
ai_client = OpenAI(api_key='sk-TWonV6ldIlpQzTtp5WDW3IiE1mJtQ5eP2p3arsIkDQT3BlbkFJ87T5N5D4WQFHo-QitD7sFOBL6360GxdKTNYpuPbV8A')
|
| 107 |
+
response = ai_client.chat.completions.create(
|
| 108 |
+
model="gpt-4o-mini",
|
| 109 |
+
messages=[
|
| 110 |
+
{"role": "system", "content": f"You are a helpful assistant. Answer based on context: {most_similar_sentence}"},
|
| 111 |
+
{"role": "user", "content": question}
|
| 112 |
+
]
|
| 113 |
+
)
|
| 114 |
+
result = response.choices[0].message.content
|
| 115 |
+
|
| 116 |
+
return result
|
| 117 |
+
|
| 118 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 119 |
+
def interface(pdf, question):
|
| 120 |
+
return generate_answer(pdf, question)
|
| 121 |
+
|
| 122 |
+
if __name__=="__main__":
|
| 123 |
+
# ์๋ฒ ๋ฉ ๋ชจ๋ธ ๋ก๋
|
| 124 |
+
embed_model = SentenceTransformer('xlm-r-100langs-bert-base-nli-stsb-mean-tokens')
|
| 125 |
+
|
| 126 |
+
# Weaviate ํด๋ผ์ด์ธํธ ์ค์
|
| 127 |
+
db_client = weaviate.Client(
|
| 128 |
+
url="https://ildmdarvrfcox58ff2tipw.c0.us-west3.gcp.weaviate.cloud", # ํด๋ฌ์คํฐ URL
|
| 129 |
+
auth_client_secret=weaviate.AuthApiKey(api_key="SPmVOW99EWg8LkstmLlsKUSuSiHfoefcLQwS"), # API ํค ์ค์
|
| 130 |
+
# timeout_config=(5, 150) # ํ์์์ ์ค์ (์ ํ ์ฌํญ)
|
| 131 |
+
)
|
| 132 |
+
|
| 133 |
+
ensure_schema()
|
| 134 |
+
|
| 135 |
+
# Gradio UI ์์ฑ
|
| 136 |
+
with gr.Blocks() as demo:
|
| 137 |
+
pdf_input = gr.File(label="Upload PDF", type="filepath")
|
| 138 |
+
question_input = gr.Textbox(label="Ask a question", placeholder="What do you want to know?")
|
| 139 |
+
output = gr.Textbox(label="Answer")
|
| 140 |
+
|
| 141 |
+
submit_btn = gr.Button("Submit")
|
| 142 |
+
submit_btn.click(fn=interface, inputs=[pdf_input, question_input], outputs=output)
|
| 143 |
+
|
| 144 |
+
# ์ฑ ์คํ
|
| 145 |
+
demo.launch()
|
| 146 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
weaviate-client==4.7.1
|
| 2 |
+
openai==1.32.0
|
| 3 |
+
PyPDF2==3.0.1
|
| 4 |
+
transformers==4.43.1
|
| 5 |
+
gradio==4.39.0
|
| 6 |
+
sentence-transformers
|