codingchild commited on
Commit
2bf5d9d
โ€ข
0 Parent(s):

first commit

Browse files
Files changed (9) hide show
  1. .gitattributes +34 -0
  2. .gitignore +2 -0
  3. README.md +13 -0
  4. app.py +224 -0
  5. app.sh +1 -0
  6. db_modules.py +60 -0
  7. debate_bot.py +42 -0
  8. gpt_modules.py +23 -0
  9. requirements.txt +4 -0
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ __pycache__/
2
+ .env
README.md ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Debate With Gpt
3
+ emoji: ๐Ÿ‘
4
+ colorFrom: red
5
+ colorTo: yellow
6
+ sdk: streamlit
7
+ sdk_version: 1.19.0
8
+ app_file: app.py
9
+ pinned: false
10
+ license: openrail
11
+ ---
12
+
13
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,224 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import streamlit as st
3
+ from streamlit_chat import message
4
+ import requests
5
+ from time import time
6
+ import json
7
+ import boto3
8
+
9
+ from db_modules import get_db, put_item, get_item, get_lastest_item
10
+ from gpt_modules import gpt_call
11
+ from debate_bot import debate_bot
12
+
13
+ import os
14
+
15
+
16
+
17
+ # DB
18
+ dynamodb = get_db()
19
+ debate_bot_log_table = dynamodb.Table('debate_bot_log')
20
+
21
+
22
+ ###########
23
+ # streamlit
24
+ ###########
25
+ st.header("DEBATE BOT")
26
+
27
+ if 'generated' not in st.session_state:
28
+ st.session_state['generated'] = []
29
+
30
+ if 'past' not in st.session_state:
31
+ st.session_state['past'] = []
32
+
33
+
34
+ def query(user_id, prompt, debate_subject, bot_role):
35
+
36
+ history_list = get_lastest_item(
37
+ table=debate_bot_log_table,
38
+ name_of_partition_key="user_id",
39
+ value_of_partition_key=user_id,
40
+ )
41
+
42
+ # history๊ฐ€ ์—†๋‹ค๋ฉด,
43
+ if history_list==[]:
44
+ history = ""
45
+ # history๊ฐ€ ์žˆ๋‹ค๋ฉด, [{}, {}]
46
+ else:
47
+ history = ""
48
+ history_dummy_list = []
49
+ for dic in history_list:
50
+ history_dummy_list.append(dic['prompt'])
51
+
52
+ history = "\n".join(history_dummy_list)
53
+
54
+
55
+ bot_result = debate_bot(prompt, history, debate_subject, bot_role)
56
+
57
+ # put_db
58
+ save_prompt = "\n".join([
59
+ "User: " + prompt,
60
+ "Cicero Bot: " + bot_result
61
+ ])
62
+
63
+ item = {
64
+ 'user_id': user_id,
65
+ 'prompt': save_prompt,
66
+ 'time_stamp': str(time()),
67
+ 'debate_subject': debate_subject,
68
+ }
69
+
70
+ put_item(debate_bot_log_table, item)
71
+
72
+ return bot_result
73
+
74
+ #############################################
75
+ # Setting Form
76
+ #############################################
77
+ if 'debate_topic' not in st.session_state:
78
+ st.session_state.debate_topic = ""
79
+
80
+ with st.form("first_form"):
81
+ #############################################
82
+ # Debate Theme
83
+ #############################################
84
+ debate_theme =st.selectbox(label='Debate Theme', options=[
85
+ 'Education',
86
+ 'Sports',
87
+ 'Religion',
88
+ 'Justice',
89
+ 'Pandemic',
90
+ 'Politics',
91
+ 'Minority',
92
+ 'etc'
93
+ ])
94
+ change = st.form_submit_button("Change")
95
+
96
+ #############################################
97
+ # Debate Topic
98
+ #############################################
99
+ if debate_theme == "Education":
100
+ topic_list = (
101
+ "THBT college entrance examinations should accept students only on the basis of their academic performance in secondary education.",
102
+ "THS a world where the government gives cash that individuals can use to freely select their academic preference (including but not limited to school of choice, private academies, and tutoring) instead of funding for public education.",
103
+ "THW abolish all requirements and evaluation criteria in higher education (i.e., attendance, exams, assignments)."
104
+ )
105
+ elif debate_theme == "Sports":
106
+ topic_list = (
107
+ "THBT having star players for team sports do more harm than good to the team.",
108
+ "THR the emphasis on winning a medal in the Olympics as a core symbol of success.",
109
+ "THP a world where sports serves purely entertainment purposes even at the expense of fair play."
110
+ )
111
+ elif debate_theme == "Religion":
112
+ topic_list = (
113
+ "THW, as a religious group/leader, cease attempts at increasing the number of believers and instead prioritize boosting loyalty amongst adherents to the religion.",
114
+ "Assuming feasibility, TH prefers a world where a panel of church leaders would create a universally accepted interpretation of the Bible that the believers would abide by.",
115
+ "THW aggressively crackdown on megachurches."
116
+ )
117
+ elif debate_theme == "Justice":
118
+ topic_list = (
119
+ "In 2050, AI robots are able to replicate the appearance, conversation, and reaction to emotions of human beings. However, their intelligence still does not allow them to sense emotions and feelings such as pain, happiness, joy, and etc.",
120
+ "In the case a human destroys the robot beyond repair, THW charge murder instead of property damage.",
121
+ "THP a world where the criminal justice systemโ€™s role is mainly for victimโ€™s vengeance. THW allow prosecutors and victims to veto assigned judges."
122
+ )
123
+ elif debate_theme == "Pandemic":
124
+ topic_list = (
125
+ "During a pandemic, THBT businesses that benefit from the pandemic should be additionally taxed.",
126
+ "THW nullify the effect of medical patents in cases of medical emergencies.",
127
+ "THW ban media content that denies the efficacy of the COVID-19 without substantial evidence."
128
+ )
129
+ elif debate_theme == "Politics":
130
+ topic_list = (
131
+ "Info: The Candle Light Will (์ด›๋ถˆ๋ฏผ์‹ฌ) is a term derived from the symbolic candle-light protests for the impeachment of the late president Park Geun Hye, commonly used to mean the peopleโ€™s will to fight against corrupt governments. The Moon administration has frequently referred to the Candle Light Will as the driving force behind its election that grants legitimacy to its policies. THR the โ€˜candle light willโ€™ narrative in the political discourse of South Korea.",
132
+ "THW impose a cap on the property and income of politicians.",
133
+ "THW give the youth extra votes."
134
+ )
135
+ elif debate_theme == "Minority":
136
+ topic_list = (
137
+ "Context: A prominent member of the LGBT movement has discovered that a very influential politician helping the LGBT movement has been lying about their sexual orientation as being gay when they are straight. THW disclose this information.",
138
+ "THBT the LGBTQIA+ movement should denounce the existence of marriage as opposed to fighting for equal marriage rights.",
139
+ "THBT the LGBTQIA+ movement should condemn the consumption of movies and TV shows that cast straight actors/actresses in non-heterosexual identified roles."
140
+ )
141
+ else:
142
+ topic_list = (
143
+ "THW remove all laws that relate to filial responsibilities.",
144
+ "THW require parents to receive approval from experts in relevant fields before making crucial decisions for their children.",
145
+ "Assuming it is possible to measure the โ€˜societal dangerโ€™ of the fetus in the future, THBT the state should raise infants that pose high levels of threat.",
146
+ "THBT any upper limits on prison sentences for particularly heinous crimes should be abolished.",
147
+ "THW require dating apps to anonymize profile pictures.",
148
+ "THW adopt a Pass/Fail grading system for students who suffer from mental health problems (e.g. depression, bipolar disorder, etc.).",
149
+ "THBT South Korean feminist movements should reject feminist icons that are adversarial and embody violence.",
150
+ "THBT freedom of speech should be considered obsolete.",
151
+ "THR the narrative that eccentric personalities are essential to create art.",
152
+ "THW allow parents of severely mentally disabled children to medically impede their children's physical growth.",
153
+ "THR the emphasis on longevity in relationships.",
154
+ "Assuming feasibility, THW choose to continuously relive the happiest moment of oneโ€™s life."
155
+ )
156
+
157
+ debate_topic = st.selectbox(
158
+ '2. Choose your Topic',
159
+ topic_list
160
+ )
161
+
162
+ #############################################
163
+ # Role of Bot
164
+ #############################################
165
+ bot_role_list = (
166
+ "์ฃผ์ œ ์ •์˜",
167
+ "POI ์—ฐ์Šต",
168
+ "์—ญํ•  ์ถ”์ฒœ",
169
+ "์ฃผ์žฅ ๋น„ํŒ",
170
+ "ํ† ๋ก "
171
+ )
172
+
173
+ bot_role = st.selectbox(
174
+ '3. Choose Role of Bot',
175
+ bot_role_list
176
+ )
177
+
178
+ submitted = st.form_submit_button('Send')
179
+
180
+
181
+
182
+ #############################################
183
+ # Chatbot
184
+ #############################################
185
+
186
+ if 'user_id' not in st.session_state:
187
+ st.session_state.user_id = ""
188
+
189
+ if 'debate_subject' not in st.session_state:
190
+ st.session_state.debate_subject = ""
191
+
192
+ with st.form('form', clear_on_submit=True):
193
+ user_id = st.text_input(
194
+ "Enter Your User ID",
195
+ st.session_state.user_id, # For remain the id
196
+ key='user_id'
197
+ )
198
+ user_input = st.text_input(
199
+ 'Message',
200
+ '',
201
+ key='input'
202
+ )
203
+ submitted = st.form_submit_button('Send')
204
+
205
+
206
+ if submitted and user_input:
207
+
208
+ output = query(user_id, user_input, debate_topic, bot_role)
209
+
210
+ st.session_state.past.append(user_input)
211
+ st.session_state.generated.append(output)
212
+
213
+ if st.session_state['generated']:
214
+ for i in range(len(st.session_state['generated'])-1, -1, -1):
215
+ message(
216
+ st.session_state['past'][i],
217
+ is_user=True,
218
+ key=str(i) + '_user'
219
+ )
220
+ message(
221
+ st.session_state["generated"][i],
222
+ key=str(i),
223
+ #avatar_style="Fun Emoji"
224
+ )
app.sh ADDED
@@ -0,0 +1 @@
 
 
1
+ streamlit run app.py
db_modules.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import boto3
3
+ from boto3.dynamodb.conditions import Key
4
+ import os
5
+ from dotenv import dotenv_values
6
+
7
+ config = dotenv_values(".env")
8
+
9
+ AWS_ACCESS_KEY_ID = config.get('AWS_ACCESS_KEY_ID')
10
+ AWS_SECRET_ACCESS_KEY = config.get('AWS_SECRET_ACCESS_KEY')
11
+
12
+ # AWS_ACCESS_KEY_ID = os.environ['AWS_ACCESS_KEY_ID']
13
+ # AWS_SECRET_ACCESS_KEY = os.environ['AWS_SECRET_ACCESS_KEY']
14
+
15
+
16
+ def get_db():
17
+
18
+ dynamodb = boto3.resource(
19
+ 'dynamodb',
20
+ region_name='ap-northeast-2',
21
+ aws_access_key_id=AWS_ACCESS_KEY_ID,
22
+ aws_secret_access_key=AWS_SECRET_ACCESS_KEY,
23
+ )
24
+ print("dynamodb connected", dynamodb)
25
+
26
+ return dynamodb
27
+
28
+ # put_item
29
+ def put_item(table, item):
30
+
31
+ print("item", item)
32
+
33
+ response = table.put_item(Item=item)
34
+
35
+ if response['ResponseMetadata']['HTTPStatusCode'] == 200:
36
+ print('Item successfully inserted')
37
+ else:
38
+ print('Error inserting item')
39
+
40
+ # get_item
41
+ def get_item(table, item):
42
+
43
+ response = table.get_item(Key=item)
44
+
45
+ if 'Item' in response:
46
+ print('Item successfully retrieved')
47
+ return response['Item']
48
+ else:
49
+ print('Item not found')
50
+ return None
51
+
52
+ def get_lastest_item(table, name_of_partition_key, value_of_partition_key):
53
+
54
+ response = table.query (
55
+ KeyConditionExpression=Key(name_of_partition_key).eq(value_of_partition_key),
56
+ ScanIndexForward=True,
57
+ Limit=10 # 1
58
+ )
59
+
60
+ return response['Items']
debate_bot.py ADDED
@@ -0,0 +1,42 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from langchain.prompts import PromptTemplate
3
+ from gpt_modules import gpt_call
4
+
5
+ def debate_bot(prompt, history="", debate_subject="", bot_role=""):
6
+
7
+ if history=="":
8
+ bot_persona = "\n".join([
9
+ 'You are name is Cicero Bot.',
10
+ 'You have to debate with User.',
11
+ 'You have to say about your opinion and evidence.',
12
+ 'You have to say logically.'
13
+ ])
14
+ few_shot_prompt = "\n".join([
15
+ ""
16
+ ])
17
+ else:
18
+ bot_persona = ""
19
+ few_shot_prompt = ""
20
+
21
+ debate_subject = debate_subject
22
+
23
+ # ์š”์•ฝํ•œ ๋‚ด์šฉ์„ ์นœ์ ˆํ•˜๊ฒŒ ์„ค๋ช…ํ•ด์ฃผ๋Š”
24
+ dialog_prompt_template = PromptTemplate(
25
+ input_variables=["prompt"],
26
+ template="\n".join([
27
+ bot_persona, #persona
28
+ few_shot_prompt,
29
+ "Debate Subject: " + debate_subject,
30
+ history,
31
+ "User: {prompt}",
32
+ "Cicero Bot: "
33
+ ])
34
+ )
35
+
36
+ # ๋งํˆฌ ๋ณ€๊ฒฝ
37
+ dduru_prompt = dialog_prompt_template.format(
38
+ prompt=prompt
39
+ )
40
+ dduru_result = gpt_call(dduru_prompt)
41
+
42
+ return dduru_result
gpt_modules.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ import os
3
+
4
+ from dotenv import dotenv_values
5
+
6
+ config = dotenv_values(".env")
7
+
8
+ openai.organization = config.get('OPENAI_ORGANIZATION')
9
+ openai.api_key = config.get('OPENAI_API_KEY')
10
+
11
+ # openai.organization = os.environ['OPENAI_ORGANIZATION']
12
+ # openai.api_key = os.environ['OPENAI_API_KEY']
13
+
14
+ def gpt_call(prompt):
15
+ response = openai.ChatCompletion.create(
16
+ model="gpt-3.5-turbo",
17
+ messages=[
18
+ {"role": "user", "content": prompt},
19
+ ]
20
+ )
21
+ output_text = response["choices"][0]["message"]["content"]
22
+
23
+ return output_text
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit_chat
2
+ boto3
3
+ openai
4
+ langchain