ll7098ll commited on
Commit
22032f3
·
verified ·
1 Parent(s): ed8ec9e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -38
app.py CHANGED
@@ -1,46 +1,51 @@
1
- # -*- coding: utf-8 -*-
2
- """Untitled1.ipynb
3
- Automatically generated by Colab.
4
- Original file is located at
5
- https://colab.research.google.com/drive/1huUmrgIbG1wwEKxs3zsCdRNir_IY5ICf
6
- """
7
-
8
  import os
 
 
9
 
10
- from openai import OpenAI
11
- import openai
12
-
13
- openai_api_key = os.getenv("OPENAI_API_KEY")
14
-
15
- client = openai.OpenAI(api_key = openai_api_key)
16
-
17
- def openai_chat(text):
18
- from openai import OpenAI
19
-
20
- completion = client.chat.completions.create(
21
- model="gpt-4o",
22
- messages=[
23
- {"role": "system", "content": "초등학교 6학년 수준 범위 내에서 학생의 질문에 친절하게 답하는 교사 역할, 설명은 구체적인 예시를 들어서, 어려운 단어는 별도로 설명해줘. 잘하면 10달러의 팁을 줄게."},
24
- {"role": "user", "content": text}
25
- ],
26
- temperature=1,
27
- max_tokens=1000,
28
- top_p=0.9,
29
- frequency_penalty=0,
30
- presence_penalty=0
 
 
31
  )
32
 
33
- return completion.choices[0].message.content
34
-
35
- openai_chat("한글은 누가 만들었어?")
36
 
37
- import gradio as gr
 
 
 
 
38
 
39
- with gr.Blocks() as demo:
40
- Q = gr.Textbox(label="질문", placeholder="질문을 넣어주세요.")
41
- btn = gr.Button("질문하기")
42
- A = gr.TextArea(label="AI 선생님의 설명", placeholder="AI 선생님이 답변 중 입니다.")
43
 
44
- btn.click(openai_chat, inputs=Q, outputs=A)
 
 
 
 
 
45
 
46
- demo.launch()
 
 
 
 
 
 
 
 
 
1
  import os
2
+ import google.generativeai as genai
3
+ import gradio as gr
4
 
5
+ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
6
+
7
+ genai.configure(api_key=GEMINI_API_KEY)
8
+
9
+ generation_config = {
10
+ "temperature": 1,
11
+ "top_p": 0.95,
12
+ "top_k": 64,
13
+ "max_output_tokens": 8192,
14
+ "response_mime_type": "text/plain",
15
+ }
16
+ safety_settings = [
17
+ {"category": "HARM_CATEGORY_HARASSMENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
18
+ {"category": "HARM_CATEGORY_HATE_SPEECH", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
19
+ {"category": "HARM_CATEGORY_SEXUALLY_EXPLICIT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
20
+ {"category": "HARM_CATEGORY_DANGEROUS_CONTENT", "threshold": "BLOCK_MEDIUM_AND_ABOVE"},
21
+ ]
22
+
23
+ model = genai.GenerativeModel(
24
+ model_name="gemini-1.5-pro",
25
+ safety_settings=safety_settings,
26
+ generation_config=generation_config,
27
+ system_instruction="교사가 학습 목표를 입력하면, 학생들이 학습 목표에 도달할 수 있게 질문을 계속 해줘, 학생들은 네 질문에 답을 할 것이야. 그러면 바로 답을 제시하지 말고 질문형태로 피드백을 해줘. 질문을 통해서 학생들이 스스로 학습 목표를 도달할 수 있도록 도와줘.",
28
  )
29
 
30
+ chat_session = model.start_chat(history=[])
 
 
31
 
32
+ def chatbot(message, history):
33
+ """
34
+ 챗봇과의 대화를 처리하는 함수
35
+ """
36
+ global chat_session
37
 
38
+ # 새로운 메시지가 입력되면 챗봇에 전달
39
+ response = chat_session.send(message)
40
+ history.append((message, response))
41
+ return history, response
42
 
43
+ iface = gr.ChatInterface(
44
+ fn=chatbot,
45
+ title="학습 챗봇",
46
+ description="학습 목표를 입력하고 챗봇과 대화해보세요!",
47
+ theme="compact",
48
+ )
49
 
50
+ # 실행
51
+ iface.launch(share=True)