| import os |
| import google.generativeai as genai |
| import gradio as gr |
|
|
| genai.configure(api_key=os.environ["GEMINI_API_KEY"]) |
|
|
| |
| generation_config = { |
| "temperature": 1, |
| "top_p": 0.95, |
| "top_k": 64, |
| "max_output_tokens": 8192, |
| "response_mime_type": "text/plain", |
| } |
| safety_settings = [ |
| {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, |
| {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, |
| {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, |
| {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"}, |
| ] |
|
|
| model = genai.GenerativeModel( |
| model_name="gemini-1.5-pro", |
| safety_settings=safety_settings, |
| generation_config=generation_config, |
| system_instruction="교사가 학습 목표를 입력하면, 학생들이 학습 목표에 도달할 수 있게 질문을 계속 해줘, 학생들은 네 질문에 답을 할 것이야. 그러면 바로 답을 제시하지 말고 질문형태로 피드백을 해줘. 질문을 통해서 학생들이 스스로 학습 목표를 도달할 수 있도록 도와줘.", |
| ) |
|
|
| chat_session = model.start_chat(history=[]) |
|
|
| def respond(user_input, history): |
| response = chat_session.send_message(user_input) |
| history.append((user_input, response.text)) |
| return "", history |
|
|
| with gr.Blocks() as demo: |
| gr.Markdown("<div style='font-size: 30px; font-weight: bold;'>AI 선생님</div>") |
| chatbot = gr.Chatbot(label="채팅창") |
| msg = gr.Textbox(label="입력") |
| clear = gr.Button("초기화") |
|
|
| msg.submit(respond, [msg, chatbot], [msg, chatbot]) |
| clear.click(lambda: [], None, chatbot, queue=False) |
|
|
| demo.launch() |