Spaces:
Runtime error
Runtime error
seperate func
Browse files- gpt_based_function.py +34 -0
gpt_based_function.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import ast
|
2 |
+
import openai
|
3 |
+
from text_annotator import generate_annotated_text
|
4 |
+
|
5 |
+
def gpt_keyw_extractor(user_text):
|
6 |
+
'''
|
7 |
+
:param user_text: str
|
8 |
+
:return: list of keywords
|
9 |
+
'''
|
10 |
+
task_description = "You are a Python function that extract 5 keywords from {input_text}. The output should be formatted as [keyword1, keyword2, ...]. Return only the function's output, with no additional explanations."
|
11 |
+
|
12 |
+
user_prompt = r"{input_text}=" + f"{user_text}"
|
13 |
+
|
14 |
+
messages = [{"role": "system", "content": task_description}, {"role": "user", "content": user_prompt}]
|
15 |
+
|
16 |
+
response = openai.ChatCompletion.create(
|
17 |
+
model="gpt-3.5-turbo",
|
18 |
+
messages=messages,
|
19 |
+
temperature=0,
|
20 |
+
max_tokens=1019,
|
21 |
+
top_p=0,
|
22 |
+
frequency_penalty=0,
|
23 |
+
presence_penalty=0
|
24 |
+
)
|
25 |
+
|
26 |
+
extracted_keywords = response['choices'][0]['message']['content']
|
27 |
+
|
28 |
+
## literal_eval 함수를 사용하여 string을 list로 변환
|
29 |
+
extracted_keywords = ast.literal_eval(extracted_keywords)
|
30 |
+
|
31 |
+
## highlighted_text 후처리 함수 추가
|
32 |
+
highlighted_text = generate_annotated_text(text=user_text, keyw_list=extracted_keywords)
|
33 |
+
|
34 |
+
return highlighted_text
|