Spaces:
Sleeping
Sleeping
import os | |
from huggingface_hub import InferenceClient | |
import gradio as gr | |
from transformers import GPT2Tokenizer | |
client = InferenceClient("meta-llama/Meta-Llama-3-70B-Instruct", token=os.getenv('HF_API_KEY')) # ์์ | |
tokenizer = GPT2Tokenizer.from_pretrained("gpt2") | |
# ์์คํ ์ธ์คํธ๋ญ์ ์ ์ค์ ํ์ง๋ง ์ฌ์ฉ์์๊ฒ ๋ ธ์ถํ์ง ์์ต๋๋ค. | |
system_instruction = """ | |
๋์ ์ด๋ฆ์ 'AIQ Codepilot'์ด๋ค. ๋๋ Huggingface์์ gradio ์ฝ๋ฉ์ ํนํ๋ ์ ๋ฌธ AI ์ด์์คํดํธ ์ญํ ์ด๋ค. | |
๋๋ ๋ชจ๋ ๋ต๋ณ์ ํ๊ธ๋ก ํ๊ณ , code ์ถ๋ ฅ์ markdown ํ์์ผ๋ก ์ถ๋ ฅํ๋ผ. | |
๋ชจ๋ ์ฝ๋๋ ๋ณ๋ ์์ฒญ์ด ์๋ํ, ๋ฐ๋์ "gradio"๋ฅผ ์ ์ฉํ ์ฝ๋๋ก ์ถ๋ ฅํ๋ผ. | |
๋ํ ๋ด์ฉ์ ๊ธฐ์ตํ๊ณ , ์ฝ๋ ๊ธธ์ด์ ์ ํ์ ๋์ง ๋ง๊ณ ์ต๋ํ ์์ธํ๊ฒ ์์ธํ๊ฒ ํ๊ธ๋ก ๋ต๋ณ์ ์ด์ด๊ฐ๋ผ. | |
Huggingface์ ๋ชจ๋ธ, ๋ฐ์ดํฐ์ , spaces์ ๋ํด ํนํ๋ ์ง์๊ณผ ์ ๋ณด ๊ทธ๋ฆฌ๊ณ full text ๊ฒ์์ ์ง์ํ๋ผ. | |
๋ชจ๋ธ๋ง๊ณผ ๋ฐ์ดํฐ์ ์ฌ์ฉ ๋ฐฉ๋ฒ ๋ฐ ์์๋ฅผ ์์ธํ๊ฒ ๋ค์ด๋ผ. | |
Huggingface์์ space์ ๋ํ ๋ณต์ , ์๋ฒ ๋ฉ, deploy, setting ๋ฑ์ ๋ํ ์ธ๋ถ์ ์ธ ์ค๋ช ์ ์ง์ํ๋ผ. | |
์ด GPTs๋ฅผ ์ด์ฉํ๋ ์ ์ ๋ค์ ์ฝ๋ฉ์ ๋ชจ๋ฅด๋ ์ด๋ณด์๋ผ๋ ์ ์ ํ์ ์น์ ํ๊ฒ ์ฝ๋์ ๋ํด ์ค๋ช ์ ํ์ฌ์ผ ํ๋ค. | |
ํนํ ์ฝ๋๋ฅผ ์์ ํ ๋๋ ๋ถ๋ถ์ ์ธ ๋ถ๋ถ๋ง ์ถ๋ ฅํ์ง ๋ง๊ณ , ์ ์ฒด ์ฝ๋๋ฅผ ์ถ๋ ฅํ๋ฉฐ '์์ '์ด ๋ ๋ถ๋ถ์ Before์ After๋ก ๊ตฌ๋ถํ์ฌ ๋ถ๋ช ํ ์๋ ค์ฃผ๋๋ก ํ๋ผ. | |
์์ฑ๋ ์ ์ฒด ์ฝ๋๋ฅผ ์ถ๋ ฅํ๊ณ ๋์, huggingface์์ ์ด๋ป๊ฒ space๋ฅผ ๋ง๋ค๊ณ app.py ํ์ผ ์ด๋ฆ์ผ๋ก ๋ณต์ฌํ ์ฝ๋๋ฅผ ๋ถ์ฌ๋ฃ๊ณ ์คํํ๋์ง ๋ฑ์ ๊ณผ์ ์ ๊ผญ ์๋ ค์ค๊ฒ. | |
๋ํ ๋ฐ๋์ "requirements.txt"์ ์ด๋ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ํฌํจ์์ผ์ผ ํ๋์ง ๊ทธ ๋ฐฉ๋ฒ๊ณผ ๋ฆฌ์คํธ๋ฅผ ์์ธํ ์๋ ค์ค๊ฒ. | |
huggingface์์ ๋์๋ ์๋น์ค๋ฅผ ๋ง๋ค๊ฒ์ด๊ธฐ์ ๋ก์ปฌ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ค์นํ๋ ๋ฐฉ๋ฒ์ ์ค๋ช ํ์ง ๋ง์๋ผ. | |
""" | |
# ๋์ ํ ํฐ ์ฌ์ฉ๋์ ์ถ์ ํ๋ ์ ์ญ ๋ณ์ | |
total_tokens_used = 0 | |
def format_prompt(message, history): | |
# ์์คํ ์ธ์คํธ๋ญ์ ์ ์ค์ ํ๊ณ , ํ๋ฆฌํฝ์ค๋ฅผ ์ถ๊ฐํฉ๋๋ค. | |
prefix = "๋ฐ๋์ ๋ชจ๋ ๋ต๋ณ๊ณผ ๋ฉ์์ง๋ 'ํ๊ธ'(ํ๊ตญ์ด)๋ก ์ถ๋ ฅํ๋ผ:" | |
prompt = "<s>[SYSTEM] {} [/SYSTEM]".format(system_instruction + prefix) | |
for user_prompt, bot_response in history: | |
prompt += f"[INST] {user_prompt} [/INST]{bot_response}</s> " | |
prompt += f"[INST] {message} [/INST]" | |
return prompt | |
def generate(prompt, history=[], temperature=0.1, max_new_tokens=2000, top_p=0.95, repetition_penalty=1.0): | |
global total_tokens_used | |
input_tokens = len(tokenizer.encode(prompt)) | |
total_tokens_used += input_tokens | |
available_tokens = 120000 - total_tokens_used | |
if available_tokens <= 0: | |
yield f"Error: ์ ๋ ฅ์ด ์ต๋ ํ์ฉ ํ ํฐ ์๋ฅผ ์ด๊ณผํฉ๋๋ค. Total tokens used: {total_tokens_used}" | |
return | |
formatted_prompt = format_prompt(prompt, history) | |
output_accumulated = "" | |
try: | |
stream = client.text_generation( | |
formatted_prompt, | |
temperature=temperature, | |
max_new_tokens=min(max_new_tokens, available_tokens), | |
top_p=top_p, | |
repetition_penalty=repetition_penalty, | |
do_sample=True, | |
seed=42, | |
stream=True | |
) | |
for response in stream: | |
output_part = response['generated_text'] if 'generated_text' in response else str(response) | |
output_accumulated += output_part | |
yield output_accumulated + f"\n\n---\nTotal tokens used: {total_tokens_used}" | |
except Exception as e: | |
yield f"Error: {str(e)}\nTotal tokens used: {total_tokens_used}" | |
mychatbot = gr.Chatbot( | |
avatar_images=["./user.png", "./botm.png"], | |
bubble_full_width=False, | |
show_label=False, | |
show_copy_button=True, | |
likeable=True, | |
) | |
examples = [ | |
["์ข์ ์์ ๋ฅผ ์๋ ค์ค.", []], # history ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ ๊ณต | |
["๋ฐ๋์ ํ๊ธ๋ก ๋ต๋ณํ ๊ฒ.", []], # history ๊ฐ์ ๋น ๋ฆฌ์คํธ๋ก ์ ๊ณต | |
["๊ณ์ ์ด์ด์ ์ถ๋ ฅ", []], | |
["requirements.txt ์ถ๋ ฅ", []], | |
["์ ์ฒด ์ฝ๋๋ฅผ ๋ค์ ์ถ๋ ฅ", []], | |
["์ฝ๋ ์ค๋ฅ๋ฅผ ํ์ธํ๊ณ ์์ธํ ์ค๋ช ํด์ค.", []], | |
["Huggingface์ Gradio๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋ํด ๋ฌผ์ด๋ณด์ธ์.", []] | |
] | |
css = """ | |
h1 { | |
font-size: 14px; /* ์ ๋ชฉ ๊ธ๊ผด ํฌ๊ธฐ๋ฅผ ์๊ฒ ์ค์ */ | |
} | |
footer { | |
visibility: hidden; | |
} | |
""" | |
def update_chat(input_text): | |
update_chat.response = input_text | |
update_chat.response = "" | |
demo = gr.Interface( | |
generate, | |
[ | |
gr.Interface.Textbox(default="์์", label="์ง๋ฌธ ์ ๋ ฅ"), | |
gr.Interface.Textbox(update_chat, label="๋ํ ์ ๋ฐ์ดํธ") | |
], | |
"chat", | |
title="AIQ ์ฝ๋ํ์ผ๋ฟ: L3", | |
examples=examples, | |
css=css | |
) | |
demo.launch(share=True, debug=True) | |