Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,162 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import pymssql
|
3 |
-
import pandas as pd
|
4 |
-
|
5 |
-
# os.environ["OPENAI_API_KEY"] = "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"
|
6 |
-
os.environ["OPENAI_API_KEY"] = "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"
|
7 |
-
|
8 |
-
from langchain.vectorstores import Chroma
|
9 |
-
from langchain.embeddings import OpenAIEmbeddings
|
10 |
-
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
11 |
-
from langchain.llms import OpenAI
|
12 |
-
from langchain.chat_models import ChatOpenAI
|
13 |
-
from langchain.chains import RetrievalQA
|
14 |
-
from langchain.document_loaders import TextLoader
|
15 |
-
from langchain.document_loaders import DirectoryLoader
|
16 |
-
from langchain.document_loaders import CSVLoader
|
17 |
-
from langchain.memory import ConversationBufferMemory
|
18 |
-
|
19 |
-
# # ์ฌ์ฉ์๊ฐ ์ง๋ฌธํ ๋ด์ญ ์ ์ฅํด์ ๋์ค์ ํ์ต์ฉ์ผ๋ก ์ฐ๊ธฐ ์ํด DB ์ ์
|
20 |
-
# # MSSQL ์ ์
|
21 |
-
# conn = pymssql.connect(host=r"(local)", database='Chatbot_Manage')
|
22 |
-
# conn.autocommit(True) # ์คํ ์ปค๋ฐ ํ์ฑํ
|
23 |
-
# # Connection ์ผ๋ก๋ถํฐ Cursor ์์ฑ
|
24 |
-
# _cursor = conn.cursor()
|
25 |
-
# _cursorConfig = conn.cursor()
|
26 |
-
|
27 |
-
# # _query = "SELECT UserInput as query, SystemAnswer as answer FROM ChatHistory WHERE AcceptFlag = 'Y'"
|
28 |
-
# _queryConfig = "SELECT Temperature FROM ChatConfig WHERE IsUse = 1"
|
29 |
-
|
30 |
-
# # _cursor.execute(_query)
|
31 |
-
# _cursorConfig.execute(_queryConfig)
|
32 |
-
|
33 |
-
|
34 |
-
# #์คํํ ๊ฐ, ์ด๋ฆ ๊ฐ์ DataFrame์ ์ ์ฅ
|
35 |
-
# #dfsql = ['query','answer'] #๋ฐ์ดํฐํ๋ ์ ์ปฌ๋ผ์ ์ด๋ฆ ์ค์ .
|
36 |
-
# # _row = cursor.fetchall()
|
37 |
-
# # df1 = pd.DataFrame(_row, columns=dfsql)
|
38 |
-
|
39 |
-
# _rowConfing = _cursorConfig.fetchone()
|
40 |
-
|
41 |
-
# while _rowConfing:
|
42 |
-
# for col in range(len(_rowConfing)):
|
43 |
-
# temperature = _rowConfing[col]
|
44 |
-
# _rowConfing = _cursorConfig.fetchone()
|
45 |
-
|
46 |
-
# conn.close() ## ์ฐ๊ฒฐ ๋๊ธฐ
|
47 |
-
|
48 |
-
persist_directory = 'realdb_LLM'
|
49 |
-
|
50 |
-
embedding = OpenAIEmbeddings()
|
51 |
-
|
52 |
-
vectordb = Chroma(
|
53 |
-
persist_directory=persist_directory,
|
54 |
-
embedding_function=embedding
|
55 |
-
)
|
56 |
-
|
57 |
-
retriever = vectordb.as_retriever(search_kwargs={"k": 1})
|
58 |
-
|
59 |
-
|
60 |
-
def process_llm_response(llm_response):
|
61 |
-
print(llm_response['result'])
|
62 |
-
print('\n\nSources:')
|
63 |
-
for source in llm_response["source_documents"]:
|
64 |
-
print(source.metadata['source'])
|
65 |
-
|
66 |
-
|
67 |
-
# ์ฑ๋ด์ ๋ต๋ณ์ ์ฒ๋ฆฌํ๋ ํจ์
|
68 |
-
def respond(message, chat_history, temperature):
|
69 |
-
|
70 |
-
qa_chain = RetrievalQA.from_chain_type(
|
71 |
-
llm=OpenAI(temperature=0.4),
|
72 |
-
# llm=OpenAI(temperature=0.4),
|
73 |
-
# llm=ChatOpenAI(temperature=0),
|
74 |
-
chain_type="stuff",
|
75 |
-
retriever=retriever
|
76 |
-
)
|
77 |
-
|
78 |
-
result = qa_chain(message)
|
79 |
-
|
80 |
-
bot_message = result['result']
|
81 |
-
|
82 |
-
# bot_message += '\n\n' + ' [์ถ์ฒ]'
|
83 |
-
|
84 |
-
# # ๋ต๋ณ์ ์ถ์ฒ๋ฅผ ํ๊ธฐ
|
85 |
-
# for i, doc in enumerate(result['source_documents']):
|
86 |
-
# bot_message += str(i+1) + '. ' + doc.metadata['source'] + ' '
|
87 |
-
|
88 |
-
# ์ฑํ
๊ธฐ๋ก์ ์ฌ์ฉ์์ ๋ฉ์์ง์ ๋ด์ ์๋ต์ ์ถ๊ฐ.
|
89 |
-
chat_history.append((message, bot_message))
|
90 |
-
|
91 |
-
historySave(message=message, answer=str(result['result']).replace("'",""))
|
92 |
-
# historySave(message=message, answer="")
|
93 |
-
|
94 |
-
return "", chat_history
|
95 |
-
|
96 |
-
def historySave(message, answer):
|
97 |
-
|
98 |
-
conn = pymssql.connect(host=r"(local)", database='Chatbot_Manage', charset='utf8')
|
99 |
-
conn.autocommit(True) # ์คํ ์ปค๋ฐ ํ์ฑํ
|
100 |
-
# Connection ์ผ๋ก๋ถํฐ Cursor ์์ฑ
|
101 |
-
cursor = conn.cursor()
|
102 |
-
|
103 |
-
SystemType = "OpenAI(Real LLM)"
|
104 |
-
|
105 |
-
# SQL๋ฌธ ์คํ'
|
106 |
-
_sql = "EXEC ChatHistory_InsUpd '" + SystemType + "','" + message + "', '" + answer + "'"
|
107 |
-
cursor.execute(_sql)
|
108 |
-
|
109 |
-
conn.close() ## ์ฐ๊ฒฐ ๋๊ธฐ
|
110 |
-
|
111 |
-
import gradio as gr
|
112 |
-
|
113 |
-
# ์ฑ๋ด ์ค๋ช
|
114 |
-
title = """
|
115 |
-
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
116 |
-
<div>
|
117 |
-
<h1>Pretraining Chatbot V2 Real</h1>
|
118 |
-
</div>
|
119 |
-
<p style="margin-bottom: 10px; font-size: 94%">
|
120 |
-
OpenAI LLM๋ฅผ ์ด์ฉํ Chatbot (Similarity)
|
121 |
-
</p>
|
122 |
-
</div>
|
123 |
-
"""
|
124 |
-
|
125 |
-
# ๊พธ๋ฏธ๊ธฐ
|
126 |
-
css="""
|
127 |
-
#col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
|
128 |
-
"""
|
129 |
-
with gr.Blocks(css=css) as UnivChatbot:
|
130 |
-
with gr.Column(elem_id="col-container"):
|
131 |
-
gr.HTML(title)
|
132 |
-
|
133 |
-
# with gr.Row():
|
134 |
-
# with gr.Column(scale=3):
|
135 |
-
# openai_key = gr.Textbox(label="You OpenAI API key", type="password", placeholder="OpenAI Key Type", elem_id="InputKey", show_label=False, container=False)
|
136 |
-
# with gr.Column(scale=1):
|
137 |
-
# langchain_status = gr.Textbox(placeholder="Status", interactive=False, show_label=False, container=False)
|
138 |
-
# with gr.Column(scale=1):
|
139 |
-
# chk_key = gr.Button("ํ์ธ", variant="primary")
|
140 |
-
|
141 |
-
chatbot = gr.Chatbot(label="๋ํ ์ฑ๋ด์์คํ
(OpenAI LLM)", elem_id="chatbot") # ์๋จ ์ข์ธก
|
142 |
-
|
143 |
-
with gr.Row():
|
144 |
-
with gr.Column(scale=9):
|
145 |
-
msg = gr.Textbox(label="์
๋ ฅ", placeholder="๊ถ๊ธํ์ ๋ด์ญ์ ์
๋ ฅํ์ฌ ์ฃผ์ธ์.", elem_id="InputQuery", show_label=False, container=False)
|
146 |
-
|
147 |
-
with gr.Row():
|
148 |
-
with gr.Column(scale=1):
|
149 |
-
submit = gr.Button("์ ์ก", variant="primary")
|
150 |
-
with gr.Column(scale=1):
|
151 |
-
clear = gr.Button("์ด๊ธฐํ", variant="stop")
|
152 |
-
|
153 |
-
# ์ฌ์ฉ์์ ์
๋ ฅ์ ์ ์ถ(submit)ํ๋ฉด respond ํจ์๊ฐ ํธ์ถ.
|
154 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
155 |
-
|
156 |
-
submit.click(respond, [msg, chatbot], [msg, chatbot])
|
157 |
-
|
158 |
-
# '์ด๊ธฐํ' ๋ฒํผ์ ํด๋ฆญํ๋ฉด ์ฑํ
๊ธฐ๋ก์ ์ด๊ธฐํ.
|
159 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
160 |
-
|
161 |
-
|
162 |
-
UnivChatbot.launch(server_port=60001)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|