Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,156 +1,30 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import json
|
4 |
import requests
|
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 |
-
print(f"chat_counter - {chat_counter}")
|
36 |
-
if chat_counter != 0 :
|
37 |
-
messages=[]
|
38 |
-
for data in chatbot:
|
39 |
-
temp1 = {}
|
40 |
-
temp1["role"] = "user"
|
41 |
-
temp1["content"] = data[0]
|
42 |
-
temp2 = {}
|
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 |
-
chat_counter+=1
|
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="center">ํ์ ์คํฌ๋ฆฝํธ</h1>"""
|
107 |
-
description = """์์ ์์ฑ์ ์ํ ์คํฌ๋ฆฝํธ๋ฅผ AI๊ฐ ์๋์ผ๋ก ์์ฑํฉ๋๋ค. ์ฃผ์ ํค์๋๋ ๋ชฉ์ ๋ฑ ํ์ํ ๋ด์ฉ๋ง ๊ฐ๋จํ ์
๋ ฅํ์ธ์. :
|
108 |
-
```
|
109 |
-
User: <utterance>
|
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 = "col_container"):
|
123 |
-
openai_api_key = gr.Textbox(type='password', label="Enter your OpenAI API key here")
|
124 |
-
|
125 |
-
# ์ถ๋ ฅํผ (chatbot)์ ์ฌ์ฉ์ ์
๋ ฅํผ (inputs) ์์ ๋ฐฐ์น
|
126 |
-
chatbot = gr.Chatbot(elem_id='chatbot') # c
|
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( minimum=-0, maximum=1.0, value=1.0, step=0.05, interactive=True, label="Top-p (nucleus sampling)",)
|
134 |
-
temperature = gr.Slider( minimum=-0, maximum=5.0, value=1.0, step=0.1, interactive=True, label="Temperature",)
|
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 |
-
["์ํ ์ค๋ช
:์๋ก ์ถ์๋ 'ํ ๋ฆฌ' ๋ฆฝ๋ฐค์ FDA ์น์ธ, ์ต๊ณ ์ ๋ณด์ต๋ ฅ, ๊ตฌ๋งค์ง์ 1์ "],
|
141 |
-
["๋ธ๋๋ฉ: 'ํ ๋ฆฌ'๋ฆฝ๋ฐค์ 20๋ ์ฌ์ฑ์๊ฒ ์ดํํ ๋ธ๋๋ฉ์ด ํ์ํด"],
|
142 |
-
["๊ด๊ณ : ์ค๋ ๋ถ๋ชจ๋๊ณผ ์น์ง ์ ๋ฌผ์ ๋ฒ์ฑํฌ ๋ณด๋ฆฌ๊ตด๋น๊ฐ ์ต๊ณ ๋๋๋ค."],
|
143 |
-
["์ ๋ณด ๊ณต์ : ๋นํ๋ฏผC ๊ณผ๋ค ๋ณต์ฉ์ ๊ฑด๊ฐ์ ์คํ๋ ค ํด๋กญ๋ค."],
|
144 |
-
["ํ๋ณด: 'OpenAI'๋ '์ฑGPT'์ ๋ง์ถค GPT '์คํ ์ด'๋ฅผ ์คํํ์๋ค."],
|
145 |
-
["์ธ์ฌ: '์ ํ ๋ฒ์ธ'์ ๊ณ ๊ฐ๊ณผ ์์ง์์ ์ํ ์ง์ทจ์ ์ธ 2024๋
์ ๋
์ธ์ฌ"]
|
146 |
-
], inputs=[inputs], fn=predict, outputs=[chatbot, state, chat_counter])
|
147 |
-
|
148 |
-
|
149 |
-
inputs.submit( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
150 |
-
b1.click( predict, [inputs, top_p, temperature, openai_api_key, chat_counter, chatbot, state], [chatbot, state, chat_counter],)
|
151 |
-
b1.click(reset_textbox, [], [inputs])
|
152 |
-
inputs.submit(reset_textbox, [], [inputs])
|
153 |
-
|
154 |
-
#gr.Markdown(description)
|
155 |
-
demo.queue().launch(debug=True)
|
156 |
-
|
|
|
|
|
|
|
|
|
1 |
import requests
|
2 |
+
from bs4 import BeautifulSoup
|
3 |
+
|
4 |
+
def get_url_content(url):
|
5 |
+
response = requests.get(url)
|
6 |
+
if response.status_code == 200:
|
7 |
+
return response.text
|
8 |
+
else:
|
9 |
+
return "URL์์ ์ฝํ
์ธ ๋ฅผ ๊ฐ์ ธ์ค๋ ๋ฐ ์คํจํ์ต๋๋ค."
|
10 |
+
|
11 |
+
def parse_html(html_content):
|
12 |
+
soup = BeautifulSoup(html_content, 'html.parser')
|
13 |
+
# ์ํ๋ HTML ์์๋ฅผ ํ์ฑํ์ฌ ๋ฐํ
|
14 |
+
# ์: soup.find_all('p') ๋ฑ
|
15 |
+
return soup.prettify()
|
16 |
+
|
17 |
+
# Gradio ์ธํฐํ์ด์ค ํจ์
|
18 |
+
def gradio_fetch_and_parse(url):
|
19 |
+
html_content = get_url_content(url)
|
20 |
+
parsed_content = parse_html(html_content)
|
21 |
+
return parsed_content
|
22 |
+
|
23 |
+
# Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
24 |
+
iface = gr.Interface(
|
25 |
+
fn=gradio_fetch_and_parse,
|
26 |
+
inputs=gr.Textbox(label="URL์ ์
๋ ฅํ์ธ์"),
|
27 |
+
outputs=gr.Textbox(label="์นํ์ด์ง ์ฝํ
์ธ ")
|
28 |
+
)
|
29 |
+
|
30 |
+
iface.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|