ll7098ll commited on
Commit
ba30c97
·
verified ·
1 Parent(s): e39984b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+
4
+ import google.generativeai as genai
5
+ import streamlit as st
6
+
7
+ # Google Gemini API 설정
8
+ genai.configure(api_key=os.environ["GEMINI_API_KEY"])
9
+
10
+ def generate_text(grade, num_paragraphs, sentences_per_paragraph, structure, topic):
11
+ generation_config = {
12
+ "temperature": 0.3,
13
+ "top_p": 0.95,
14
+ "top_k": 64,
15
+ "max_output_tokens": 15000, # 맥스 토큰 설정 추가
16
+ "response_mime_type": "text/plain",
17
+ }
18
+
19
+ model = genai.GenerativeModel(
20
+ model_name="gemini-1.5-pro-exp-0827",
21
+ generation_config=generation_config,
22
+ )
23
+
24
+ # 프롬프트 생성
25
+ prompt = f"""
26
+ 초등학교 {grade}학년 학생이 이해할 수 있는 수준의 설명문을 작성.
27
+ 설명문의 주제는 {topic}이며, 주제를 중심으로 {structure} 구조에 따라 설명문을 작성할 것,
28
+ 전체 문단 수는 {num_paragraphs} 문단 내외, 문단 당 문장 수는 {sentences_per_paragraph} 문장 내외,
29
+ 설명문은 줄글로 작성하고 문단 구분을 꼭 할 것, 지정된 문단 수와 문단 당 문장 수를 꼭 지킬것.
30
+ """
31
+
32
+ full_text = ""
33
+ placeholder = st.empty() # 텍스트 출력을 위한 placeholder
34
+
35
+ try:
36
+ # 스트리밍 방식으로 응답 생성
37
+ response = model.generate_content(prompt, stream=True)
38
+ for chunk in response:
39
+ full_text += chunk.text
40
+ placeholder.text(full_text) # placeholder에 업데이트된 텍스트 출력
41
+ time.sleep(0.05)
42
+
43
+ except Exception as e:
44
+ st.error(f"에러 발생: {str(e)}")
45
+
46
+
47
+ # Streamlit 인터페이스 설정
48
+ st.title("초등학생을 위한 읽기 자료 생성기")
49
+ st.write("주제를 입력하면 초등학생이 이해하기 쉬운 읽기 자료를 만들어줍니다.")
50
+
51
+ grade = st.selectbox("학년", [str(i) for i in range(1, 7)], index=0)
52
+ num_paragraphs = st.number_input("문단 수", min_value=1, value=3)
53
+ sentences_per_paragraph = st.selectbox("문단 당 문장 수", ["3-4", "5-6", "6-7", "7-8", "9-10"], index=0)
54
+ structure = st.selectbox("설명문 구조", ["정의와 예시", "비교와 대조", "분류", "분석", "인과", "순서"], index=0)
55
+ topic = st.text_area("주제 및 내용을 입력하세요", height=100)
56
+
57
+ if st.button("읽기 자료 생성"):
58
+ generate_text(grade, num_paragraphs, sentences_per_paragraph, structure, topic)