Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,8 @@
|
|
1 |
import gradio as gr
|
2 |
-
from huggingface_hub import InferenceClient
|
3 |
-
import os
|
4 |
import random
|
5 |
-
import
|
6 |
-
|
7 |
-
# 로깅 설정
|
8 |
-
logging.basicConfig(filename='language_model_playground.log', level=logging.DEBUG,
|
9 |
-
format='%(asctime)s - %(levelname)s - %(message)s')
|
10 |
|
11 |
-
# 모델 목록
|
12 |
MODELS = {
|
13 |
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
14 |
"DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
|
@@ -21,62 +15,38 @@ MODELS = {
|
|
21 |
"Aya-23-35B": "CohereForAI/aya-23-35B"
|
22 |
}
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
if not hf_token:
|
27 |
-
raise ValueError("HF_TOKEN 환경 변수가 설정되지 않았습니다.")
|
28 |
|
29 |
-
def
|
30 |
-
client =
|
31 |
-
|
32 |
random_seed = random.randint(0, 1000000)
|
33 |
-
|
34 |
-
|
35 |
-
response = client.text_generation(
|
36 |
-
combined_prompt,
|
37 |
-
max_new_tokens=max_tokens,
|
38 |
-
temperature=temperature,
|
39 |
-
top_p=top_p,
|
40 |
-
seed=random_seed
|
41 |
-
)
|
42 |
-
return response
|
43 |
-
except Exception as e:
|
44 |
-
logging.error(f"HuggingFace API 호출 중 오류 발생: {str(e)}")
|
45 |
-
return f"응답 생성 중 오류 발생: {str(e)}. 나중에 다시 시도해 주세요."
|
46 |
|
47 |
-
def
|
48 |
-
|
49 |
-
response_html = f"""
|
50 |
-
<h3>생성된 응답:</h3>
|
51 |
-
<div style='max-height: 500px; overflow-y: auto; white-space: pre-wrap; word-wrap: break-word;'>
|
52 |
-
{response}
|
53 |
-
</div>
|
54 |
-
"""
|
55 |
-
return response_html
|
56 |
|
57 |
-
|
58 |
-
with gr.Blocks() as demo:
|
59 |
-
gr.Markdown("## 언어 모델 프롬프트 플레이그라운드")
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
)
|
80 |
|
81 |
-
|
82 |
-
demo.launch(share=True)
|
|
|
1 |
import gradio as gr
|
|
|
|
|
2 |
import random
|
3 |
+
import os
|
4 |
+
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
5 |
|
|
|
6 |
MODELS = {
|
7 |
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
8 |
"DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
|
|
|
15 |
"Aya-23-35B": "CohereForAI/aya-23-35B"
|
16 |
}
|
17 |
|
18 |
+
def create_client(model_name):
|
19 |
+
return InferenceClient(model_name, token=os.getenv("HF_TOKEN"))
|
|
|
|
|
20 |
|
21 |
+
def call_api(model, content, system_message, max_tokens, temperature, top_p):
|
22 |
+
client = create_client(MODELS[model])
|
23 |
+
messages = [{"role": "system", "content": system_message}, {"role": "user", "content": content}]
|
24 |
random_seed = random.randint(0, 1000000)
|
25 |
+
response = client.chat_completion(messages=messages, max_tokens=max_tokens, temperature=temperature, top_p=top_p, seed=random_seed)
|
26 |
+
return response.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
def generate_text(model, user_message, system_message, max_tokens, temperature, top_p):
|
29 |
+
return call_api(model, user_message, system_message, max_tokens, temperature, top_p)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
title = "AI 텍스트 생성기"
|
|
|
|
|
32 |
|
33 |
+
with gr.Blocks() as demo:
|
34 |
+
gr.Markdown(f"# {title}")
|
35 |
+
|
36 |
+
model = gr.Radio(choices=list(MODELS.keys()), label="언어 모델 선택", value="Zephyr 7B Beta")
|
37 |
+
user_message = gr.Textbox(label="사용자 메시지", lines=5)
|
38 |
+
system_message = gr.Textbox(label="시스템 메시지 (프롬프트)", lines=10)
|
39 |
+
|
40 |
+
with gr.Accordion("고급 설정", open=False):
|
41 |
+
max_tokens = gr.Slider(label="Max Tokens", minimum=0, maximum=4000, value=500, step=100)
|
42 |
+
temperature = gr.Slider(label="Temperature", minimum=0.1, maximum=1.0, value=0.75, step=0.05)
|
43 |
+
top_p = gr.Slider(label="Top P", minimum=0.1, maximum=1.0, value=0.95, step=0.05)
|
44 |
+
|
45 |
+
generate_btn = gr.Button("텍스트 생성하기")
|
46 |
+
output = gr.Textbox(label="생성된 텍스트", lines=10)
|
47 |
+
|
48 |
+
generate_btn.click(fn=generate_text,
|
49 |
+
inputs=[model, user_message, system_message, max_tokens, temperature, top_p],
|
50 |
+
outputs=[output])
|
|
|
51 |
|
52 |
+
demo.launch()
|
|