File size: 10,230 Bytes
96fe5d5 f016720 96fe5d5 f016720 96fe5d5 2aa7156 96fe5d5 3fb1a1c 85c8d68 96fe5d5 a405c81 96fe5d5 2aa7156 a405c81 f016720 a405c81 68fea43 bc373af f016720 96fe5d5 68fea43 f016720 68fea43 f016720 68fea43 96fe5d5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
import gradio as gr
from openai import OpenAI
import os
import openai
# client = OpenAI(api_key="sk-9hWFK-_5UQgzJMqT44ng72sZbKQXVvVXeVd11yoER3T3BlbkFJmd2HDc-N7CaH6PDRCYZfqe8hkT7DnxZNwcVkf3eeUA")
openai.api_key ="sk-9hWFK-_5UQgzJMqT44ng72sZbKQXVvVXeVd11yoER3T3BlbkFJmd2HDc-N7CaH6PDRCYZfqe8hkT7DnxZNwcVkf3eeUA"
import gradio as gr
# from langchain.chat_models import ChatOpenAI
# from langchain.memory import ConversationBufferMemory
# from langchain.chains import ConversationChain
# from langchain.schema import AIMessage, HumanMessage, SystemMessage
# from langchain.document_loaders import TextLoader
# from langchain.document_loaders import PyPDFLoader
#from langchain_community.chat_models import ChatOpenAI
from langchain_openai import ChatOpenAI
# from langchain_community.memory import ConversationBufferMemory
from langchain.memory import ConversationBufferMemory
from langchain.chains import ConversationChain
from langchain.schema import AIMessage, HumanMessage, SystemMessage
from langchain_community.document_loaders import TextLoader
from langchain_community.document_loaders import PyPDFLoader
from langchain_core.runnables import RunnableWithMessageHistory
# LLMκ³Ό λ©λͺ¨λ¦¬ μ΄κΈ°ν
llm = ChatOpenAI(temperature=0.0, model='gpt-3.5-turbo', openai_api_key="sk-9hWFK-_5UQgzJMqT44ng72sZbKQXVvVXeVd11yoER3T3BlbkFJmd2HDc-N7CaH6PDRCYZfqe8hkT7DnxZNwcVkf3eeUA")
memory = ConversationBufferMemory()
#memory = RunnableWithMessageHistory()
conversation = ConversationChain(
llm=llm,
memory=memory)
# μΈμ
νμ€ν 리 μ μ
def get_session_history():
return [] # μΈμ
νμ€ν 리λ₯Ό κ΄λ¦¬νλ λ‘μ§μ ꡬνν μ μμ΅λλ€.
# λ©λͺ¨λ¦¬ λ° λν μ΄κΈ°ν
# memory = RunnableWithMessageHistory(runnable=llm, get_session_history=get_session_history)
# conversation = RunnableWithMessageHistory(llm=llm, memory=memory)
# μλ΄λ΄ - μ±ν
λ° λ΅λ³
def counseling_bot_chat(message, chat_history):
if message == "":
return "", chat_history
else:
result_message = ""
if len(chat_history) <= 1:
messages = [
SystemMessage(content="λΉμ μ ν€μ΄λ§νΈμ μλ΄μμ
λλ€. λ§νΈ μνκ³Ό κ΄λ ¨λμ§ μμ μ§λ¬Έμλ μ μ€ν κ±°μ νμΈμ."),
AIMessage(content="μλ
νμΈμ, ν€μ΄λ§νΈμ
λλ€. μλ΄μ λμλλ¦¬κ² μ΅λλ€."),
HumanMessage(content=message)
]
result_message = conversation.predict(input=messages)
else:
result_message = conversation.predict(input=message)
chat_history.append([message, result_message])
return "", chat_history
# μλ΄λ΄ - λλ리기
def counseling_bot_undo(chat_history):
if len(chat_history) > 1:
chat_history.pop()
return chat_history
# μλ΄λ΄ - μ΄κΈ°ν
def counseling_bot_reset(chat_history):
chat_history = [[None, "μλ
νμΈμ, ν€μ΄λ§νΈμ
λλ€. μλ΄μ λμλλ¦¬κ² μ΅λλ€."]]
return chat_history
# λ²μλ΄
def translate_bot(output_conditions, output_language, input_text):
if input_text == "":
return ""
else:
if output_conditions == "":
output_conditions = ""
else:
output_conditions = "λ²μν λμ 쑰건μ λ€μκ³Ό κ°μ΅λλ€. " + output_conditions
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "λΉμ μ λ²μκ°μ
λλ€. μ
λ ₯ν μΈμ΄λ₯Ό λ€λ₯Έ μ€λͺ
μμ΄ κ³§λ°λ‘ {0}λ‘ λ²μν΄μ μλ €μ£ΌμΈμ. λ²μμ΄ λΆκ°λ₯ν μΈμ΄λΌλ©΄ λ²μμ΄ λΆκ°λ₯νλ€κ³ λ§ν ν κ·Έ μ΄μ λ₯Ό μ€λͺ
ν΄μ£ΌμΈμ. {1}".format(output_language, output_conditions)},
{"role": "user", "content": input_text}
])
return completion.choices[0].message.content
# λ²μλ΄ - Textμ
λ‘λ
def translate_bot_Text_upload(files):
loader = TextLoader(files)
document = loader.load()
return document[0].page_content
# λ²μλ΄ - PDFμ
λ‘λ
def translate_bot_PDF_upload(files):
loader = PyPDFLoader(files)
pages = loader.load_and_split()
return pages[0].page_content
# μμ€λ΄
def novel_bot(model, temperature, detail):
completion = client.chat.completions.create(
model=model,
temperature=temperature,
messages=[
{"role": "system", "content": "λΉμ μ μμ€κ°μ
λλ€. μμ²νλ 쑰건μ λ§μΆ° μμ€μ μμ±ν΄μ£ΌμΈμ."},
{"role": "user", "content": detail}
])
return completion.choices[0].message.content
# λ μ΄μμ
with gr.Blocks(theme=gr.themes.Default()) as app:
with gr.Tab("μλ΄λ΄"):
#1
gr.Markdown(
value="""
# <center>μλ΄λ΄</center>
<center>ν€μ΄λ§νΈ μλ΄ λ΄μ
λλ€. λ§νΈμμ ν맀νλ μνκ³Ό κ΄λ ¨λ μ§λ¬Έμ λ΅λ³λ립λλ€.</center>
""")
#2
cb_chatbot = gr.Chatbot(
value=[[None, "μλ
νμΈμ, ν€μ΄λ§νΈμ
λλ€. μλ΄μ λμλλ¦¬κ² μ΅λλ€."]],
show_label=False
)
with gr.Row():
#3
cb_user_input = gr.Text(
lines=1,
placeholder="μ
λ ₯ μ°½",
container=False,
scale=9
)
#4
cb_send_btn = gr.Button(
value="보λ΄κΈ°",
scale=1,
variant="primary",
icon="https://cdn-icons-png.flaticon.com/128/12439/12439334.png"
)
with gr.Row():
#5
gr.Button(value="β©οΈ λλ리기").click(fn=counseling_bot_undo, inputs=cb_chatbot, outputs=cb_chatbot)
#6
gr.Button(value="ποΈ μ΄κΈ°ν").click(fn=counseling_bot_reset, inputs=cb_chatbot, outputs=cb_chatbot)
# 보λ΄κΈ°1
cb_send_btn.click(fn=counseling_bot_chat, inputs=[cb_user_input, cb_chatbot], outputs=[cb_user_input, cb_chatbot])
# 보λ΄κΈ°2
cb_user_input.submit(fn=counseling_bot_chat, inputs=[cb_user_input, cb_chatbot], outputs=[cb_user_input, cb_chatbot])
with gr.Tab("λ²μλ΄"):
#1
gr.Markdown(
value="""
# <center>λ²μλ΄</center>
<center>λ€κ΅μ΄ λ²μ λ΄μ
λλ€.</center>
""")
with gr.Row():
#2
tb_output_conditions = gr.Text(
label="λ²μ 쑰건",
placeholder="μμ: μμ°μ€λ½κ²",
lines=1,
max_lines=3
)
#3
tb_output_language = gr.Dropdown(
label="μΆλ ₯ μΈμ΄",
choices=["νκ΅μ΄", "μμ΄", "μΌλ³Έμ΄", "μ€κ΅μ΄"],
value="νκ΅μ΄",
allow_custom_value=True,
interactive=True
)
with gr.Row():
#7
tb_TXTupload = gr.UploadButton(label="π Txt μ
λ‘λ")
#8
tb_PDFupload = gr.UploadButton(label="π€ PDF μ
λ‘λ")
#4
tb_submit = gr.Button(
value="λ²μνκΈ°",
variant="primary"
)
with gr.Row():
#5
tb_input_text = gr.Text(
placeholder="λ²μν λ΄μ©μ μ μ΄μ£ΌμΈμ.",
lines=10,
max_lines=20,
show_copy_button=True,
label=""
)
#6
tb_output_text = gr.Text(
lines=10,
max_lines=20,
show_copy_button=True,
label="",
interactive=False
)
# 보λ΄κΈ°
tb_submit.click(
fn=translate_bot,
inputs=[tb_output_conditions,
tb_output_language,
tb_input_text],
outputs=tb_output_text
)
# TextνμΌ μ
λ‘λ
tb_TXTupload.upload(
fn=translate_bot_Text_upload,
inputs=tb_TXTupload,
outputs=tb_input_text
)
# PDFνμΌ μ
λ‘λ
tb_PDFupload.upload(
fn=translate_bot_PDF_upload,
inputs=tb_PDFupload,
outputs=tb_input_text
)
with gr.Tab("μμ€λ΄"):
#1
gr.Markdown(
value="""
# <center>μμ€λ΄</center>
<center>μμ€μ μμ±ν΄μ£Όλ λ΄μ
λλ€.</center>
""")
with gr.Accordion(label="μ¬μ©μ μ€μ "):
with gr.Row():
with gr.Column(scale=1):
#2
nb_model = gr.Dropdown(
label="λͺ¨λΈ μ ν",
choices=["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4", "gpt-4-32k", "gpt-4-1106-preview"],
value="gpt-4-1106-preview",
interactive=True
)
#3
nb_temperature = gr.Slider(
label="μ°½μμ±",
info="μ«μκ° λμ μλ‘ μ°½μμ ",
minimum=0,
maximum=2,
step=0.1,
value=1,
interactive=True
)
#4
nb_detail = gr.Text(
container=False,
placeholder="μμ€μ μΈλΆμ μΈ μ€μ μ μμ±ν©λλ€.",
lines=8,
scale=4
)
#5
nb_submit = gr.Button(
value="μμ±νκΈ°",
variant="primary"
)
#6
nb_output = gr.Text(
label="",
placeholder="μ΄κ³³μ μμ€μ λ΄μ©μ΄ μΆλ ₯λ©λλ€.",
lines=10,
max_lines=200,
show_copy_button=True
)
# 보λ΄κΈ°
nb_submit.click(
fn=novel_bot,
inputs=[nb_model, nb_temperature, nb_detail],
outputs=nb_output
)
app.launch() |