Update app.py
Browse files
app.py
CHANGED
@@ -7,6 +7,29 @@ import os
|
|
7 |
# OpenAI API μ€μ (νκ²½ λ³μμμ μ½μ΄μ΄)
|
8 |
openai.api_key = os.getenv("OPENAI_API_KEY") # μ€μ μ½λμμ μ£Όμ ν΄μ
|
9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
def main():
|
11 |
st.title("Keyword Highlighter")
|
12 |
|
@@ -98,31 +121,16 @@ def main():
|
|
98 |
presence_penalty=0
|
99 |
)
|
100 |
|
101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
|
103 |
-
#gptλ΅λ³μ΄ μ€λ₯κ° λλ κ²½μ°κ° μμ΄μ μ€λ₯ 체ν¬λ₯Ό μν λΆλΆ μΆκ°
|
104 |
-
# GPT-3λ‘λΆν° λ°νλ°μ μλ΅
|
105 |
-
highlighted_text = response['choices'][0]['message']['content']
|
106 |
-
|
107 |
-
# 1. λ°νλ ν
μ€νΈκ° `annotated_text(`λ‘ μμνκ³ `)`λ‘ λλλμ§ νμΈ
|
108 |
-
if not highlighted_text.startswith("annotated_text(") or not highlighted_text.endswith(")"):
|
109 |
-
st.error("Invalid response format from GPT-3.")
|
110 |
-
|
111 |
-
# 2. κ΄νΈμ κ°μκ° μ¬λ°λ₯Έμ§ νμΈ
|
112 |
-
elif highlighted_text.count("(") != highlighted_text.count(")"):
|
113 |
-
st.error("Mismatched parentheses in the response.")
|
114 |
-
|
115 |
-
# 3. λ¬Έμμ΄ λ΄μ μλ°μ΄νλ μ±κΈ λ°μ΄νμ λ¬Έμ κ° μλμ§ νμΈ
|
116 |
-
elif highlighted_text.count('"') % 2 != 0:
|
117 |
-
st.error("Mismatched quotes in the response.")
|
118 |
-
|
119 |
-
else:
|
120 |
-
# λ°νλ ν
μ€νΈλ₯Ό μ€νν©λλ€. (μ΄ λΆλΆμ 보μμ μνν μ μμΌλ―λ‘ μ μ€νκ² μ¬μ©ν΄μΌ ν©λλ€.)
|
121 |
-
exec(highlighted_text)
|
122 |
|
123 |
-
# # μ¬κΈ°μλ κ°λ¨νκ² exec ν¨μλ₯Ό μ΄μ©ν΄ GPT-3.5-turboκ° μμ±ν μ½λλ₯Ό μ€νν©λλ€.
|
124 |
-
# # μ€μ νλ‘λμ
νκ²½μμλ 보μ μ΄μλ₯Ό κ³ λ €ν΄μΌ ν©λλ€.
|
125 |
-
# exec(highlighted_text)
|
126 |
|
127 |
if __name__ == "__main__":
|
128 |
main()
|
|
|
7 |
# OpenAI API μ€μ (νκ²½ λ³μμμ μ½μ΄μ΄)
|
8 |
openai.api_key = os.getenv("OPENAI_API_KEY") # μ€μ μ½λμμ μ£Όμ ν΄μ
|
9 |
|
10 |
+
# μ€λ₯ μμ ν¨μ
|
11 |
+
def fix_response_errors(text):
|
12 |
+
if not text.startswith("annotated_text("):
|
13 |
+
text = "annotated_text(" + text
|
14 |
+
if not text.endswith(")"):
|
15 |
+
text += ")"
|
16 |
+
|
17 |
+
open_paren = text.count("(")
|
18 |
+
close_paren = text.count(")")
|
19 |
+
while open_paren > close_paren:
|
20 |
+
text += ")"
|
21 |
+
close_paren += 1
|
22 |
+
while open_paren < close_paren:
|
23 |
+
text = text.rsplit(")", 1)[0]
|
24 |
+
close_paren -= 1
|
25 |
+
|
26 |
+
quotes_count = text.count('"')
|
27 |
+
if quotes_count % 2 != 0:
|
28 |
+
text += '"'
|
29 |
+
|
30 |
+
return text
|
31 |
+
|
32 |
+
|
33 |
def main():
|
34 |
st.title("Keyword Highlighter")
|
35 |
|
|
|
121 |
presence_penalty=0
|
122 |
)
|
123 |
|
124 |
+
highlighted_response = response['choices'][0]['message']['content']
|
125 |
+
fixed_response = fix_response_errors(highlighted_response)
|
126 |
+
|
127 |
+
# μμ λ μλ΅μ st-annotated-textλ‘ μ€ν
|
128 |
+
try:
|
129 |
+
exec(fixed_response)
|
130 |
+
except Exception as e:
|
131 |
+
st.error(f"An error occurred: {e}")
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
|
|
|
|
|
|
|
134 |
|
135 |
if __name__ == "__main__":
|
136 |
main()
|