Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -18,25 +18,6 @@ models = {
|
|
18 |
def get_client(model):
|
19 |
return InferenceClient(model=model, token=hf_token)
|
20 |
|
21 |
-
# 히스토리 관리 클래스
|
22 |
-
class PromptHistory:
|
23 |
-
def __init__(self):
|
24 |
-
self.history = []
|
25 |
-
|
26 |
-
def add_entry(self, prompt, response, model, system_message):
|
27 |
-
self.history.append({
|
28 |
-
"prompt": prompt,
|
29 |
-
"response": response,
|
30 |
-
"model": model,
|
31 |
-
"system_message": system_message
|
32 |
-
})
|
33 |
-
|
34 |
-
def get_history(self):
|
35 |
-
return self.history
|
36 |
-
|
37 |
-
# 히스토리 인스턴스 생성
|
38 |
-
prompt_history = PromptHistory()
|
39 |
-
|
40 |
# 응답 생성 함수
|
41 |
def respond(prompt, system_message, max_tokens, temperature, top_p, selected_model):
|
42 |
stop_event.clear()
|
@@ -50,7 +31,6 @@ def respond(prompt, system_message, max_tokens, temperature, top_p, selected_mod
|
|
50 |
|
51 |
try:
|
52 |
response = ""
|
53 |
-
total_tokens_used = 0 # 사용된 토큰 수 추적
|
54 |
|
55 |
# 모델에서 응답을 스트리밍
|
56 |
for chunk in client.text_generation(
|
@@ -64,31 +44,16 @@ def respond(prompt, system_message, max_tokens, temperature, top_p, selected_mod
|
|
64 |
break
|
65 |
if chunk:
|
66 |
response += chunk
|
67 |
-
total_tokens_used += len(chunk.split()) # 청크당 사용된 토큰 수 추산
|
68 |
|
69 |
-
#
|
70 |
-
prompt_history.add_entry(prompt, response, selected_model, system_message)
|
71 |
-
|
72 |
-
return response
|
73 |
|
74 |
except Exception as e:
|
75 |
-
return f"오류 발생: {str(e)}"
|
76 |
-
|
77 |
-
# 대화 계속 함수
|
78 |
-
def continue_writing(chatbot, system_message, max_tokens, temperature, top_p, selected_model):
|
79 |
-
last_message = chatbot[-1][1] if chatbot else ""
|
80 |
-
return respond(last_message, system_message, max_tokens, temperature, top_p, selected_model)
|
81 |
|
82 |
# 응답 중단 함수
|
83 |
def stop_generation():
|
84 |
stop_event.set()
|
85 |
|
86 |
-
# 히스토리 확인 함수
|
87 |
-
def view_history():
|
88 |
-
history = prompt_history.get_history()
|
89 |
-
history_str = "\n\n".join([f"모델: {entry['model']}\n프롬프트: {entry['prompt']}\n응답: {entry['response']}\n시스템 메시지: {entry['system_message']}" for entry in history])
|
90 |
-
return history_str if history_str else "히스토리가 없습니다."
|
91 |
-
|
92 |
# Gradio UI 구성
|
93 |
with gr.Blocks() as demo:
|
94 |
gr.Markdown("# 프롬프트 최적화 Playground")
|
@@ -124,20 +89,14 @@ with gr.Blocks() as demo:
|
|
124 |
|
125 |
with gr.Row():
|
126 |
send = gr.Button("전송")
|
127 |
-
continue_btn = gr.Button("계속 작성")
|
128 |
stop = gr.Button("🛑 생성 중단")
|
129 |
clear = gr.Button("🗑️ 대화 내역 지우기")
|
130 |
-
view_history_btn = gr.Button("🗂️ 히스토리 보기")
|
131 |
|
132 |
# Event handlers
|
133 |
send.click(respond, inputs=[prompt, system_message, max_tokens, temperature, top_p, model], outputs=[chatbot])
|
134 |
prompt.submit(respond, inputs=[prompt, system_message, max_tokens, temperature, top_p, model], outputs=[chatbot])
|
135 |
-
continue_btn.click(continue_writing,
|
136 |
-
inputs=[chatbot, system_message, max_tokens, temperature, top_p, model],
|
137 |
-
outputs=[chatbot])
|
138 |
stop.click(stop_generation)
|
139 |
clear.click(lambda: None, outputs=[chatbot])
|
140 |
-
view_history_btn.click(view_history, outputs=[gr.Textbox(label="히스토리")])
|
141 |
|
142 |
# UI 실행
|
143 |
demo.launch()
|
|
|
18 |
def get_client(model):
|
19 |
return InferenceClient(model=model, token=hf_token)
|
20 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
# 응답 생성 함수
|
22 |
def respond(prompt, system_message, max_tokens, temperature, top_p, selected_model):
|
23 |
stop_event.clear()
|
|
|
31 |
|
32 |
try:
|
33 |
response = ""
|
|
|
34 |
|
35 |
# 모델에서 응답을 스트리밍
|
36 |
for chunk in client.text_generation(
|
|
|
44 |
break
|
45 |
if chunk:
|
46 |
response += chunk
|
|
|
47 |
|
48 |
+
return [(prompt, response)] # 대화창에 프롬프트와 응답을 표시
|
|
|
|
|
|
|
49 |
|
50 |
except Exception as e:
|
51 |
+
return [(prompt, f"오류 발생: {str(e)}")]
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
# 응답 중단 함수
|
54 |
def stop_generation():
|
55 |
stop_event.set()
|
56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
# Gradio UI 구성
|
58 |
with gr.Blocks() as demo:
|
59 |
gr.Markdown("# 프롬프트 최적화 Playground")
|
|
|
89 |
|
90 |
with gr.Row():
|
91 |
send = gr.Button("전송")
|
|
|
92 |
stop = gr.Button("🛑 생성 중단")
|
93 |
clear = gr.Button("🗑️ 대화 내역 지우기")
|
|
|
94 |
|
95 |
# Event handlers
|
96 |
send.click(respond, inputs=[prompt, system_message, max_tokens, temperature, top_p, model], outputs=[chatbot])
|
97 |
prompt.submit(respond, inputs=[prompt, system_message, max_tokens, temperature, top_p, model], outputs=[chatbot])
|
|
|
|
|
|
|
98 |
stop.click(stop_generation)
|
99 |
clear.click(lambda: None, outputs=[chatbot])
|
|
|
100 |
|
101 |
# UI 실행
|
102 |
demo.launch()
|