Spaces:
Running
on
Zero
Running
on
Zero
import re | |
import threading | |
import gradio as gr | |
import spaces | |
import transformers | |
from transformers import pipeline | |
# λͺ¨λΈκ³Ό ν ν¬λμ΄μ λ‘λ© | |
model_name = "CohereForAI/c4ai-command-r7b-arabic-02-2025" | |
if gr.NO_RELOAD: | |
pipe = pipeline( | |
"text-generation", | |
model=model_name, | |
device_map="auto", | |
torch_dtype="auto", | |
) | |
# μ΅μ’ λ΅λ³μ κ°μ§νκΈ° μν λ§μ»€ | |
ANSWER_MARKER = "**λ΅λ³**" | |
# λ¨κ³λ³ μΆλ‘ μ μμνλ λ¬Έμ₯λ€ | |
rethink_prepends = [ | |
"μ, μ΄μ λ€μμ νμ ν΄μΌ ν©λλ€ ", | |
"μ μκ°μλ ", | |
"μ μλ§μ, μ μκ°μλ ", | |
"λ€μ μ¬νμ΄ λ§λμ§ νμΈν΄ λ³΄κ² μ΅λλ€ ", | |
"λν κΈ°μ΅ν΄μΌ ν κ²μ ", | |
"λ λ€λ₯Έ μ£Όλͺ©ν μ μ ", | |
"κ·Έλ¦¬κ³ μ λ λ€μκ³Ό κ°μ μ¬μ€λ κΈ°μ΅ν©λλ€ ", | |
"μ΄μ μΆ©λΆν μ΄ν΄νλ€κ³ μκ°ν©λλ€ ", | |
"μ§κΈκΉμ§μ μ 보λ₯Ό λ°νμΌλ‘, μλ μ§λ¬Έμ μ¬μ©λ μΈμ΄λ‘ λ΅λ³νκ² μ΅λλ€:" | |
"\n{question}\n" | |
f"\n{ANSWER_MARKER}\n", | |
] | |
# μμ νμ λ¬Έμ ν΄κ²°μ μν μ€μ | |
latex_delimiters = [ | |
{"left": "$$", "right": "$$", "display": True}, | |
{"left": "$", "right": "$", "display": False}, | |
] | |
def reformat_math(text): | |
"""Gradio ꡬ문(Katex)μ μ¬μ©νλλ‘ MathJax κ΅¬λΆ κΈ°νΈ μμ . | |
μ΄κ²μ Gradioμμ μν 곡μμ νμνκΈ° μν μμ ν΄κ²°μ± μ λλ€. νμ¬λ‘μλ | |
λ€λ₯Έ latex_delimitersλ₯Ό μ¬μ©νμ¬ μμλλ‘ μλνκ² νλ λ°©λ²μ μ°Ύμ§ λͺ»νμ΅λλ€... | |
""" | |
text = re.sub(r"\\\[\s*(.*?)\s*\\\]", r"$$\1$$", text, flags=re.DOTALL) | |
text = re.sub(r"\\\(\s*(.*?)\s*\\\)", r"$\1$", text, flags=re.DOTALL) | |
return text | |
def user_input(message, history: list): | |
"""μ¬μ©μ μ λ ₯μ νμ€ν 리μ μΆκ°νκ³ μ λ ₯ ν μ€νΈ μμ λΉμ°κΈ°""" | |
return "", history + [ | |
gr.ChatMessage(role="user", content=message.replace(ANSWER_MARKER, "")) | |
] | |
def rebuild_messages(history: list): | |
"""μ€κ° μκ° κ³Όμ μμ΄ λͺ¨λΈμ΄ μ¬μ©ν νμ€ν 리μμ λ©μμ§ μ¬κ΅¬μ±""" | |
messages = [] | |
for h in history: | |
if isinstance(h, dict) and not h.get("metadata", {}).get("title", False): | |
messages.append(h) | |
elif ( | |
isinstance(h, gr.ChatMessage) | |
and h.metadata.get("title") | |
and isinstance(h.content, str) | |
): | |
messages.append({"role": h.role, "content": h.content}) | |
return messages | |
def bot( | |
history: list, | |
max_num_tokens: int, | |
final_num_tokens: int, | |
do_sample: bool, | |
temperature: float, | |
): | |
"""λͺ¨λΈμ΄ μ§λ¬Έμ λ΅λ³νλλ‘ νκΈ°""" | |
# λμ€μ μ€λ λμμ ν ν°μ μ€νΈλ¦ΌμΌλ‘ κ°μ Έμ€κΈ° μν¨ | |
streamer = transformers.TextIteratorStreamer( | |
pipe.tokenizer, # pyright: ignore | |
skip_special_tokens=True, | |
skip_prompt=True, | |
) | |
# νμν κ²½μ° μΆλ‘ μ μ§λ¬Έμ λ€μ μ½μ νκΈ° μν¨ | |
question = history[-1]["content"] | |
# 보쑰μ λ©μμ§ μ€λΉ | |
history.append( | |
gr.ChatMessage( | |
role="assistant", | |
content=str(""), | |
metadata={"title": "π§ μκ° μ€...", "status": "pending"}, | |
) | |
) | |
# νμ¬ μ±ν μ νμλ μΆλ‘ κ³Όμ | |
messages = rebuild_messages(history) | |
for i, prepend in enumerate(rethink_prepends): | |
if i > 0: | |
messages[-1]["content"] += "\n\n" | |
messages[-1]["content"] += prepend.format(question=question) | |
num_tokens = int( | |
max_num_tokens if ANSWER_MARKER not in prepend else final_num_tokens | |
) | |
t = threading.Thread( | |
target=pipe, | |
args=(messages,), | |
kwargs=dict( | |
max_new_tokens=num_tokens, | |
streamer=streamer, | |
do_sample=do_sample, | |
temperature=temperature, | |
), | |
) | |
t.start() | |
# μ λ΄μ©μΌλ‘ νμ€ν 리 μ¬κ΅¬μ± | |
history[-1].content += prepend.format(question=question) | |
if ANSWER_MARKER in prepend: | |
history[-1].metadata = {"title": "π μ¬κ³ κ³Όμ ", "status": "done"} | |
# μκ° μ’ λ£, μ΄μ λ΅λ³μ λλ€ (μ€κ° λ¨κ³μ λν λ©νλ°μ΄ν° μμ) | |
history.append(gr.ChatMessage(role="assistant", content="")) | |
for token in streamer: | |
history[-1].content += token | |
history[-1].content = reformat_math(history[-1].content) | |
yield history | |
t.join() | |
yield history | |
with gr.Blocks(fill_height=True, title="λͺ¨λ LLM λͺ¨λΈμ μΆλ‘ λ₯λ ₯ λΆμ¬νκΈ°") as demo: | |
with gr.Row(scale=1): | |
with gr.Column(scale=5): | |
chatbot = gr.Chatbot( | |
scale=1, | |
type="messages", | |
latex_delimiters=latex_delimiters, | |
) | |
msg = gr.Textbox( | |
submit_btn=True, | |
label="", | |
show_label=False, | |
placeholder="μ¬κΈ°μ μ§λ¬Έμ μ λ ₯νμΈμ.", | |
autofocus=True, | |
) | |
with gr.Column(scale=1): | |
gr.Markdown("""## λ§€κ°λ³μ μ‘°μ """) | |
num_tokens = gr.Slider( | |
50, | |
4000, | |
2000, | |
step=1, | |
label="μΆλ‘ λ¨κ³λΉ μ΅λ ν ν° μ", | |
interactive=True, | |
) | |
final_num_tokens = gr.Slider( | |
50, | |
4000, | |
2000, | |
step=1, | |
label="μ΅μ’ λ΅λ³μ μ΅λ ν ν° μ", | |
interactive=True, | |
) | |
do_sample = gr.Checkbox(True, label="μνλ§ μ¬μ©") | |
temperature = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="μ¨λ") | |
# μ¬μ©μκ° λ©μμ§λ₯Ό μ μΆνλ©΄ λ΄μ΄ μλ΅ν©λλ€ | |
msg.submit( | |
user_input, | |
[msg, chatbot], # μ λ ₯ | |
[msg, chatbot], # μΆλ ₯ | |
).then( | |
bot, | |
[ | |
chatbot, | |
num_tokens, | |
final_num_tokens, | |
do_sample, | |
temperature, | |
], # μ€μ λ‘λ "history" μ λ ₯ | |
chatbot, # μΆλ ₯μμ μ νμ€ν 리 μ μ₯ | |
) | |
if __name__ == "__main__": | |
demo.queue().launch() |