JUNGU commited on
Commit
f4de003
ยท
1 Parent(s): fc12987

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -27
app.py CHANGED
@@ -5,28 +5,29 @@ from annotated_text import annotated_text
5
  import os
6
  import achivenment_standards as data
7
 
 
8
  # OpenAI API ์„ค์ • (ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์ฝ์–ด์˜ด)
9
  openai.api_key = os.getenv("OPENAI_API_KEY")
10
 
11
- # gpt์ด์šฉํ•ด์„œ ์ถ”๋ก ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ
12
  def generate_annotated_text(text):
13
  response = openai.ChatCompletion.create(
14
- model="gpt-3.5-turbo-16k",
15
- messages=[
16
- {
17
- "role": "system",
18
- "content": "์„ฑ์ทจ๊ธฐ์ค€ ๊ธฐ๋ฐ˜ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™ ํ‰๊ฐ€ ์ƒ์„ฑ\n์„ฑ์ทจ๊ธฐ์ค€์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด, ํ•ด๋‹น ์„ฑ์ทจ๊ธฐ์ค€์— ๊ธฐ๋ฐ˜ํ•œ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™์— ๋Œ€ํ•œ ํ‰๊ฐ€๋ฅผ annotated_text ํ˜•์‹์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค."
19
- },
20
- {
21
- "role": "user",
22
- "content": text
23
- }
24
- ],
25
- temperature=1,
26
- max_tokens=10000,
27
- top_p=1,
28
- frequency_penalty=0,
29
- presence_penalty=0
30
  )
31
  return response['choices'][0]['message']['content']
32
 
@@ -52,10 +53,11 @@ def generate_similar_sentences(base_sentence):
52
  )
53
  generated_sentences = response.choices[0].message['content'].split('\n')
54
  return [sentence.strip() for sentence in generated_sentences if sentence.strip()]
55
-
 
56
  # Streamlit ์•ฑ์˜ ์ œ๋ชฉ ๋ฐ ์„ค๋ช…
57
  st.title("์„ฑ์ทจ๊ธฐ์ค€ ๊ธฐ๋ฐ˜ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™ ํ‰๊ฐ€ ์ƒ์„ฑ")
58
- st.write("์„ฑ์ทจ๊ธฐ์ค€์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด, ํ•ด๋‹น ์„ฑ์ทจ๊ธฐ์ค€์— ๊ธฐ๋ฐ˜ํ•œ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™์— ๋Œ€ํ•œ ํ‰๊ฐ€๋ฅผ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
59
 
60
  # ์„ฑ์ทจ๊ธฐ์ค€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
61
  achievement_standards = data.achievement_standards
@@ -84,19 +86,21 @@ if 'similar_sentences' not in st.session_state:
84
  if st.button("ํ‰๊ฐ€ ์ƒ์„ฑ"):
85
  with st.spinner('๋‹ต๋ณ€ ์ƒ์„ฑ์ค‘...'):
86
  result = generate_annotated_text(achievement_standard)
87
- st.session_state.generated_result = result # ๊ฒฐ๊ณผ ์ €์žฅ
88
  exec(result.replace('```', ''))
89
 
90
  # annotated_text ๊ฒฐ๊ณผ์—์„œ ๋ฌธ์žฅ๋งŒ ์ถ”์ถœ
91
  result_lines = result.split('\n')
92
  sentences = []
93
  for line in result_lines:
 
94
  start_idx = line.find('("')
95
  if start_idx != -1:
96
  end_idx = line.find('",', start_idx)
97
  sentence = line[start_idx + 2:end_idx].strip() # "(" ๋‹ค์Œ๋ถ€ํ„ฐ "," ์ „๊นŒ์ง€์˜ ๋ฌธ์ž์—ด์„ ์ถ”์ถœ
98
  sentences.append(sentence)
99
 
 
100
  # ๋ฌธ์žฅ์„ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์œผ๋กœ ํ‘œ์‹œ ๋ฐ ์„ ํƒ๋œ ๋ฌธ์žฅ ์ €์žฅ
101
  selected_index = st.radio("๋ฌธ์žฅ์„ ์„ ํƒํ•˜์„ธ์š”:", range(len(sentences)), format_func=lambda x: sentences[x])
102
  st.session_state.selected_sentence = sentences[selected_index] if sentences else None
@@ -104,10 +108,6 @@ if st.button("ํ‰๊ฐ€ ์ƒ์„ฑ"):
104
  # ์œ ์‚ฌํ•œ ๋ฌธ์žฅ ์ƒ์„ฑ ๋ฒ„ํŠผ ์ถ”๊ฐ€
105
  if st.button("์œ ์‚ฌํ•œ ๋ฌธ๊ตฌ ์ƒ์„ฑ") and st.session_state.get('selected_sentence'):
106
  with st.spinner('๋ฌธ์žฅ ์ƒ์„ฑ์ค‘...'):
107
- st.session_state.similar_sentences = generate_similar_sentences(st.session_state.selected_sentence)
108
-
109
- # ์ƒ์„ฑ๋œ ๊ฒฐ๊ณผ ๋ฐ ์œ ์‚ฌํ•œ ๋ฌธ์žฅ๋“ค ์ถœ๋ ฅ
110
- if st.session_state.get('generated_result'):
111
- exec(st.session_state.generated_result.replace('```', ''))
112
- for sentence in st.session_state.similar_sentences:
113
- st.write(sentence)
 
5
  import os
6
  import achivenment_standards as data
7
 
8
+
9
  # OpenAI API ์„ค์ • (ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์ฝ์–ด์˜ด)
10
  openai.api_key = os.getenv("OPENAI_API_KEY")
11
 
12
+ #gpt์ด์šฉํ•ด์„œ ์ถ”๋ก ํ•จ์ˆ˜ ๋งŒ๋“ค๊ธฐ
13
  def generate_annotated_text(text):
14
  response = openai.ChatCompletion.create(
15
+ model="gpt-3.5-turbo-16k",
16
+ messages=[
17
+ {
18
+ "role": "system",
19
+ "content": "์„ฑ์ทจ๊ธฐ์ค€ ๊ธฐ๋ฐ˜ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™ ํ‰๊ฐ€ ์ƒ์„ฑ\n์„ฑ์ทจ๊ธฐ์ค€์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด, ํ•ด๋‹น ์„ฑ์ทจ๊ธฐ์ค€์— ๊ธฐ๋ฐ˜ํ•œ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™์— ๋Œ€ํ•œ ํ‰๊ฐ€๋ฅผ annotated_text ํ˜•์‹์œผ๋กœ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์„ฑ์ทจ๊ธฐ์ค€์„ ๋ณด๋ฉฐ ํ•™์ƒ์˜ ํŠน์ • ํ™œ๋™, ์„ฑ์ทจ ์ˆ˜์ค€, ๊ต์‚ฌ์˜ ์ดํ‰, ๊ทธ๋ฆฌ๊ณ  ํ•™์ƒ์˜ ์—ญ๋Ÿ‰์„ ๊ณ ๋ คํ•˜์—ฌ ์ฒด๊ณ„์ ์œผ๋กœ ๊ตฌ์„ฑ๋œ ์ถœ๋ ฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ฃผ์–ด๋Š” ๋ฐ˜๋“œ์‹œ ์ƒ๋žตํ•ฉ๋‹ˆ๋‹ค. \n\n์˜ˆ์ œ:\n์ž…๋ ฅ: ```์„ฑ์ทจ๊ธฐ์ค€: [6๊ตญ01-07]์ƒ๋Œ€๊ฐ€ ์ฒ˜ํ•œ ์ƒํ™ฉ์„ ์ดํ•ดํ•˜๊ณ  ๊ณต๊ฐํ•˜๋ฉฐ ๋“ฃ๋Š” ํƒœ๋„๋ฅผ ์ง€๋‹Œ๋‹ค, [6๊ตญ01-02] ์˜๊ฒฌ์„ ์ œ์‹œํ•˜๊ณ  ํ•จ๊ป˜ ์กฐ์ •ํ•˜๋ฉฐ ํ† ์˜ํ•œ๋‹ค.```\n์ถœ๋ ฅ: ```annotated_text(\n (\"ํ‰์†Œ ์ž์‹ ์˜ ์ƒ๊ฐ์„ ์ผ๋ชฉ์š”์—ฐํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๋Š” ์Šต๊ด€์ด ์žˆ์Œ.\", \"์—ญ๋Ÿ‰\", \"rgba(255, 0, 0, 0.3)\"),\n (\"์‚ฌํšŒ ํ˜„์•ˆ์— ๊ด€ํ•œ ์ฃผ์žฅํ•˜๋Š” ๊ธ€์“ฐ๊ธฐ๋ฅผ ์ž˜ํ•จ.\", \"์„ฑ์ทจ์ˆ˜์ค€\", \"rgba(0, 0, 255, 0.3)\"),\n (\"์นœ๊ตฌ์˜ ๊ณ ๋ฏผ์„ ํ•ด๊ฒฐํ•ด์ฃผ๋Š” ์—ญํ• ๊ทน์—์„œ ์ƒ๋Œ€๋ฐฉ์„ ๋ฐฐ๋ คํ•˜์—ฌ ํ•ด๊ฒฐ ๊ฐ€๋Šฅํ•œ ๋ฐฉ์•ˆ์„ ์ œ์•ˆํ•จ.\", \"์ˆ˜ํ–‰\", \"rgba(0, 128, 0, 0.3)\"),\n (\"์ƒ๋Œ€๊ฐ€ ์ฒ˜ํ•œ ์ƒํ™ฉ์„ ์ดํ•ดํ•˜๊ณ  ๊ณต๊ฐํ•˜๋Š” ํƒœ๋„๋ฅผ ๊ฐ€์ง€๊ณ  ์นœ๊ตฌ๋“ค๊ณผ ์›๋งŒํ•œ ๊ด€๊ณ„๋ฅผ ๋งบ๊ณ  ๊ฐˆ๋“ฑ์„ ์กฐ์ •ํ•จ.\", \"๊ต์‚ฌ์ดํ‰\", \"rgba(128, 128, 128, 0.3)\"),\n (\"์ค‘๊ฐ„ ๋†€์ด ์‹œ๊ฐ„์— ์šด๋™์žฅ์„ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ์ •ํ•˜๊ธฐ๋ฅผ ์ฃผ์ œ๋กœ ํ•œ ํ† ์˜์—์„œ ์•Œ๋งž์€ ๊ทผ๊ฑฐ์™€ ๋’ท๋ฐ›์นจํ•  ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๋ฅผ ํ† ๋Œ€๋กœ ์ž์‹ ์˜ ์˜๊ฒฌ์„ ํƒ€๋‹นํ•˜๊ฒŒ ์ œ์‹œํ•˜๋ฉด์„œ ๋‹ค๋ฅธ ์‚ฌ๋žŒ์˜ ์˜๊ฒฌ์„ ๋Šฅ๋™์ ์œผ๋กœ ์ˆ˜์šฉํ•˜๊ณ  ํšจ๊ณผ์ ์œผ๋กœ ์˜๊ฒฌ์„ ์กฐ์ •ํ•˜๋Š” ๋Šฅ๋ ฅ์„ ๋ณด์ž„.\", \"์ˆ˜ํ–‰\", \"rgba(0, 128, 0, 0.3)\"),\n (\"์ƒ๋Œ€์˜ ์˜๊ฒฌ์„ ์กด์ค‘ํ•˜๊ณ  ํ˜‘๋ ฅํ•˜๋Š” ํƒœ๋„๋ฅผ ๋ณด์ž„.\", \"์—ญ๋Ÿ‰\", \"rgba(255, 0, 0, 0.3)\")\n)\n```"
20
+ },
21
+ {
22
+ "role": "user",
23
+ "content": text
24
+ }
25
+ ],
26
+ temperature=1,
27
+ max_tokens=10000,
28
+ top_p=1,
29
+ frequency_penalty=0,
30
+ presence_penalty=0
31
  )
32
  return response['choices'][0]['message']['content']
33
 
 
53
  )
54
  generated_sentences = response.choices[0].message['content'].split('\n')
55
  return [sentence.strip() for sentence in generated_sentences if sentence.strip()]
56
+
57
+
58
  # Streamlit ์•ฑ์˜ ์ œ๋ชฉ ๋ฐ ์„ค๋ช…
59
  st.title("์„ฑ์ทจ๊ธฐ์ค€ ๊ธฐ๋ฐ˜ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™ ํ‰๊ฐ€ ์ƒ์„ฑ")
60
+ st.write("์„ฑ์ทจ๊ธฐ์ค€์„ ์ž…๋ ฅํ•˜์‹œ๋ฉด, ํ•ด๋‹น ์„ฑ์ทจ๊ธฐ์ค€์— ๊ธฐ๋ฐ˜ํ•œ ํ•™์ƒ์˜ ํŠน์„ฑ ๋ฐ ํ™œ๋™์— ๋Œ€ํ•œ ํ‰๊ฐ€๋ฅผ \n\n [ํ•™์ƒ ํ™œ๋™, ์„ฑ์ทจ ์ˆ˜์ค€, ๊ต์‚ฌ์˜ ์ดํ‰, ํ•™์ƒ ์—ญ๋Ÿ‰] 4๊ฐ€์ง€ ์š”์†Œ๋ฅผ ์กฐํ•ฉํ•˜์—ฌ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค.")
61
 
62
  # ์„ฑ์ทจ๊ธฐ์ค€ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ
63
  achievement_standards = data.achievement_standards
 
86
  if st.button("ํ‰๊ฐ€ ์ƒ์„ฑ"):
87
  with st.spinner('๋‹ต๋ณ€ ์ƒ์„ฑ์ค‘...'):
88
  result = generate_annotated_text(achievement_standard)
89
+ # ๊ฒฐ๊ณผ ์ถœ๋ ฅ
90
  exec(result.replace('```', ''))
91
 
92
  # annotated_text ๊ฒฐ๊ณผ์—์„œ ๋ฌธ์žฅ๋งŒ ์ถ”์ถœ
93
  result_lines = result.split('\n')
94
  sentences = []
95
  for line in result_lines:
96
+ # "(" ๋ฌธ์ž์—ด์ด ํฌํ•จ๋œ ์œ„์น˜๋ฅผ ์ฐพ์•„์„œ ๊ทธ ์ดํ›„์˜ ๋ฌธ์ž์—ด๋งŒ ์ถ”์ถœ
97
  start_idx = line.find('("')
98
  if start_idx != -1:
99
  end_idx = line.find('",', start_idx)
100
  sentence = line[start_idx + 2:end_idx].strip() # "(" ๋‹ค์Œ๋ถ€ํ„ฐ "," ์ „๊นŒ์ง€์˜ ๋ฌธ์ž์—ด์„ ์ถ”์ถœ
101
  sentences.append(sentence)
102
 
103
+
104
  # ๋ฌธ์žฅ์„ ๋ผ๋””์˜ค ๋ฒ„ํŠผ์œผ๋กœ ํ‘œ์‹œ ๋ฐ ์„ ํƒ๋œ ๋ฌธ์žฅ ์ €์žฅ
105
  selected_index = st.radio("๋ฌธ์žฅ์„ ์„ ํƒํ•˜์„ธ์š”:", range(len(sentences)), format_func=lambda x: sentences[x])
106
  st.session_state.selected_sentence = sentences[selected_index] if sentences else None
 
108
  # ์œ ์‚ฌํ•œ ๋ฌธ์žฅ ์ƒ์„ฑ ๋ฒ„ํŠผ ์ถ”๊ฐ€
109
  if st.button("์œ ์‚ฌํ•œ ๋ฌธ๊ตฌ ์ƒ์„ฑ") and st.session_state.get('selected_sentence'):
110
  with st.spinner('๋ฌธ์žฅ ์ƒ์„ฑ์ค‘...'):
111
+ similar_sentences = generate_similar_sentences(st.session_state.selected_sentence)
112
+ for sentence in st.session_state.similar_sentences:
113
+ st.write(sentence)