ll7098ll commited on
Commit
d335f3a
·
verified ·
1 Parent(s): 022a7a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -66
app.py CHANGED
@@ -1,93 +1,83 @@
1
  import os
2
  import time
 
3
 
4
  import google.generativeai as genai
5
- from gradio import gradio as gr
6
 
7
- # Google AI Python SDK 설정
8
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
9
 
10
  # 모델 설정
11
  generation_config = {
12
- "temperature": 1,
13
- "top_p": 0.95,
14
- "top_k": 64,
15
- "max_output_tokens": 8192,
16
- "response_mime_type": "text/plain",
17
  }
18
-
19
  model = genai.GenerativeModel(
20
- model_name="gemini-1.5-pro",
21
- generation_config=generation_config,
22
- system_instruction="언어 치료 전문가, 한글 교육 전문가, 한국어 발음 교정 전문가",
23
  )
24
 
25
- # 대화 기록
26
  chat_history = []
27
 
28
-
29
  def add_to_chat_history(message, speaker):
30
- """
31
- 챗봇 대화 기록에 메시지 추가
32
- """
33
  global chat_history
34
  chat_history.append({"speaker": speaker, "message": message})
35
 
36
-
37
- def clear_chat_history():
38
- """
39
- 챗봇 대화 기록 초기화
40
- """
41
- global chat_history
42
- chat_history = []
43
-
44
 
45
  def generate_response(message):
46
- """
47
- 사용자 메시지에 대한 응답 생성
48
- """
49
- global chat_history
50
-
51
- chat_session = model.start_chat(history=chat_history)
52
  response = chat_session.send_message(message)
 
 
 
 
 
53
 
54
- # 글자씩 출력 효과 위한 코드
 
55
  typed_response = ""
56
- for char in response.text:
57
  typed_response += char
58
  yield typed_response
59
- time.sleep(0.03)
60
 
61
- add_to_chat_history(response.text, "AI")
62
-
63
-
64
- def display_chat_history():
65
- """
66
- 챗봇 대화 기록 출력
67
- """
68
- chat_output = ""
69
- for message in chat_history:
70
- chat_output += f"{message['speaker']}: {message['message']}\n"
71
- return chat_output
72
-
73
-
74
- with gr.Blocks() as demo:
75
- # 챗봇 인터페 생성
76
- chatbot = gr.Chatbot()
77
- msg_input = gr.Textbox(label="메시지 입력")
78
- clear_button = gr.Button("새로운 상담 시작하기")
79
-
80
- # 메시지 전송 버튼 클릭 시 이벤트 처리
81
- msg_input.submit(
82
- generate_response, msg_input, chatbot, queue=True
83
- ).then(display_chat_history, chatbot)
84
-
85
- # 새로운 상담 시작하기 버튼 클릭 시 이벤트 처리
86
- clear_button.click(
87
- clear_chat_history,
88
- None,
89
- chatbot,
90
- ).then(lambda: "", chatbot)
91
-
92
- # 데모 실행
93
- demo.launch()
 
1
  import os
2
  import time
3
+ import gradio as gr
4
 
5
  import google.generativeai as genai
 
6
 
7
+ # Google Gemini API 설정
8
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
9
 
10
  # 모델 설정
11
  generation_config = {
12
+ "temperature": 1,
13
+ "top_p": 0.95,
14
+ "top_k": 64,
15
+ "max_output_tokens": 8192,
16
+ "response_mime_type": "text/plain",
17
  }
 
18
  model = genai.GenerativeModel(
19
+ model_name="gemini-1.5-pro",
20
+ generation_config=generation_config,
21
+ system_instruction="언어 치료 전문가, 한글 교육 전문가, 한국어 발음 교정 전문가",
22
  )
23
 
24
+ # 챗봇 기록 초기화
25
  chat_history = []
26
 
 
27
  def add_to_chat_history(message, speaker):
28
+ """챗봇 기록에 메시지 추가"""
 
 
29
  global chat_history
30
  chat_history.append({"speaker": speaker, "message": message})
31
 
32
+ def display_chat_history():
33
+ """챗봇 기록을 HTML 형식으로 표시"""
34
+ chat_html = ""
35
+ for message in chat_history:
36
+ chat_html += f"<p><b>{message['speaker']}:</b> {message['message']}</p>"
37
+ return chat_html
 
 
38
 
39
  def generate_response(message):
40
+ """Google Gemini API를 사용하여 응답 생성"""
41
+ global chat_session
 
 
 
 
42
  response = chat_session.send_message(message)
43
+ return response.text
44
+
45
+ def chatbot_response(message):
46
+ """사용자 메시지 처리 및 AI 응답 생성"""
47
+ add_to_chat_history(message, "사용자")
48
 
49
+ # AI 응답 생성 및 타이핑 효과 적용
50
+ ai_response = generate_response(message)
51
  typed_response = ""
52
+ for char in ai_response:
53
  typed_response += char
54
  yield typed_response
55
+ time.sleep(0.02)
56
 
57
+ def new_chat():
58
+ """새로운 상담 시작 및 기록 초기화"""
59
+ global chat_history, chat_session
60
+ chat_history = []
61
+ chat_session = model.start_chat()
62
+ return "새로운 상담을 시작합니다.", ""
63
+
64
+ # Gradio 인터페이스 설정
65
+ with gr.Blocks() as iface:
66
+ chatbot_output = gr.HTML(value="", label="챗봇")
67
+ user_input = gr.Textbox(placeholder="메시지를 입력하세요...", label="사용자")
68
+ submit_button = gr.Button("전송")
69
+ new_chat_button = gr.Button("새로운 상담 시작하기")
70
+
71
+ # 버튼 클릭 벤트 처리
72
+ submit_button.click(
73
+ fn=chatbot_response,
74
+ inputs=user_input,
75
+ outputs=chatbot_output,
76
+ queue=True,
77
+ )
78
+ new_chat_button.click(
79
+ fn=new_chat,
80
+ outputs=[chatbot_output, user_input],
81
+ )
82
+
83
+ iface.launch()