Unggi commited on
Commit
5f3392e
1 Parent(s): 9d904f1

add vocal_app.py

Browse files
Files changed (5) hide show
  1. README.md +5 -12
  2. bots/debate_bot.py +3 -1
  3. bots/one_to_one_debate.py +398 -0
  4. requirements.txt +2 -1
  5. vocal_app.py +24 -0
README.md CHANGED
@@ -1,13 +1,6 @@
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
 
2
+
3
+ # ffmpeg
4
+
5
+ apt-get update
6
+ apt-get ffmpeg
bots/debate_bot.py CHANGED
@@ -4,6 +4,7 @@ from langchain.prompts import PromptTemplate
4
  from modules.gpt_modules import gpt_call
5
 
6
  from .normal_debate import nomal_debator
 
7
 
8
 
9
  #############################################
@@ -12,7 +13,8 @@ from .normal_debate import nomal_debator
12
  def debate_bot(prompt, history="", debate_subject="", bot_role="", history_num=0):
13
 
14
  if bot_role == "토론":
15
- bot_response = nomal_debator(prompt, history, debate_subject, bot_role, history_num)
 
16
  elif bot_role == "주제 정의":
17
  pass
18
  elif bot_role == "POI 연습":
 
4
  from modules.gpt_modules import gpt_call
5
 
6
  from .normal_debate import nomal_debator
7
+ from .one_to_one_debate import one_to_one_debator
8
 
9
 
10
  #############################################
 
13
  def debate_bot(prompt, history="", debate_subject="", bot_role="", history_num=0):
14
 
15
  if bot_role == "토론":
16
+ #bot_response = nomal_debator(prompt, history, debate_subject, bot_role, history_num)
17
+ bot_response = one_to_one_debator(prompt, history, debate_subject, bot_role, history_num)
18
  elif bot_role == "주제 정의":
19
  pass
20
  elif bot_role == "POI 연습":
bots/one_to_one_debate.py ADDED
@@ -0,0 +1,398 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+
3
+
4
+ import re
5
+ import random
6
+ from langchain.prompts import PromptTemplate
7
+ from modules.gpt_modules import gpt_call
8
+
9
+ def erase_start_word_and_after(text, start_word):
10
+ pattern = re.compile(re.escape(start_word) + '.*')
11
+ return re.sub(pattern, '', text)
12
+
13
+ def one_to_one_debator(prompt, history, debate_subject, bot_role, history_num):
14
+ # Debate Rule 설명하기
15
+ if history_num == 0:
16
+ print("history_num", history_num)
17
+
18
+ user_role = ""
19
+ bot_response = ""
20
+
21
+ debate_role = [
22
+ "pro side",
23
+ "con side"
24
+ ]
25
+
26
+ # user role random으로 정하기
27
+ user_debate_role = random.choice(debate_role)
28
+ # user role이 아닌 것이 bot의 role임
29
+ bot_debate_role_list = [role for role in debate_role if role != user_debate_role]
30
+
31
+ print("user_debate_role", user_debate_role)
32
+ print("bot_debate_role_list", bot_debate_role_list)
33
+
34
+ debate_preset = "\n".join([
35
+ "Debate Rules: ",
36
+ "1) This debate will be divided into two teams, pro and con, with two debates on each team.",
37
+ "2) The order of speaking is: first debater for the pro side, first debater for the con side, second debater for the pro side, second debater for the con side.",
38
+ "3) Answer logically with an introduction, body, and conclusion.\n", #add this one.
39
+ "User debate role: " + user_debate_role,
40
+ "Bot debate roles: " + ", ".join(bot_debate_role_list) + "\n",
41
+ "Debate subject: " + debate_subject
42
+ ])
43
+
44
+ # User가 첫번째 차례라면, User에게 먼저 prompt를 받아야 함
45
+ if user_debate_role == debate_role[0]:
46
+ #print("user_debate_role", user_debate_role)
47
+ bot_preset = "\n".join([
48
+ debate_preset + "\n",
49
+ "It's your turn! Write your opinion!"
50
+ ])
51
+ bot_response = bot_preset
52
+ print("bot_response", bot_response)
53
+ #return bot_response
54
+
55
+ # User가 두번째 차례라면, Bot이 1번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
56
+ elif user_debate_role == debate_role[1]:
57
+
58
+ bot_preset = "\n".join([
59
+ debate_preset,
60
+ ])
61
+
62
+ first_prompt_template = PromptTemplate(
63
+ input_variables=["prompt"],
64
+ template="\n".join([
65
+ bot_preset, #persona
66
+ "{prompt}",
67
+ "Only say " + debate_role[0] + "\'s opinion after \':\'. Do not write " + debate_role[1] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
68
+ debate_role[0] + ": "
69
+ ])
70
+ )
71
+ first_bot_prompt = first_prompt_template.format(
72
+ prompt=""
73
+ )
74
+ first_response = gpt_call(first_bot_prompt)
75
+
76
+ # preprocess
77
+ # if first_response contain the first debater for the con side's opinion, remove it.
78
+ first_response = erase_start_word_and_after(first_response, debate_role[1])
79
+ first_response = erase_start_word_and_after(first_response, debate_role[2])
80
+ first_response = erase_start_word_and_after(first_response, debate_role[3])
81
+
82
+ #first_response = re.sub(debate_role[1] + ":.*", "", first_response)
83
+
84
+ bot_response = "\n".join([
85
+ bot_preset + "\n",
86
+ "-----------------------------------------------------------------",
87
+ "[First debater for the pro side]: " + "\n" + first_response + "\n",
88
+ "-----------------------------------------------------------------",
89
+ "It's your turn! Write your opinion!"
90
+ ])
91
+
92
+ # User가 세번째 차례라면, Bot이 1, 2번째 차례에 대한 response를 만들고, 사용자의 답변을 받아야 함
93
+ elif user_debate_role == debate_role[2]:
94
+
95
+ bot_preset = "\n".join([
96
+ debate_preset,
97
+ ])
98
+ # first
99
+ first_prompt_template = PromptTemplate(
100
+ input_variables=["prompt"],
101
+ template="\n".join([
102
+ bot_preset, #persona
103
+ "{prompt}",
104
+ debate_role[0] + ": ",
105
+ ])
106
+ )
107
+ first_bot_prompt = first_prompt_template.format(
108
+ prompt=""
109
+ )
110
+ first_response = gpt_call(first_bot_prompt)
111
+
112
+ # second
113
+ second_prompt_template = PromptTemplate(
114
+ input_variables=["first_prompt"],
115
+ template="\n".join([
116
+ bot_preset, #persona
117
+ "Only say " + debate_role[1] + "\'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
118
+ debate_role[0] + ": " + "{first_prompt}",
119
+ debate_role[1] + ": "
120
+ ])
121
+ )
122
+ second_bot_prompt = second_prompt_template.format(
123
+ first_prompt=first_response
124
+ )
125
+ second_response = gpt_call(second_bot_prompt)
126
+
127
+ # preprocess
128
+ # if first_response contain the first debater for the con side's opinion, remove it.
129
+ first_response = erase_start_word_and_after(first_response, debate_role[1])
130
+ first_response = erase_start_word_and_after(first_response, debate_role[2])
131
+ first_response = erase_start_word_and_after(first_response, debate_role[3])
132
+ # if second_response contain the first debater for the con side's opinion, remove it.
133
+ #second_response = re.sub(debate_role[2] + ":.*", "", second_response)
134
+ second_response = erase_start_word_and_after(second_response, debate_role[2])
135
+ second_response = erase_start_word_and_after(second_response, debate_role[3])
136
+
137
+ bot_response = "\n".join([
138
+ bot_preset + "\n",
139
+ "-----------------------------------------------------------------",
140
+ "[First debater for the pro side]: " + "\n" + first_response + "\n",
141
+ "-----------------------------------------------------------------",
142
+ "[First debater for the con side]: " + "\n" + second_response + "\n",
143
+ "-----------------------------------------------------------------",
144
+ "It's your turn! Write your opinion!"
145
+ ])
146
+
147
+
148
+ elif user_debate_role == debate_role[3]:
149
+
150
+ bot_preset = "\n".join([
151
+ debate_preset,
152
+ ])
153
+
154
+ # first
155
+ first_prompt_template = PromptTemplate(
156
+ input_variables=["prompt"],
157
+ template="\n".join([
158
+ bot_preset, #persona
159
+ "{prompt}",
160
+ debate_role[0] + ": ",
161
+ ])
162
+ )
163
+ first_bot_prompt = first_prompt_template.format(
164
+ prompt=""
165
+ )
166
+ first_response = gpt_call(first_bot_prompt)
167
+
168
+ # second
169
+ second_prompt_template = PromptTemplate(
170
+ input_variables=["first_prompt"],
171
+ template="\n".join([
172
+ bot_preset, #persona
173
+ "Only say " + debate_role[1] + "'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[2] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
174
+ debate_role[0] + ": " + "{first_prompt}",
175
+ debate_role[1] + ": "
176
+ ])
177
+ )
178
+ second_bot_prompt = second_prompt_template.format(
179
+ first_prompt=first_response
180
+ )
181
+ second_response = gpt_call(second_bot_prompt)
182
+
183
+ # third
184
+ third_prompt_template = PromptTemplate(
185
+ input_variables=["first_prompt", "second_prompt"],
186
+ template="\n".join([
187
+ bot_preset, #persona
188
+ "Only say " + debate_role[2] + "\'s opinion after \':\'. Do not write " + debate_role[0] + "\'s " + "opinions, " + debate_role[1] + "\'s " + "opinions and " + debate_role[3] + "\'s " + "opinions.",
189
+ debate_role[0] + ": " + "{first_prompt}",
190
+ debate_role[1] + ": " + "{second_prompt}",
191
+ debate_role[2] + ": "
192
+ ])
193
+ )
194
+ third_bot_prompt = third_prompt_template.format(
195
+ first_prompt=first_response,
196
+ second_prompt=second_response
197
+ )
198
+ third_response = gpt_call(third_bot_prompt)
199
+
200
+ # preprocess
201
+ # if first_response contain the first debater for the con side's opinion, remove it.
202
+ first_response = erase_start_word_and_after(first_response, debate_role[1])
203
+ first_response = erase_start_word_and_after(first_response, debate_role[2])
204
+ first_response = erase_start_word_and_after(first_response, debate_role[3])
205
+ # if second_response contain the first debater for the con side's opinion, remove it.
206
+ #second_response = re.sub(debate_role[2] + ":.*", "", second_response)
207
+ second_response = erase_start_word_and_after(second_response, debate_role[2])
208
+ second_response = erase_start_word_and_after(second_response, debate_role[3])
209
+ # if third_response contain the first debater for the con side's opinion, remove it.
210
+ thir_response = erase_start_word_and_after(thir_response, debate_role[3])
211
+ #third_response = re.sub(debate_role[3] + ":.*", "", third_response)
212
+
213
+ bot_response = "\n".join([
214
+ bot_preset + "\n",
215
+ "-----------------------------------------------------------------",
216
+ "[First debater for the pro side]: " + "\n" + first_response + "\n",
217
+ "-----------------------------------------------------------------",
218
+ "[First debater for the con side]: " + "\n" + second_response + "\n",
219
+ "-----------------------------------------------------------------",
220
+ "[Second debater for the pro side]: " + "\n" + third_response + "\n",
221
+ "-----------------------------------------------------------------",
222
+ "It's your turn! Write your opinion!"
223
+ ])
224
+ else:
225
+ pass
226
+
227
+ # Answer and Ask Judgement.
228
+ if history_num == 1:
229
+
230
+ debate_role = [
231
+ "first debater for the pro side",
232
+ "first debater for the con side",
233
+ "second debater for the pro side",
234
+ "second debater for the con side"
235
+ ]
236
+
237
+ print("history1: ", history)
238
+
239
+ # user가 가장 첫번째로 답변했다면, 봇이 2, 3, 4 답변을 하고, 평가할지를 물어보면 됨.
240
+ if "User debate role: first debater for the pro side" in history:
241
+
242
+ # second
243
+ second_prompt_template = PromptTemplate(
244
+ input_variables=["prompt"],
245
+ template="\n".join([
246
+ history,
247
+ "User: {prompt}",
248
+ debate_role[2] + ": "
249
+ ])
250
+ )
251
+ second_bot_prompt = second_prompt_template.format(
252
+ prompt=prompt
253
+ )
254
+ second_response = gpt_call(second_bot_prompt)
255
+
256
+
257
+ # third
258
+ third_prompt_template = PromptTemplate(
259
+ input_variables=["prompt"],
260
+ template="\n".join([
261
+ history,
262
+ "User: {prompt}",
263
+ debate_role[2] + ": "
264
+ ])
265
+ )
266
+ third_bot_prompt = third_prompt_template.format(
267
+ prompt=prompt
268
+ )
269
+ third_response = gpt_call(third_bot_prompt)
270
+
271
+ # fourth
272
+ fourth_prompt_template = PromptTemplate(
273
+ input_variables=["prompt"],
274
+ template="\n".join([
275
+ history,
276
+ "User: {prompt}",
277
+ debate_role[3] + ": "
278
+ ])
279
+ )
280
+ fourth_bot_prompt = fourth_prompt_template.format(
281
+ prompt=prompt
282
+ )
283
+ fourth_response = gpt_call(fourth_bot_prompt)
284
+
285
+ ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
286
+ bot_response = "\n".join([
287
+ "[first debater for the con side]: " + "\n" + second_response + "\n",
288
+ "-----------------------------------------------------------------",
289
+ "[second debater for the pro sid]: " + "\n" + third_response + "\n",
290
+ "-----------------------------------------------------------------",
291
+ "[second debater for the con side]: " + "\n" + fourth_response + "\n",
292
+ "-----------------------------------------------------------------",
293
+ ask_judgement
294
+ ])
295
+
296
+ # user가 두번째로 답변했다면, 봇이 3, 4 번째 답변을 하고, 평가할지를 물어보면 됨.
297
+ elif "User debate role: first debater for the con side" in history:
298
+
299
+ # third
300
+ third_prompt_template = PromptTemplate(
301
+ input_variables=["prompt"],
302
+ template="\n".join([
303
+ history,
304
+ "User: {prompt}",
305
+ debate_role[2] + ": "
306
+ ])
307
+ )
308
+ third_bot_prompt = third_prompt_template.format(
309
+ prompt=prompt
310
+ )
311
+ third_response = gpt_call(third_bot_prompt)
312
+
313
+ # fourth
314
+ fourth_prompt_template = PromptTemplate(
315
+ input_variables=["prompt"],
316
+ template="\n".join([
317
+ history,
318
+ "User: {prompt}",
319
+ debate_role[2] + ": " + third_response,
320
+ debate_role[3] + ": "
321
+ ])
322
+ )
323
+ fourth_bot_prompt = fourth_prompt_template.format(
324
+ prompt=prompt
325
+ )
326
+ fourth_response = gpt_call(fourth_bot_prompt)
327
+
328
+ # ask_judgement
329
+ ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
330
+ bot_response = "\n".join([
331
+ "[second debater for the pro sid]: " + "\n" + third_response + "\n",
332
+ "-----------------------------------------------------------------",
333
+ "[second debater for the con side]: " + "\n" + fourth_response + "\n",
334
+ "-----------------------------------------------------------------",
335
+ ask_judgement
336
+ ])
337
+
338
+ # user가 세번째로 답변했다면, 봇��� 4 번째 답변을 하고, 평가할지를 물어보면 됨.
339
+ elif "User debate role: second debater for the pro side" in history:
340
+
341
+ fourth_prompt_template = PromptTemplate(
342
+ input_variables=["prompt"],
343
+ template="\n".join([
344
+ history,
345
+ "User: {prompt}",
346
+ debate_role[3] + ": "
347
+ ])
348
+ )
349
+ fourth_bot_prompt = fourth_prompt_template.format(
350
+ prompt=prompt
351
+ )
352
+ fourth_response = gpt_call(fourth_bot_prompt)
353
+
354
+
355
+
356
+ ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
357
+ bot_response = "\n".join([
358
+ "[second debater for the con side]: " + "\n" + fourth_response + "\n",
359
+ "-----------------------------------------------------------------",
360
+ ask_judgement
361
+ ])
362
+
363
+ # user가 네번째로 답변했다면, 바로 평가할지를 물어보면 됨.
364
+ elif "User debate role: second debater for the con side" in history:
365
+ ask_judgement = "Do you want to be the judge of this debate? (If you want, enter any words.)"
366
+ bot_response = ask_judgement
367
+ else:
368
+ pass
369
+
370
+ # Judgement.
371
+ if history_num == 2:
372
+ judgement_word_list = "\n".join([
373
+ "!!Instruction!",
374
+ "You are now the judge of this debate. Evaluate the debate according to the rules below.",
375
+ "Rule 1. Decide between the pro and con teams.",
376
+ "Rule 2. Summarize the debate as a whole and what each debater said.",
377
+ "Rule 3. For each debater, explain what was persuasive and what made the differnce between winning and losing.",
378
+ ])
379
+
380
+ judgement_prompt_template = PromptTemplate(
381
+ input_variables=["prompt"],
382
+ template="\n".join([
383
+ history,
384
+ "{prompt}",
385
+ judgement_word_list,
386
+ "Judgement: "
387
+ ])
388
+ )
389
+ judgement_bot_prompt = judgement_prompt_template.format(
390
+ prompt=""
391
+ )
392
+ judgement_response = gpt_call(judgement_bot_prompt)
393
+
394
+ bot_response = "\n".join([
395
+ "[Judgement]: " + "\n" + judgement_response + "\n",
396
+ ])
397
+
398
+ return bot_response
requirements.txt CHANGED
@@ -2,4 +2,5 @@ streamlit_chat
2
  boto3
3
  openai
4
  langchain
5
- python-dotenv
 
 
2
  boto3
3
  openai
4
  langchain
5
+ python-dotenv
6
+ ffmpeg-python
vocal_app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from transformers import pipeline
3
+ import gradio as gr
4
+ import time
5
+
6
+ p = pipeline("automatic-speech-recognition")
7
+
8
+ def transcribe(audio, state=""):
9
+ time.sleep(2)
10
+ text = p(audio)["text"]
11
+ state += text + " "
12
+ return state, state
13
+
14
+ gr.Interface(
15
+ fn=transcribe,
16
+ inputs=[
17
+ gr.Audio(source="microphone", type="filepath", streaming=True),
18
+ "state"
19
+ ],
20
+ outputs=[
21
+ "textbox",
22
+ "state"
23
+ ],
24
+ live=True).launch()