JUNGU commited on
Commit
924cf8c
ยท
1 Parent(s): 4e2a346

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -36
app.py CHANGED
@@ -7,44 +7,46 @@ import os
7
  # OpenAI API ์„ค์ • (ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์ฝ์–ด์˜ด)
8
  openai.api_key = os.getenv("OPENAI_API_KEY") # ์‹ค์ œ ์ฝ”๋“œ์—์„œ ์ฃผ์„ ํ•ด์ œ
9
 
10
- # Streamlit ์•ฑ ์‹œ์ž‘
11
- def app():
12
- st.title("ํ‚ค์›Œ๋“œ ๋ถ„์„")
13
-
14
- user_text = st.text_area("๋ถ„์„ํ•  ํ…์ŠคํŠธ๋ฅผ ๋ถ™์—ฌ ๋„ฃ์œผ์„ธ์š”:", height=300)
15
-
16
- if st.button("ํ‚ค์›Œ๋“œ ๋ถ„์„"):
17
- # ํ‚ค์›Œ๋“œ ์ถ”์ถœ ๋กœ์ง (GPT๋ฅผ ์‚ฌ์šฉ)
18
- task_description = "Identify key terms in the text."
19
- user_prompt = f"{user_text}"
20
- messages = [
21
- {"role": "system", "content": task_description},
22
- {"role": "user", "content": user_prompt},
23
- ]
24
-
25
- response = openai.Completion.create(
26
- model="gpt-3.5-turbo",
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  messages=messages,
28
- max_tokens=100,
 
29
  )
30
 
31
- # GPT๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ์‘๋‹ต์„ ํŒŒ์‹ฑํ•˜์—ฌ ํ‚ค์›Œ๋“œ ์ถ”์ถœ
32
- extracted_keywords = response['choices'][0]['message']['content'].split(", ")
33
-
34
- # ๋นˆ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ดˆ๊ธฐํ™”ํ•˜์—ฌ ์ฃผ์„์ด ๋‹ฌ๋ฆฐ ํ…์ŠคํŠธ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.
35
- annotated_list = []
36
-
37
- # ํ…์ŠคํŠธ๋ฅผ ๋‹จ์–ด๋กœ ๋ถ„ํ• ํ•˜๊ณ , ๊ฐ ๋‹จ์–ด๋ฅผ ๊ฒ€์‚ฌํ•˜์—ฌ ์ฃผ์„์„ ๋‹ต๋‹ˆ๋‹ค.
38
- for word in user_text.split():
39
- if word in extracted_keywords:
40
- annotated_list.append((word, 'Keyword'))
41
- else:
42
- annotated_list.append(word)
43
- annotated_list.append(" ") # ์›๋ž˜์˜ ๊ณต๋ฐฑ์„ ๋ณต์›ํ•ฉ๋‹ˆ๋‹ค.
44
-
45
- # ์ฃผ์„์ด ๋‹ฌ๋ฆฐ ํ…์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.
46
- annotated_text(*annotated_list)
47
 
48
- # Streamlit ์•ฑ ์‹คํ–‰
49
  if __name__ == "__main__":
50
- app()
 
7
  # OpenAI API ์„ค์ • (ํ™˜๊ฒฝ ๋ณ€์ˆ˜์—์„œ ์ฝ์–ด์˜ด)
8
  openai.api_key = os.getenv("OPENAI_API_KEY") # ์‹ค์ œ ์ฝ”๋“œ์—์„œ ์ฃผ์„ ํ•ด์ œ
9
 
10
+ def main():
11
+ st.title("Keyword Highlighter")
12
+
13
+ user_text = st.text_area("Please enter your text here:", "")
14
+
15
+ if st.button("Find Keywords"):
16
+
17
+ # few-shot learning์„ ์ด์šฉํ•œ task_description
18
+ task_description = """You are a helpful assistant that generates annotated text for the st-annotated-text library in Python. Highlight the key terms that are most important in the context of the sentence. Your output should be formatted in the following way:
19
+ annotated_text(
20
+ "This ",
21
+ ("is", ""),
22
+ " some ",
23
+ ("annotated", ""),
24
+ ("text", ""),
25
+ " for those of ",
26
+ ("you", ""),
27
+ " who ",
28
+ ("like", ""),
29
+ " this sort of ",
30
+ ("thing", ""),
31
+ ". "
32
+ )"""
33
+
34
+ user_prompt = f"Now, please annotate this text: {user_text}"
35
+
36
+ messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
37
+
38
+ response = openai.ChatCompletion.create(
39
+ model="gpt-3.5-turbo-16k",
40
  messages=messages,
41
+ temperature=0.1,
42
+ max_tokens=1000
43
  )
44
 
45
+ highlighted_text = response['choices'][0]['message']['content']
46
+
47
+ # ์—ฌ๊ธฐ์„œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ exec ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด GPT-3.5-turbo๊ฐ€ ์ƒ์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค.
48
+ # ์‹ค์ œ ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” ๋ณด์•ˆ ์ด์Šˆ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
49
+ exec(highlighted_text)
 
 
 
 
 
 
 
 
 
 
 
50
 
 
51
  if __name__ == "__main__":
52
+ main()