Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,45 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import
|
| 4 |
|
| 5 |
-
# ๋ชจ๋ธ ๋ก๋ฉ ์ค ํ์
|
| 6 |
-
status_message = "โณ ๋ชจ๋ธ ๋ก๋ฉ ์ค์
๋๋ค... ์ต๋ 5๋ถ ์ ๋ ๊ฑธ๋ฆด ์ ์์ด์."
|
| 7 |
-
|
| 8 |
-
# ๊ธฐ๋ณธ ํ์ดํ๋ผ์ธ์ None์ผ๋ก ์ค์ ํด๋๊ณ , ์๋์์ ๋ก๋ฉ
|
| 9 |
chat_model = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
# ๋๋ต ํจ์
|
| 12 |
def chat_with_model(message, history):
|
| 13 |
-
global chat_model
|
| 14 |
-
if
|
| 15 |
-
return history + [[message, "โ ๏ธ ๋ชจ๋ธ์ด ์์ง ๋ก๋ฉ
|
| 16 |
|
| 17 |
prompt = f"๋ค์ ๋ฌธ์ฅ์ ๋ถ์ํ๊ณ , ๋ฌด๋กํ๊ฑฐ๋ ๊ณต๊ฒฉ์ ์ธ ํํ์ด ์์ผ๋ฉด ์๋ ค์ฃผ๊ณ ๋ ์์ ์๊ฒ ๋ฐ๊ฟ์ค:\n\n{message}"
|
| 18 |
response = chat_model(prompt, max_new_tokens=200)[0]['generated_text']
|
| 19 |
return history + [[message, response]]
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
def
|
| 23 |
-
|
| 24 |
-
time.sleep(1) # ์ ๊น ๊ธฐ๋ค๋ฆฌ๊ฒ ํด์ status ํ์๊ฐ ์ ์ฉ๋๋๋ก
|
| 25 |
-
chat_model = pipeline("text-generation", model="beomi/KoAlpaca-Polyglot-5.8B")
|
| 26 |
-
return "โ
๋ชจ๋ธ์ด ์ค๋น๋์์ต๋๋ค. ์ด์ ๋ฌธ์ฅ์ ์
๋ ฅํด ๋ณด์ธ์!"
|
| 27 |
|
| 28 |
-
#
|
|
|
|
|
|
|
|
|
|
| 29 |
with gr.Blocks() as demo:
|
| 30 |
-
chatbot = gr.Chatbot(
|
| 31 |
-
msg = gr.Textbox(label="๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์", placeholder="์: ๋
|
| 32 |
-
status = gr.Markdown(
|
| 33 |
-
|
| 34 |
-
# ๋ฒํผ ์
๋ ฅ ์ ์ฐ๊ฒฐ
|
| 35 |
-
def respond_and_clear(user_input, history):
|
| 36 |
-
new_history = chat_with_model(user_input, history)
|
| 37 |
-
return "", new_history
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
gr.on_page_load(load_model, outputs=status)
|
| 43 |
|
| 44 |
-
# ์คํ
|
| 45 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
+
import threading
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
chat_model = None
|
| 6 |
+
loading_done = False
|
| 7 |
+
status_text = "โณ ๋ชจ๋ธ ๋ก๋ฉ ์ค์
๋๋ค..."
|
| 8 |
+
|
| 9 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋ชจ๋ธ ๋ก๋ฉ
|
| 10 |
+
def load_model_bg():
|
| 11 |
+
global chat_model, loading_done, status_text
|
| 12 |
+
chat_model = pipeline("text-generation", model="beomi/KoAlpaca-Polyglot-5.8B")
|
| 13 |
+
loading_done = True
|
| 14 |
+
status_text = "โ
๋ชจ๋ธ ๋ก๋ฉ ์๋ฃ! ๋ฌธ์ฅ์ ์
๋ ฅํด๋ณด์ธ์."
|
| 15 |
|
| 16 |
+
# ๋๋ต ํจ์
|
| 17 |
def chat_with_model(message, history):
|
| 18 |
+
global chat_model, loading_done
|
| 19 |
+
if not loading_done:
|
| 20 |
+
return history + [[message, "โ ๏ธ ๋ชจ๋ธ์ด ์์ง ๋ก๋ฉ ์ค์
๋๋ค. ์ ์๋ง ๊ธฐ๋ค๋ ค ์ฃผ์ธ์."]]
|
| 21 |
|
| 22 |
prompt = f"๋ค์ ๋ฌธ์ฅ์ ๋ถ์ํ๊ณ , ๋ฌด๋กํ๊ฑฐ๋ ๊ณต๊ฒฉ์ ์ธ ํํ์ด ์์ผ๋ฉด ์๋ ค์ฃผ๊ณ ๋ ์์ ์๊ฒ ๋ฐ๊ฟ์ค:\n\n{message}"
|
| 23 |
response = chat_model(prompt, max_new_tokens=200)[0]['generated_text']
|
| 24 |
return history + [[message, response]]
|
| 25 |
|
| 26 |
+
# ์ํ ํ
์คํธ ๋ฐํ ํจ์ (๋งค๋ฒ ์๋ก ์ฝ์ด์ด)
|
| 27 |
+
def get_status():
|
| 28 |
+
return status_text
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# ๋ฐฑ๊ทธ๋ผ์ด๋์์ ๋ชจ๋ธ ๋ก๋ฉ ์์
|
| 31 |
+
threading.Thread(target=load_model_bg).start()
|
| 32 |
+
|
| 33 |
+
# Gradio ์ฑ
|
| 34 |
with gr.Blocks() as demo:
|
| 35 |
+
chatbot = gr.Chatbot()
|
| 36 |
+
msg = gr.Textbox(label="๋ฌธ์ฅ์ ์
๋ ฅํ์ธ์", placeholder="์: ๋ ์ ๋ง ์ ๊ทธ๋ ๊ฒ ๋งํด?")
|
| 37 |
+
status = gr.Markdown(get_status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
+
def respond_and_clear(user_input, history):
|
| 40 |
+
updated_history = chat_with_model(user_input, history)
|
| 41 |
+
return "", updated_history, get_status()
|
| 42 |
|
| 43 |
+
msg.submit(respond_and_clear, [msg, chatbot], [msg, chatbot, status])
|
|
|
|
| 44 |
|
|
|
|
| 45 |
demo.launch()
|