-
Notifications
You must be signed in to change notification settings - Fork 27
/
08-mbti.py
70 lines (60 loc) · 2.17 KB
/
08-mbti.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import json
import configparser
import http.client
import streamlit as st
class CompletionExecutor:
def __init__(self, host, api_key, api_key_primary_val, request_id):
self._host = host
self._api_key = api_key
self._api_key_primary_val = api_key_primary_val
self._request_id = request_id
def _send_request(self, completion_request):
headers = {
'Content-Type': 'application/json; charset=utf-8',
'X-NCP-CLOVASTUDIO-API-KEY': self._api_key,
'X-NCP-APIGW-API-KEY': self._api_key_primary_val,
'X-NCP-CLOVASTUDIO-REQUEST-ID': self._request_id
}
conn = http.client.HTTPSConnection(self._host)
conn.request('POST', '/testapp/v1/completions/LK-D', json.dumps(completion_request), headers)
response = conn.getresponse()
result = json.loads(response.read().decode(encoding='utf-8'))
conn.close()
return result
def execute(self, completion_request):
res = self._send_request(completion_request)
if res['status']['code'] == '20000':
return res['result']['text']
else:
return 'Error'
config = configparser.ConfigParser()
config.sections()
config.read('./your_apikey.ini')
completion_executor = CompletionExecutor(
host=config['CLOVA']['host'],
api_key=config['CLOVA']['api_key'],
api_key_primary_val=config['CLOVA']['api_key_primary_val'],
request_id=config['CLOVA']['request_id']
)
st.title('MBTI 대백과사전')
question = st.text_input(
'질문',
placeholder='질문을 입력해 주세요'
)
if question:
preset_text = f'MBTI에 대한 지식을 기반으로, 아래의 질문에 답해보세요.\n\n질문:{question}'
request_data = {
'text': preset_text,
'maxTokens': 100,
'temperature': 0.5,
'topK': 0,
'topP': 0.8,
'repeatPenalty': 5.0,
'start': '\n$$$답:',
'stopBefore': ['###', '질문:', '답:', '###\n'],
'includeTokens': True,
'includeAiFilters': True,
'includeProbs': True
}
response_text = completion_executor.execute(request_data)
st.markdown(response_text.split('$$$')[1])