bnvsyjy commited on
Commit
47b57c9
ยท
verified ยท
1 Parent(s): 4f9e9f9

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +147 -0
app.py ADDED
@@ -0,0 +1,147 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from openai import OpenAI
3
+ import time
4
+ import subprocess
5
+
6
+ gpt_api_key = os.environ['OPENAI_API_KEY']
7
+ sys_prompt = os.environ['SYSTEM_PROMPT']
8
+
9
+ client = OpenAI(api_key=gpt_api_key)
10
+ model_name = "ft:gpt-3.5-turbo-1106:personal::8oksosTP"
11
+
12
+ ###ํ•จ์ˆ˜
13
+ #์–ด์‹œ์Šคํ„ดํŠธ ์‘๋‹ต ์ƒ์„ฑ ํ•จ์ˆ˜
14
+ def assistant_choice(assistantid,text):
15
+ # assistant retrieve ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
16
+ assistant = client.beta.assistants.retrieve(assistantid)
17
+
18
+ # message thread ๋งŒ๋“ค๊ธฐ
19
+ thread = client.beta.threads.create()
20
+
21
+ # message ์ถ”๊ฐ€ํ•˜๊ธฐ
22
+ message = client.beta.threads.messages.create(
23
+ thread_id=thread.id,
24
+ role="user",
25
+ content=text,
26
+ )
27
+
28
+ # ์‹คํ–‰ํ•˜๊ธฐ
29
+ run = client.beta.threads.runs.create(
30
+ thread_id=thread.id,
31
+ assistant_id=assistant.id,
32
+ )
33
+
34
+ # ๊ธฐ๋‹ค๋ฆฌ๊ธฐ
35
+ print("run์„ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.")
36
+ while True:
37
+ if run.status == "completed":
38
+ break
39
+ run = client.beta.threads.runs.retrieve(thread_id=thread.id, run_id=run.id)
40
+ # print(run)
41
+ # print("์‹คํ–‰ ์ค‘...")
42
+ time.sleep(5)
43
+
44
+ # ๊ฒฐ๊ณผ ์ถœ๋ ฅํ•˜๊ธฐ
45
+ messages = client.beta.threads.messages.list(thread_id=thread.id)
46
+ gpt_answer = messages.data[0].content[0].text.value
47
+
48
+ return gpt_answer
49
+
50
+ #gpt์‘๋‹ต ์ƒ์„ฑ ํ•จ์ˆ˜
51
+ def fine_function_gpt(state, state_chatbot, text):
52
+ if text=="":
53
+ return state, state_chatbot, state_chatbot
54
+
55
+ messages = state + [{'role': 'user','content': text}]
56
+
57
+ res =client.chat.completions.create(
58
+ model=model_name,
59
+ messages=messages,
60
+ temperature=0.6,
61
+ top_p=1,
62
+ frequency_penalty=0,
63
+ presence_penalty=0
64
+ )
65
+
66
+ msg = res.choices[0].message.content
67
+
68
+ print(msg)
69
+
70
+ if msg == "์ปคํŠผ ๊ตฌ๋งค์˜ต์…˜":
71
+ msg = assistant_choice("asst_ni23TayvHjxOarAxVTDODm91",text)
72
+
73
+ elif msg == "๋ธ”๋ผ์ธ๋“œ ๊ตฌ๋งค์˜ต์…˜":
74
+ msg = assistant_choice("asst_qc85Yh4LjKqWMxlDakHcXsFs",text)
75
+
76
+ new_state = [{'role': 'user','content': text},
77
+ {'role': 'assistant','content': msg}]
78
+
79
+ state = state + new_state # ๋‚ด๋ถ€์ ์œผ๋กœ ๊ธฐ๋Šฅ
80
+ state_chatbot = state_chatbot + [(text, msg)] # ํ™”๋ฉด์— ๋ณด์ด๋Š” ๊ธฐ๋Šฅ
81
+ print(state)
82
+
83
+ return state, state_chatbot, state_chatbot
84
+
85
+ #์ƒ๋‹ด ์‹œ์ž‘ ๋ฒ„ํŠผ
86
+ def start_def(m):
87
+ if m == []:
88
+ return [],[],[("์ƒ๋‹ด์„ ์ง„ํ–‰ํ•  ์ œํ’ˆ์„ ์„ ํƒํ•ด์ฃผ์„ธ์š”!","")]
89
+ else:
90
+ text=f"{m} ๋ฌธ์˜๋“œ๋ฆฝ๋‹ˆ๋‹ค."
91
+ message = [{'role': 'system','content': sys_prompt},
92
+ {'role': 'user','content': text},
93
+ {'role': 'assistant','content': "์•ˆ๋…•ํ•˜์„ธ์š” ๊ณ ๊ฐ๋‹˜~ ๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?"} ]
94
+
95
+ state_chatbot = [("...", "์•ˆ๋…•ํ•˜์„ธ์š”. ๊ณ ๊ฐ๋‹˜! ๋ฌด์—‡์„ ๋„์™€๋“œ๋ฆด๊นŒ์š”?")] # ํŠœํ”Œ
96
+ return message,state_chatbot,state_chatbot
97
+
98
+ with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
99
+ state = gr.State([]) #gpt ํ˜ธ์ถœ ์‹œ ์‚ฌ์šฉํ•˜๋Š” message
100
+ state_chatbot = gr.State([]) #gr.chatbot์˜ ์ƒํƒœ๋ฅผ ์—…๋กœ๋“œํ•˜๋Š” ์ฑ„ํŒ…๊ธฐ๋ก
101
+
102
+ with gr.Row():
103
+ gr.HTML("""<div style="text-align: center; max-width: 550px; margin: 0 auto;">
104
+ <div>
105
+ <p style="font-size: 40px; font-weight: bold;">๋งˆ๋ฆฌํ•˜์šฐ์Šค ์ฑ—๋ด‡</p>
106
+ </div>
107
+ </div>""")
108
+
109
+ with gr.Row():
110
+ with gr.Column(scale=6):
111
+ md = gr.Dropdown(["์ปคํŠผ","๋ธ”๋ผ์ธ๋“œ"],type="value",label="์ œํ’ˆ ์„ ํƒ", info="์›ํ•˜๋Š” ์ƒ๋‹ด ์ œํ’ˆ์„ ๊ณจ๋ผ์ฃผ์„ธ์š”!")
112
+ with gr.Column(scale=4):
113
+ gr.HTML("""<div style="text-align: center;">
114
+ <div>
115
+ <p style="font-size: 15px; font-weight: bold;">ํ™•์ธ</p>
116
+ </div>
117
+ </div>""")
118
+ start_btn = gr.Button("์‹œ์ž‘ํ•˜๊ธฐ")
119
+ with gr.Column(scale=1):
120
+ gr.HTML("""<div style="text-align: center;">
121
+ <div>
122
+ <p style="font-size: 15px; font-weight: bold;">๋‹คํฌ๋ชจ๋“œ ์ „ํ™˜</p>
123
+ </div>
124
+ </div>""")
125
+ toggle_dark = gr.Button(value="Toggle Dark")
126
+ toggle_dark.click(None,
127
+ js="""() => {
128
+ document.body.classList.toggle('dark');
129
+ document.querySelector('gradio-container').style.backgroundColor = 'var(--color-background-primary)'
130
+ }
131
+ """
132
+ )
133
+
134
+ with gr.Row():
135
+ with gr.Column(scale=1):
136
+ chatbot = gr.Chatbot(elem_id='๋ฉ”์„ธ์ง€์ฐฝ',height=650)
137
+ txt = gr.Textbox(show_label=False,placeholder='๋ฉ”์„ธ์ง€๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!')
138
+ txt_btn = gr.Button("๋ฉ”์„ธ์ง€ ๋ณด๋‚ด๊ธฐ")
139
+
140
+ txt_btn.click(fine_function_gpt, [state, state_chatbot, txt], [state, state_chatbot, chatbot])
141
+ txt_btn.click(lambda: '', None, txt)
142
+ txt.submit(fine_function_gpt, [state, state_chatbot, txt], [state, state_chatbot, chatbot]) # ํ…์ŠคํŠธ ๋ฐ•์Šค์—์„œ enter์‹œ ์ „์†ก
143
+ txt.submit(lambda: '', None, txt)
144
+
145
+ start_btn.click(start_def, inputs= [md], outputs = [state, state_chatbot, chatbot])
146
+
147
+ demo.launch(debug=True)