Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
from huggingface_hub import InferenceClient
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
|
|
|
| 5 |
|
| 6 |
# ์์คํ
์ธ์คํธ๋ญ์
์ ์ค์ ํ์ง๋ง ์ฌ์ฉ์์๊ฒ ๋
ธ์ถํ์ง ์์ต๋๋ค.
|
| 7 |
system_instruction = """
|
|
@@ -74,39 +76,29 @@ const feedbackLink = `https://promptspellsmith.com/feedback`
|
|
| 74 |
"""
|
| 75 |
|
| 76 |
def format_prompt(message, history):
|
| 77 |
-
prompt = "<s>"
|
| 78 |
-
# ์์คํ
์ธ์คํธ๋ญ์
์ ํ๋กฌํํธ์ ํฌํจํ์ง๋ง, ์ด๋ฅผ ์ฌ์ฉ์์๊ฒ๋ ํ์ํ์ง ์์ต๋๋ค.
|
| 79 |
-
prompt += f"[SYSTEM] {system_instruction} [/SYSTEM]"
|
| 80 |
for user_prompt, bot_response in history:
|
| 81 |
-
prompt += f"[INST] {user_prompt} [/INST]"
|
| 82 |
-
prompt += f" {bot_response}</s> "
|
| 83 |
prompt += f"[INST] {message} [/INST]"
|
| 84 |
return prompt
|
| 85 |
|
| 86 |
def generate(prompt, history, temperature=0.1, max_new_tokens=25000, top_p=0.95, repetition_penalty=1.0):
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
generate_kwargs = dict(
|
| 93 |
-
temperature=temperature,
|
| 94 |
-
max_new_tokens=max_new_tokens,
|
| 95 |
-
top_p=top_p,
|
| 96 |
-
repetition_penalty=repetition_penalty,
|
| 97 |
-
do_sample=True,
|
| 98 |
-
seed=42,
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
formatted_prompt = format_prompt(prompt, history)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
|
|
|
|
|
|
| 110 |
|
| 111 |
mychatbot = gr.Chatbot(
|
| 112 |
avatar_images=["./user.png", "./botm.png"],
|
|
@@ -116,23 +108,20 @@ mychatbot = gr.Chatbot(
|
|
| 116 |
likeable=True,
|
| 117 |
)
|
| 118 |
|
| 119 |
-
|
| 120 |
examples = [
|
| 121 |
["์ข์ ์์ ๋ฅผ ์๋ ค์ค."],
|
| 122 |
["requirements.txt ์ถ๋ ฅ"],
|
| 123 |
["์ ์ฒด ์ฝ๋๋ฅผ ๋ค์ ์ถ๋ ฅ"],
|
| 124 |
["์ฝ๋ ์ค๋ฅ๋ฅผ ํ์ธํ๊ณ ์์ธํ ์ค๋ช
ํด์ค."],
|
| 125 |
-
["Huggingface์ Gradio๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๋ฌผ์ด๋ณด์ธ์."]
|
| 126 |
]
|
| 127 |
|
| 128 |
-
|
| 129 |
-
demo = gr.ChatInterface(
|
| 130 |
fn=generate,
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
examples=examples
|
| 136 |
)
|
| 137 |
|
| 138 |
-
demo.
|
|
|
|
| 1 |
from huggingface_hub import InferenceClient
|
| 2 |
import gradio as gr
|
| 3 |
+
from transformers import GPT2Tokenizer
|
| 4 |
|
| 5 |
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
|
| 6 |
+
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
|
| 7 |
|
| 8 |
# ์์คํ
์ธ์คํธ๋ญ์
์ ์ค์ ํ์ง๋ง ์ฌ์ฉ์์๊ฒ ๋
ธ์ถํ์ง ์์ต๋๋ค.
|
| 9 |
system_instruction = """
|
|
|
|
| 76 |
"""
|
| 77 |
|
| 78 |
def format_prompt(message, history):
|
| 79 |
+
prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction)
|
|
|
|
|
|
|
| 80 |
for user_prompt, bot_response in history:
|
| 81 |
+
prompt += f"[INST] {user_prompt} [/INST]{bot_response}</s> "
|
|
|
|
| 82 |
prompt += f"[INST] {message} [/INST]"
|
| 83 |
return prompt
|
| 84 |
|
| 85 |
def generate(prompt, history, temperature=0.1, max_new_tokens=25000, top_p=0.95, repetition_penalty=1.0):
|
| 86 |
+
# ํ ํฐ ์ ๊ณ์ฐ
|
| 87 |
+
input_tokens = len(tokenizer.encode(prompt))
|
| 88 |
+
available_tokens = 32768 - input_tokens # ์ต๋ ํ์ฉ ํ ํฐ ์์์ ์
๋ ฅ ํ ํฐ ์๋ฅผ ๋บ ๊ฐ
|
| 89 |
+
max_new_tokens = min(max_new_tokens, available_tokens) # ์ฌ์ฉ ๊ฐ๋ฅํ ์ต๋ ํ ํฐ ์ ์กฐ์
|
| 90 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
formatted_prompt = format_prompt(prompt, history)
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
stream = client.text_generation(formatted_prompt, temperature=temperature, max_new_tokens=max_new_tokens,
|
| 95 |
+
top_p=top_p, repetition_penalty=repetition_penalty, do_sample=True, seed=42, stream=True)
|
| 96 |
+
output = ""
|
| 97 |
+
for response in stream:
|
| 98 |
+
output += response.token.text
|
| 99 |
+
yield output
|
| 100 |
+
except Exception as e:
|
| 101 |
+
yield f"Error: {str(e)}"
|
| 102 |
|
| 103 |
mychatbot = gr.Chatbot(
|
| 104 |
avatar_images=["./user.png", "./botm.png"],
|
|
|
|
| 108 |
likeable=True,
|
| 109 |
)
|
| 110 |
|
|
|
|
| 111 |
examples = [
|
| 112 |
["์ข์ ์์ ๋ฅผ ์๋ ค์ค."],
|
| 113 |
["requirements.txt ์ถ๋ ฅ"],
|
| 114 |
["์ ์ฒด ์ฝ๋๋ฅผ ๋ค์ ์ถ๋ ฅ"],
|
| 115 |
["์ฝ๋ ์ค๋ฅ๋ฅผ ํ์ธํ๊ณ ์์ธํ ์ค๋ช
ํด์ค."],
|
| 116 |
+
["Huggingface์ Gradio๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๋ฌผ์ด๋ณด์ธ์."]
|
| 117 |
]
|
| 118 |
|
| 119 |
+
demo = gr.Interface(
|
|
|
|
| 120 |
fn=generate,
|
| 121 |
+
inputs=gr.Textbox(placeholder="์ฌ๊ธฐ์ ์ง๋ฌธ์ ์
๋ ฅํ์ธ์...", lines=2),
|
| 122 |
+
outputs=gr.Markdown(),
|
| 123 |
+
examples=examples,
|
| 124 |
+
title="AIQ ์ฝ๋ํ์ผ๋ฟ: OpenLLM v1.12"
|
|
|
|
| 125 |
)
|
| 126 |
|
| 127 |
+
demo.launch(show_api=False)
|