Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,18 @@
|
|
1 |
import gradio as gr
|
2 |
-
import os
|
3 |
-
import json
|
4 |
import requests
|
5 |
|
6 |
-
|
7 |
-
API_URL = "https://api.openai.com/v1/chat/completions" #os.getenv("API_URL") + "/generate_stream"
|
8 |
-
|
9 |
-
#Testing with my Open AI Key
|
10 |
-
OPENAI_API_KEY = os.getenv("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")
|
11 |
|
12 |
def predict(inputs, top_p, temperature, openai_api_key, chat_counter, chatbot=[], history=[]):
|
13 |
-
|
14 |
-
# ์ฌ์ฉ์์ ์
๋ ฅ์ ๋๋ ์ด์
์คํ์ผ์ ํ๋กฌํํธ๋ก ๋ณํ
|
15 |
narration_prompt = f"๋์์์ ์ฌ์ฉํ ์ ๋ฌธ์ ์ธ ๋๋ ์ด์
์ ์์ฑํ๋ผ. ๋ฐ๋์ ํ๊ธ๋ก ์์ฑํ ๊ฒ. ์ผ์ฒด์ ์ง๋ฌธ์ด๋ ์ง์, ๋ฐฐ๊ฒฝ ์ค๋ช
๋ฑ์ ๋
ธ์ถ ํ๊ฑฐ๋ ์ถ๋ ฅํ์ง ๋ง๊ณ ์์ํ ๋๋ ์ด์
๋ง 2์ค์ฉ ๋ฌถ์ด์ ์ต๋ 8์ค ์ด๋ด๋ก ์ถ๋ ฅ๋ ฅ. ์
๋ ฅ: '{inputs}'"
|
16 |
|
|
|
|
|
|
|
|
|
|
|
17 |
payload = {
|
18 |
"model": "gpt-4-1106-preview",
|
19 |
"messages": [{"role": "system", "content": narration_prompt}],
|
@@ -23,134 +22,78 @@ def predict(inputs, top_p, temperature, openai_api_key, chat_counter, chatbot=[]
|
|
23 |
"stream": True,
|
24 |
"presence_penalty": 0,
|
25 |
"frequency_penalty": 0,
|
26 |
-
"max_tokens": 1000
|
27 |
}
|
28 |
|
|
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
"Authorization": f"Bearer {openai_api_key}"
|
33 |
-
}
|
34 |
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
temp2["role"] = "assistant"
|
44 |
-
temp2["content"] = data[1]
|
45 |
-
messages.append(temp1)
|
46 |
-
messages.append(temp2)
|
47 |
-
temp3 = {}
|
48 |
-
temp3["role"] = "user"
|
49 |
-
temp3["content"] = inputs
|
50 |
-
messages.append(temp3)
|
51 |
-
#messages
|
52 |
-
payload = {
|
53 |
-
"model": "gpt-4-1106-preview",
|
54 |
-
"messages": messages, #[{"role": "user", "content": f"{inputs}"}],
|
55 |
-
"temperature" : temperature, #1.0,
|
56 |
-
"top_p": top_p, #1.0,
|
57 |
-
"n" : 1,
|
58 |
-
"stream": True,
|
59 |
-
"presence_penalty":0,
|
60 |
-
"frequency_penalty":0,
|
61 |
-
}
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
-
history.append(inputs)
|
66 |
-
print(f"payload is - {payload}")
|
67 |
-
# make a POST request to the API endpoint using the requests.post method, passing in stream=True
|
68 |
-
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
69 |
-
#response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
70 |
-
token_counter = 0
|
71 |
-
partial_words = ""
|
72 |
-
|
73 |
-
counter=0
|
74 |
-
for chunk in response.iter_lines():
|
75 |
-
#Skipping first chunk
|
76 |
-
if counter == 0:
|
77 |
-
counter+=1
|
78 |
-
continue
|
79 |
-
# ๋๋ ์ด์
์ ๊ธธ์ด๋ฅผ 8์ค๋ก ์ ํ
|
80 |
-
if len(history) >= 8:
|
81 |
-
break
|
82 |
-
#counter+=1
|
83 |
-
# check whether each line is non-empty
|
84 |
-
if chunk.decode() :
|
85 |
-
chunk = chunk.decode()
|
86 |
-
# decode each line as response data is in bytes
|
87 |
-
if len(chunk) > 12 and "content" in json.loads(chunk[6:])['choices'][0]['delta']:
|
88 |
-
#if len(json.loads(chunk.decode()[6:])['choices'][0]["delta"]) == 0:
|
89 |
-
# break
|
90 |
-
partial_words = partial_words + json.loads(chunk[6:])['choices'][0]["delta"]["content"]
|
91 |
-
if token_counter == 0:
|
92 |
-
history.append(" " + partial_words)
|
93 |
-
else:
|
94 |
-
history[-1] = partial_words
|
95 |
-
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2) ] # convert to tuples of list
|
96 |
-
token_counter+=1
|
97 |
-
yield chat, history, chat_counter # resembles {chatbot: chat, state: history}
|
98 |
-
|
99 |
-
|
100 |
-
return chat, history, chat_counter
|
101 |
|
102 |
|
103 |
def reset_textbox():
|
104 |
return gr.update(value='')
|
105 |
|
106 |
-
title = """<h1 align=
|
107 |
-
description = "
|
108 |
-
|
109 |
-
|
110 |
-
Assistant: <utterance>
|
111 |
-
User: <utterance>
|
112 |
-
Assistant: <utterance>
|
113 |
-
...
|
114 |
-
```
|
115 |
-
In this app, you can explore the outputs of a gpt-3.5-turbo LLM.
|
116 |
-
"""
|
117 |
-
|
118 |
-
|
119 |
-
with gr.Blocks(css = """#col_container {width: 1000px; margin-left: auto; margin-right: auto;}
|
120 |
-
#chatbot {height: 520px; overflow: auto;}""") as demo:
|
121 |
gr.HTML(title)
|
122 |
-
with gr.Column(elem_id
|
123 |
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
inputs = gr.Textbox(placeholder="์ฌ๊ธฐ์ ์
๋ ฅํ์ธ์.", label="๋๋ ์ด์
์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๊ณ ์ถ์ ์ฃผ์ ์ด๋ ๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์.") # t
|
128 |
-
state = gr.State([]) # s
|
129 |
b1 = gr.Button()
|
130 |
-
|
131 |
-
#inputs, top_p, temperature, top_k, repetition_penalty
|
132 |
with gr.Accordion("Parameters", open=False):
|
133 |
-
top_p = gr.Slider(
|
134 |
-
temperature = gr.Slider(
|
135 |
-
#top_k = gr.Slider( minimum=1, maximum=50, value=4, step=1, interactive=True, label="Top-k",)
|
136 |
-
#repetition_penalty = gr.Slider( minimum=0.1, maximum=3.0, value=1.03, step=0.01, interactive=True, label="Repetition Penalty", )
|
137 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
138 |
|
139 |
examples = gr.Examples(examples=[
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
#gr.Markdown(description)
|
155 |
-
demo.queue().launch(debug=True)
|
156 |
-
|
|
|
1 |
import gradio as gr
|
2 |
+
import os
|
3 |
+
import json
|
4 |
import requests
|
5 |
|
6 |
+
API_URL = "https://api.openai.com/v1/chat/completions"
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def predict(inputs, top_p, temperature, openai_api_key, chat_counter, chatbot=[], history=[]):
|
|
|
|
|
9 |
narration_prompt = f"๋์์์ ์ฌ์ฉํ ์ ๋ฌธ์ ์ธ ๋๋ ์ด์
์ ์์ฑํ๋ผ. ๋ฐ๋์ ํ๊ธ๋ก ์์ฑํ ๊ฒ. ์ผ์ฒด์ ์ง๋ฌธ์ด๋ ์ง์, ๋ฐฐ๊ฒฝ ์ค๋ช
๋ฑ์ ๋
ธ์ถ ํ๊ฑฐ๋ ์ถ๋ ฅํ์ง ๋ง๊ณ ์์ํ ๋๋ ์ด์
๋ง 2์ค์ฉ ๋ฌถ์ด์ ์ต๋ 8์ค ์ด๋ด๋ก ์ถ๋ ฅ๋ ฅ. ์
๋ ฅ: '{inputs}'"
|
10 |
|
11 |
+
headers = {
|
12 |
+
"Content-Type": "application/json",
|
13 |
+
"Authorization": f"Bearer {openai_api_key}"
|
14 |
+
}
|
15 |
+
|
16 |
payload = {
|
17 |
"model": "gpt-4-1106-preview",
|
18 |
"messages": [{"role": "system", "content": narration_prompt}],
|
|
|
22 |
"stream": True,
|
23 |
"presence_penalty": 0,
|
24 |
"frequency_penalty": 0,
|
25 |
+
"max_tokens": 1000
|
26 |
}
|
27 |
|
28 |
+
response = requests.post(API_URL, headers=headers, json=payload, stream=True)
|
29 |
|
30 |
+
partial_words = ""
|
31 |
+
token_counter = 0
|
|
|
|
|
32 |
|
33 |
+
try:
|
34 |
+
for chunk in response.iter_lines():
|
35 |
+
if chunk:
|
36 |
+
try:
|
37 |
+
chunk_text = chunk.decode()
|
38 |
+
print("Raw Chunk:", chunk_text) # ์์ ์๋ต ์ถ๋ ฅ
|
39 |
+
chunk_data = json.loads(chunk_text[6:]) # JSON ํ์ฑ
|
40 |
+
print("Parsed Data:", chunk_data) # ํ์ฑ๋ ๋ฐ์ดํฐ ์ถ๋ ฅ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
if 'choices' in chunk_data and 'content' in chunk_data['choices'][0]['delta']:
|
43 |
+
partial_words += chunk_data['choices'][0]['delta']['content']
|
44 |
+
if token_counter == 0:
|
45 |
+
history.append(" " + partial_words)
|
46 |
+
else:
|
47 |
+
history[-1] = partial_words
|
48 |
+
token_counter += 1
|
49 |
+
chat = [(history[i], history[i + 1]) for i in range(0, len(history) - 1, 2)]
|
50 |
+
yield chat, history, chat_counter
|
51 |
+
except json.JSONDecodeError as e:
|
52 |
+
print("JSON ํ์ฑ ์ค๋ฅ:", e)
|
53 |
+
except Exception as e:
|
54 |
+
print("์๋ต ์ฒ๋ฆฌ ์ค๋ฅ:", e)
|
55 |
+
|
56 |
+
return chatbot, history, chat_counter
|
57 |
+
|
58 |
+
# ๋๋จธ์ง ์ฝ๋ ๋ถ๋ถ (์ธํฐํ์ด์ค ์์ฑ, ์์ ์ถ๊ฐ ๋ฑ)์ ๊ทธ๋๋ก ์ ์ง๋ฉ๋๋ค.
|
59 |
+
|
60 |
+
# ์ฝ๋ ์คํ ๋ถ๋ถ
|
61 |
+
# ์: demo.launch() ๋ฑ
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
64 |
|
65 |
def reset_textbox():
|
66 |
return gr.update(value='')
|
67 |
|
68 |
+
title = """<h1 align='center'>ํ์ ์คํฌ๋ฆฝํธ</h1>"""
|
69 |
+
description = "์์ ์์ฑ์ ์ํ ์คํฌ๋ฆฝํธ๋ฅผ AI๊ฐ ์๋์ผ๋ก ์์ฑํฉ๋๋ค. ์ฃผ์ ํค์๋๋ ๋ชฉ์ ๋ฑ ํ์ํ ๋ด์ฉ๋ง ๊ฐ๋จํ ์
๋ ฅํ์ธ์."
|
70 |
+
|
71 |
+
with gr.Blocks(css="#col_container {width: 1000px; margin-left: auto; margin-right: auto;} #chatbot {height: 520px; overflow: auto;}") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
gr.HTML(title)
|
73 |
+
with gr.Column(elem_id="col_container"):
|
74 |
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
75 |
+
chatbot = gr.Chatbot(elem_id='chatbot')
|
76 |
+
inputs = gr.Textbox(placeholder="์ฌ๊ธฐ์ ์
๋ ฅํ์ธ์.", label="๋๋ ์ด์
์คํฌ๋ฆฝํธ๋ฅผ ์์ฑํ๊ณ ์ถ์ ์ฃผ์ ์ด๋ ๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์.")
|
77 |
+
state = gr.State([])
|
|
|
|
|
78 |
b1 = gr.Button()
|
79 |
+
|
|
|
80 |
with gr.Accordion("Parameters", open=False):
|
81 |
+
top_p = gr.Slider(minimum=0, maximum=1.0, value=1.0, step=0.05, label="Top-p (nucleus sampling)")
|
82 |
+
temperature = gr.Slider(minimum=0, maximum=5.0, value=1.0, step=0.1, label="Temperature")
|
|
|
|
|
83 |
chat_counter = gr.Number(value=0, visible=False, precision=0)
|
84 |
|
85 |
examples = gr.Examples(examples=[
|
86 |
+
["์ํ ์ค๋ช
:์๋ก ์ถ์๋ 'ํ ๋ฆฌ' ๋ฆฝ๋ฐค์ FDA ์น์ธ, ์ต๊ณ ์ ๋ณด์ต๋ ฅ, ๊ตฌ๋งค์ง์ 1์"],
|
87 |
+
["๋ธ๋๋ฉ: 'ํ ๋ฆฌ'๋ฆฝ๋ฐค์ 20๋ ์ฌ์ฑ์๊ฒ ์ดํํ ๋ธ๋๋ฉ์ด ํ์ํด"],
|
88 |
+
["๊ด๊ณ : ์ค๋ ๋ถ๋ชจ๋๊ณผ ์น์ง ์ ๋ฌผ์ ๋ฒ์ฑํฌ ๋ณด๋ฆฌ๊ตด๋น '๋ฒ์ฑ๊ตด๋น'๊ฐ ์ต๊ณ ๋๋๋ค."],
|
89 |
+
["์ ๋ณด ๊ณต์ : ๋นํ๋ฏผC ๊ณผ๋ค ๋ณต์ฉ์ ๊ฑด๊ฐ์ ์คํ๋ ค ํด๋กญ๋ค."],
|
90 |
+
["ํ๋ณด: 'OpenAI'๋ '์ฑGPT'์ ๋ง์ถค GPT '์คํ ์ด'๋ฅผ ์คํํ์๋ค."],
|
91 |
+
["์ธ์ฌ: '์ ํ ๋ฒ์ธ'์ ๊ณ ๊ฐ๊ณผ ์์ง์์ ์ํ ์ง์ทจ์ ์ธ 2024๋
์ ๋
์ธ์ฌ"]
|
92 |
+
], inputs=[inputs], fn=predict, outputs=[chatbot, state, chat_counter])
|
93 |
+
|
94 |
+
inputs.submit(predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
95 |
+
b1.click(predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter])
|
96 |
+
b1.click(reset_textbox, [], [inputs])
|
97 |
+
inputs.submit(reset_textbox, [], [inputs])
|
98 |
+
|
99 |
+
demo.launch(debug=True)
|
|
|
|
|
|