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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -61
app.py CHANGED
@@ -4,80 +4,65 @@ 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()
 
4
 
5
  import google.generativeai as genai
6
 
 
7
  genai.configure(api_key=os.environ["GEMINI_API_KEY"])
8
 
9
  # 모델 설정
10
  generation_config = {
11
+ "temperature": 1,
12
+ "top_p": 0.95,
13
+ "top_k": 64,
14
+ "max_output_tokens": 8192,
15
+ "response_mime_type": "text/plain",
16
  }
 
 
 
 
 
17
 
18
+ # 시스템 명령어 설정
19
+ system_instruction = "언어 치료 전문가, 한글 교육 전문가, 한국어 발음 교정 전문가"
20
 
21
+ def type_text(text):
22
+ for char in text:
23
+ print(char, end='', flush=True)
24
+ time.sleep(0.03) # 타이핑 속도 조절
25
+ print("\n") # 줄바꿈 추가
26
 
27
+ def chat(message, history):
28
+ # 새로운 상담 시작 기록 초기화
29
+ if message == "/새로운 상담 시작":
30
+ history = []
31
+ return "새로운 상담을 시작합니다.", history
 
32
 
33
+ # 모델 생성 및 채팅 세션 시작
34
+ model = genai.GenerativeModel(
35
+ model_name="gemini-1.5-pro",
36
+ generation_config=generation_config,
37
+ system_instruction=system_instruction,
38
+ )
39
+ chat_session = model.start_chat(history=history)
40
 
41
+ # 사용자 메시지 추가
42
+ history.append({"role": "user", "message": message})
 
43
 
44
+ # AI 응답 생성 및 추가
45
+ response = chat_session.send_message(message)
46
+ history.append({"role": "assistant", "message": response.text})
 
 
 
 
47
 
48
+ # 타이핑 효과 출력
49
+ type_text(response.text)
 
 
 
 
50
 
51
+ return "", history
 
 
 
 
 
52
 
53
+ # Gradio 인터페 설정
54
+ iface = gr.Interface(
55
+ fn=chat,
56
+ inputs=[
57
+ gr.inputs.Textbox(lines=5, placeholder="문의 내용을 입력하세요."),
58
+ gr.State([]),
59
+ ],
60
+ outputs=[
61
+ gr.outputs.Textbox(label="AI 답변"),
62
+ gr.State(),
63
+ ],
64
+ title="AI 언어 치료 상담 챗봇",
65
+ description="언어 치료 전문가 AI와 상담해보세요. 새로운 상담을 시작하려면 '/새로운 상담 시작'을 입력하세요.",
66
+ )
67
 
68
  iface.launch()