Spaces:
Runtime error
Runtime error
edit UI for small device & refactor major func
Browse files- gpt_based_function.py +24 -8
gpt_based_function.py
CHANGED
|
@@ -2,33 +2,49 @@ import ast
|
|
| 2 |
import openai
|
| 3 |
from text_annotator import generate_annotated_text
|
| 4 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def gpt_keyword_highlighter(user_text):
|
| 7 |
'''
|
| 8 |
:param user_text: str
|
| 9 |
:return: annotated_text: str
|
| 10 |
'''
|
| 11 |
-
|
| 12 |
-
|
| 13 |
user_prompt = r"{input_text}=" + f"{user_text}"
|
| 14 |
-
|
| 15 |
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
|
| 16 |
-
|
| 17 |
response = openai.ChatCompletion.create(
|
| 18 |
model="gpt-3.5-turbo",
|
| 19 |
messages=messages,
|
| 20 |
temperature=0,
|
| 21 |
-
max_tokens=
|
| 22 |
top_p=0,
|
| 23 |
frequency_penalty=0,
|
| 24 |
presence_penalty=0
|
| 25 |
)
|
| 26 |
-
|
| 27 |
extracted_keywords = response['choices'][0]['message']['content']
|
| 28 |
-
|
| 29 |
## literal_eval 함수를 사용하여 string을 list로 변환
|
| 30 |
extracted_keywords = ast.literal_eval(extracted_keywords)
|
| 31 |
-
|
| 32 |
## highlighted_text 후처리 함수 추가
|
| 33 |
highlighted_text = generate_annotated_text(text=user_text, keyw_list=extracted_keywords)
|
| 34 |
|
|
|
|
| 2 |
import openai
|
| 3 |
from text_annotator import generate_annotated_text
|
| 4 |
|
| 5 |
+
def gpt_summary_generator(user_text):
|
| 6 |
+
'''
|
| 7 |
+
:param user_text:
|
| 8 |
+
:return:
|
| 9 |
+
'''
|
| 10 |
+
task_description_fs = "Summarize the text in 500 characters in Korean."
|
| 11 |
+
user_prompt = f"{user_text}"
|
| 12 |
+
messages = [{"role": "system", "content": task_description_fs}, {"role": "user", "content": user_prompt}]
|
| 13 |
+
response = openai.ChatCompletion.create(
|
| 14 |
+
model="gpt-3.5-turbo-16k",
|
| 15 |
+
messages=messages,
|
| 16 |
+
temperature=0.06,
|
| 17 |
+
max_tokens=8362,
|
| 18 |
+
top_p=0,
|
| 19 |
+
frequency_penalty=0,
|
| 20 |
+
presence_penalty=0
|
| 21 |
+
)
|
| 22 |
+
gpt_summary = response['choices'][0]['message']['content']
|
| 23 |
+
|
| 24 |
+
return gpt_summary
|
| 25 |
+
|
| 26 |
|
| 27 |
def gpt_keyword_highlighter(user_text):
|
| 28 |
'''
|
| 29 |
:param user_text: str
|
| 30 |
:return: annotated_text: str
|
| 31 |
'''
|
| 32 |
+
# get keywords from user_text
|
| 33 |
+
task_description = "You are a Python function that extracts 5 keywords from {input_text} considering {overall_text_contents}. The output should be formatted as ['keyword1', 'keyword2', ...]. Return only the function's output, with no additional explanations."
|
| 34 |
user_prompt = r"{input_text}=" + f"{user_text}"
|
|
|
|
| 35 |
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
|
|
|
|
| 36 |
response = openai.ChatCompletion.create(
|
| 37 |
model="gpt-3.5-turbo",
|
| 38 |
messages=messages,
|
| 39 |
temperature=0,
|
| 40 |
+
max_tokens=2006,
|
| 41 |
top_p=0,
|
| 42 |
frequency_penalty=0,
|
| 43 |
presence_penalty=0
|
| 44 |
)
|
|
|
|
| 45 |
extracted_keywords = response['choices'][0]['message']['content']
|
|
|
|
| 46 |
## literal_eval 함수를 사용하여 string을 list로 변환
|
| 47 |
extracted_keywords = ast.literal_eval(extracted_keywords)
|
|
|
|
| 48 |
## highlighted_text 후처리 함수 추가
|
| 49 |
highlighted_text = generate_annotated_text(text=user_text, keyw_list=extracted_keywords)
|
| 50 |
|