AIRider commited on
Commit
4ae262e
·
verified ·
1 Parent(s): bb9bd92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -1
app.py CHANGED
@@ -18,12 +18,31 @@ models = {
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()
24
  client = get_client(selected_model)
25
 
26
- # 프롬프트 설정 - 시스템 메시지를 자유롭게 설정 가능
27
  messages = [
28
  {"role": "system", "content": system_message},
29
  {"role": "user", "content": prompt}
@@ -46,6 +65,9 @@ def respond(prompt, system_message, max_tokens, temperature, top_p, selected_mod
46
  if chunk:
47
  response += chunk
48
  total_tokens_used += len(chunk.split()) # 청크당 사용된 토큰 수 추산
 
 
 
49
 
50
  return response
51
 
@@ -61,6 +83,12 @@ def continue_writing(chatbot, system_message, max_tokens, temperature, top_p, se
61
  def stop_generation():
62
  stop_event.set()
63
 
 
 
 
 
 
 
64
  # Gradio UI 구성
65
  with gr.Blocks() as demo:
66
  gr.Markdown("# 프롬프트 최적화 Playground")
@@ -99,6 +127,7 @@ with gr.Blocks() as demo:
99
  continue_btn = gr.Button("계속 작성")
100
  stop = gr.Button("🛑 생성 중단")
101
  clear = gr.Button("🗑️ 대화 내역 지우기")
 
102
 
103
  # Event handlers
104
  send.click(respond, inputs=[prompt, system_message, max_tokens, temperature, top_p, model], outputs=[chatbot])
@@ -108,6 +137,7 @@ with gr.Blocks() as demo:
108
  outputs=[chatbot])
109
  stop.click(stop_generation)
110
  clear.click(lambda: None, outputs=[chatbot])
 
111
 
112
  # UI 실행
113
  demo.launch()
 
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()
43
  client = get_client(selected_model)
44
 
45
+ # 프롬프트 설정
46
  messages = [
47
  {"role": "system", "content": system_message},
48
  {"role": "user", "content": prompt}
 
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
 
 
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")
 
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])
 
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()