Update app.py
Browse files
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
|
| 8 |
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
|
| 9 |
|
| 10 |
# 모델 설정
|
| 11 |
generation_config = {
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
}
|
| 18 |
-
|
| 19 |
model = genai.GenerativeModel(
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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 |
-
|
| 38 |
-
""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 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
|
| 57 |
typed_response += char
|
| 58 |
yield typed_response
|
| 59 |
-
time.sleep(0.
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
#
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 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()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|