|
import os |
|
from openai import OpenAI |
|
import time |
|
import subprocess |
|
|
|
gpt_api_key = os.environ['OPENAI_API_KEY'] |
|
sys_prompt = os.environ['SYSTEM_PROMPT'] |
|
|
|
client = OpenAI(api_key=gpt_api_key) |
|
model_name = "ft:gpt-3.5-turbo-1106:personal::8oksosTP" |
|
|
|
|
|
|
|
def assistant_choice(assistantid,text): |
|
|
|
assistant = client.beta.assistants.retrieve(assistantid) |
|
|
|
|
|
thread = client.beta.threads.create() |
|
|
|
|
|
message = client.beta.threads.messages.create( |
|
thread_id=thread.id, |
|
role="user", |
|
content=text, |
|
) |
|
|
|
|
|
run = client.beta.threads.runs.create( |
|
thread_id=thread.id, |
|
assistant_id=assistant.id, |
|
) |
|
|
|
|
|
print("run์ ์คํํฉ๋๋ค.") |
|
while True: |
|
if run.status == "completed": |
|
break |
|
run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id) |
|
|
|
|
|
time.sleep(5) |
|
|
|
|
|
messages = client.beta.threads.messages.list(thread_id=thread.id) |
|
gpt_answer = messages.data[0].content[0].text.value |
|
|
|
return gpt_answer |
|
|
|
|
|
def fine_function_gpt(state, state_chatbot, text): |
|
if text=="": |
|
return state, state_chatbot, state_chatbot |
|
|
|
messages = state + [{'role': 'user','content': text}] |
|
|
|
res =client.chat.completions.create( |
|
model=model_name, |
|
messages=messages, |
|
temperature=0.6, |
|
top_p=1, |
|
frequency_penalty=0, |
|
presence_penalty=0 |
|
) |
|
|
|
msg = res.choices[0].message.content |
|
|
|
print(msg) |
|
|
|
if msg == "์ปคํผ ๊ตฌ๋งค์ต์
": |
|
msg = assistant_choice("asst_ni23TayvHjxOarAxVTDODm91",text) |
|
|
|
elif msg == "๋ธ๋ผ์ธ๋ ๊ตฌ๋งค์ต์
": |
|
msg = assistant_choice("asst_qc85Yh4LjKqWMxlDakHcXsFs",text) |
|
|
|
new_state = [{'role': 'user','content': text}, |
|
{'role': 'assistant','content': msg}] |
|
|
|
state = state + new_state |
|
state_chatbot = state_chatbot + [(text, msg)] |
|
print(state) |
|
|
|
return state, state_chatbot, state_chatbot |
|
|
|
|
|
def start_def(m): |
|
if m == []: |
|
return [],[],[("์๋ด์ ์งํํ ์ ํ์ ์ ํํด์ฃผ์ธ์!","")] |
|
else: |
|
text=f"{m} ๋ฌธ์๋๋ฆฝ๋๋ค." |
|
message = [{'role': 'system','content': sys_prompt}, |
|
{'role': 'user','content': text}, |
|
{'role': 'assistant','content': "์๋
ํ์ธ์ ๊ณ ๊ฐ๋~ ๋ฌด์์ ๋์๋๋ฆด๊น์?"} ] |
|
|
|
state_chatbot = [("...", "์๋
ํ์ธ์. ๊ณ ๊ฐ๋! ๋ฌด์์ ๋์๋๋ฆด๊น์?")] |
|
return message,state_chatbot,state_chatbot |
|
|
|
with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo: |
|
state = gr.State([]) |
|
state_chatbot = gr.State([]) |
|
|
|
with gr.Row(): |
|
gr.HTML("""<div style="text-align: center; max-width: 550px; margin: 0 auto;"> |
|
<div> |
|
<p style="font-size: 40px; font-weight: bold;">๋ง๋ฆฌํ์ฐ์ค ์ฑ๋ด</p> |
|
</div> |
|
</div>""") |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=6): |
|
md = gr.Dropdown(["์ปคํผ","๋ธ๋ผ์ธ๋"],type="value",label="์ ํ ์ ํ", info="์ํ๋ ์๋ด ์ ํ์ ๊ณจ๋ผ์ฃผ์ธ์!") |
|
with gr.Column(scale=4): |
|
gr.HTML("""<div style="text-align: center;"> |
|
<div> |
|
<p style="font-size: 15px; font-weight: bold;">ํ์ธ</p> |
|
</div> |
|
</div>""") |
|
start_btn = gr.Button("์์ํ๊ธฐ") |
|
with gr.Column(scale=1): |
|
gr.HTML("""<div style="text-align: center;"> |
|
<div> |
|
<p style="font-size: 15px; font-weight: bold;">๋คํฌ๋ชจ๋ ์ ํ</p> |
|
</div> |
|
</div>""") |
|
toggle_dark = gr.Button(value="Toggle Dark") |
|
toggle_dark.click(None, |
|
js="""() => { |
|
document.body.classList.toggle('dark'); |
|
document.querySelector('gradio-container').style.backgroundColor = 'var(--color-background-primary)' |
|
} |
|
""" |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
chatbot = gr.Chatbot(elem_id='๋ฉ์ธ์ง์ฐฝ',height=650) |
|
txt = gr.Textbox(show_label=False,placeholder='๋ฉ์ธ์ง๋ฅผ ์
๋ ฅํด์ฃผ์ธ์!') |
|
txt_btn = gr.Button("๋ฉ์ธ์ง ๋ณด๋ด๊ธฐ") |
|
|
|
txt_btn.click(fine_function_gpt, [state, state_chatbot, txt], [state, state_chatbot, chatbot]) |
|
txt_btn.click(lambda: '', None, txt) |
|
txt.submit(fine_function_gpt, [state, state_chatbot, txt], [state, state_chatbot, chatbot]) |
|
txt.submit(lambda: '', None, txt) |
|
|
|
start_btn.click(start_def, inputs= [md], outputs = [state, state_chatbot, chatbot]) |
|
|
|
demo.launch(debug=True) |