RithwikG commited on
Commit
5c2a135
·
1 Parent(s): a95dbb0

fixed chat

Browse files
.history/app_20240731171136.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Get a simplified summary with step-by-step observations to solve the problem, and chat with the bot about questions you have about the editorial.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+ """)
46
+
47
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
48
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
49
+ msg = gr.Textbox(label = "Chat with the bot")
50
+ # clear = gr.ClearButton([msg, interface])
51
+ state = gr.State()
52
+
53
+
54
+ def cp_helper(message, history, state):
55
+ global chats, last_update
56
+ current_time = time.time()
57
+ day = 24 * 60 * 60
58
+ if (current_time - last_update >= day):
59
+ last_update=current_time
60
+ chats=1000
61
+
62
+ if (chats <= 0):
63
+ history.append((message, "Too many chats today, try again later."))
64
+ return "", history, state
65
+
66
+ chats-=1
67
+
68
+ global ind
69
+ if (state is None):
70
+
71
+ # state = {'chatgpt': None,
72
+ # 'problem_url': None,
73
+ # 'edi_url' : None,
74
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
75
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
76
+ # 'first' : True,
77
+ # 'style' : 'hint'}
78
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
79
+ # print('Edi', state['edi_text'])
80
+ # assert(False)
81
+ # state = {'chatgpt': None,
82
+ # 'problem_url': None,
83
+ # 'edi_url' : None,
84
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
85
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
86
+ # 'first' : True,
87
+ # 'style' : 'hint'}
88
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
89
+ # print(state['problem_text'], state['edi_text'])
90
+ ind = random.randint(1, 100000)
91
+ state = {'chatgpt': None,
92
+ 'problem_url': None,
93
+ 'edi_url' : None,
94
+ 'problem_text' : '',
95
+ 'edi_text' : '',
96
+ 'first' : True,
97
+ 'style' : None,
98
+ 'id': '#' + str(ind)}
99
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
100
+ ind += 1
101
+ bot = ""
102
+ if (state['problem_text'] == ''):
103
+ try:
104
+ state['problem_url'] = message
105
+ if ('usaco' in message.lower()):
106
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
107
+ elif ('codeforces' in message.lower()):
108
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
109
+ else:
110
+ assert(False)
111
+
112
+ bot = "Thanks! Now could you provide a link to the editorial."
113
+ print(state['problem_text'])
114
+ except Exception as e:
115
+ print(e)
116
+ bot = "There was something wrong with the link. Could you paste it again?"
117
+ elif (state['edi_text'] == ''):
118
+ try:
119
+ state['edi_url'] = message
120
+ if ('usaco' in message.lower()):
121
+ # cbot = Chat(observation_generation)
122
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
123
+ elif ('codeforces' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
126
+ else:
127
+ assert(False)
128
+
129
+ assert(len(state['edi_text']))
130
+ print('EDITORIAL\n', state['edi_text'])
131
+ state['first']=True
132
+
133
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
134
+
135
+ except Exception as e:
136
+ print(e)
137
+ bot = "There was something wrong with the link. Could you paste it again?"
138
+
139
+ elif (state['style'] == None):
140
+ message = message.lower()
141
+ if ('editorial' in message):
142
+ state['style'] = 'editorial'
143
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
144
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
145
+ elif ('hint' in message):
146
+ state['style'] = 'hint'
147
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
148
+ bot += "What are you current thoughts on the problem? "
149
+ else:
150
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
151
+
152
+
153
+
154
+ if (len(bot) == 0):
155
+ if (state['style'] == 'hint'):
156
+ if (state['first']):
157
+ state['first']=False
158
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
159
+ else:
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
161
+ else:
162
+ state['first']=False
163
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
164
+
165
+
166
+ history.append((message, bot))
167
+
168
+ chat_history = ''
169
+ for i in history:
170
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
171
+
172
+ with open(CHAT_PATH + state['id'], 'w') as f:
173
+ f.write(chat_history)
174
+
175
+ return "", history, state
176
+
177
+
178
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
179
+
180
+ def sub_rating(state, option):
181
+ if (state['id'] == None):
182
+ return
183
+ with open(CHAT_PATH + 'meta', 'a') as f:
184
+ f.write(f"{state['id']}: {option}\n")
185
+
186
+ return state, "Choose an Option"
187
+
188
+ rating = gr.Dropdown([
189
+ "Choose an Option",
190
+ "1 - Very Unhelpful",
191
+ "2 - Unhelpful",
192
+ "3 - Neutral",
193
+ "4 - Helpful",
194
+ "5 - Very Helpful"
195
+ ], value = "Choose an Option", label='Rate the Conversation')
196
+
197
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
198
+
199
+ def sub_feedback(state, message):
200
+ if (state['id'] == None):
201
+ return
202
+ with open(CHAT_PATH + state['id'], 'a') as f:
203
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
204
+ with open(CHAT_PATH + 'feedback', 'a') as f:
205
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
206
+
207
+ return state, ""
208
+
209
+ feedback = gr.Textbox(label = "Submit Feedback")
210
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
211
+
212
+
213
+
214
+
215
+ demo.launch()
.history/app_20240731171138.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Get a simplified summary with step-by-step observations to solve the problem, and chat with the bot about questions you have about the editorial.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+ """)
46
+
47
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
48
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
49
+ msg = gr.Textbox(label = "Chat with the bot")
50
+ # clear = gr.ClearButton([msg, interface])
51
+ state = gr.State()
52
+
53
+
54
+ def cp_helper(message, history, state):
55
+ global chats, last_update
56
+ current_time = time.time()
57
+ day = 24 * 60 * 60
58
+ if (current_time - last_update >= day):
59
+ last_update=current_time
60
+ chats=1000
61
+
62
+ if (chats <= 0):
63
+ history.append((message, "Too many chats today, try again later."))
64
+ return "", history, state
65
+
66
+ chats-=1
67
+
68
+ global ind
69
+ if (state is None):
70
+
71
+ # state = {'chatgpt': None,
72
+ # 'problem_url': None,
73
+ # 'edi_url' : None,
74
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
75
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
76
+ # 'first' : True,
77
+ # 'style' : 'hint'}
78
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
79
+ # print('Edi', state['edi_text'])
80
+ # assert(False)
81
+ # state = {'chatgpt': None,
82
+ # 'problem_url': None,
83
+ # 'edi_url' : None,
84
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
85
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
86
+ # 'first' : True,
87
+ # 'style' : 'hint'}
88
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
89
+ # print(state['problem_text'], state['edi_text'])
90
+ ind = random.randint(1, 100000)
91
+ state = {'chatgpt': None,
92
+ 'problem_url': None,
93
+ 'edi_url' : None,
94
+ 'problem_text' : '',
95
+ 'edi_text' : '',
96
+ 'first' : True,
97
+ 'style' : None,
98
+ 'id': '#' + str(ind)}
99
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
100
+ ind += 1
101
+ bot = ""
102
+ if (state['problem_text'] == ''):
103
+ try:
104
+ state['problem_url'] = message
105
+ if ('usaco' in message.lower()):
106
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
107
+ elif ('codeforces' in message.lower()):
108
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
109
+ else:
110
+ assert(False)
111
+
112
+ bot = "Thanks! Now could you provide a link to the editorial."
113
+ print(state['problem_text'])
114
+ except Exception as e:
115
+ print(e)
116
+ bot = "There was something wrong with the link. Could you paste it again?"
117
+ elif (state['edi_text'] == ''):
118
+ try:
119
+ state['edi_url'] = message
120
+ if ('usaco' in message.lower()):
121
+ # cbot = Chat(observation_generation)
122
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
123
+ elif ('codeforces' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
126
+ else:
127
+ assert(False)
128
+
129
+ assert(len(state['edi_text']))
130
+ print('EDITORIAL\n', state['edi_text'])
131
+ state['first']=True
132
+
133
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
134
+
135
+ except Exception as e:
136
+ print(e)
137
+ bot = "There was something wrong with the link. Could you paste it again?"
138
+
139
+ elif (state['style'] == None):
140
+ message = message.lower()
141
+ if ('editorial' in message):
142
+ state['style'] = 'editorial'
143
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
144
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
145
+ elif ('hint' in message):
146
+ state['style'] = 'hint'
147
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
148
+ bot += "What are you current thoughts on the problem? "
149
+ else:
150
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
151
+
152
+
153
+
154
+ if (len(bot) == 0):
155
+ if (state['style'] == 'hint'):
156
+ if (state['first']):
157
+ state['first']=False
158
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
159
+ else:
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
161
+ else:
162
+ state['first']=False
163
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
164
+
165
+
166
+ history.append((message, bot))
167
+
168
+ chat_history = ''
169
+ for i in history:
170
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
171
+
172
+ with open(CHAT_PATH + state['id'], 'w') as f:
173
+ f.write(chat_history)
174
+
175
+ return "", history, state
176
+
177
+
178
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
179
+
180
+ def sub_rating(state, option):
181
+ if (state['id'] == None):
182
+ return
183
+ with open(CHAT_PATH + 'meta', 'a') as f:
184
+ f.write(f"{state['id']}: {option}\n")
185
+
186
+ return state, "Choose an Option"
187
+
188
+ rating = gr.Dropdown([
189
+ "Choose an Option",
190
+ "1 - Very Unhelpful",
191
+ "2 - Unhelpful",
192
+ "3 - Neutral",
193
+ "4 - Helpful",
194
+ "5 - Very Helpful"
195
+ ], value = "Choose an Option", label='Rate the Conversation')
196
+
197
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
198
+
199
+ def sub_feedback(state, message):
200
+ if (state['id'] == None):
201
+ return
202
+ with open(CHAT_PATH + state['id'], 'a') as f:
203
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
204
+ with open(CHAT_PATH + 'feedback', 'a') as f:
205
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
206
+
207
+ return state, ""
208
+
209
+ feedback = gr.Textbox(label = "Submit Feedback")
210
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
211
+
212
+
213
+
214
+
215
+ demo.launch()
.history/app_20240731171140.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms is coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Get a simplified summary with step-by-step observations to solve the problem, and chat with the bot about questions you have about the editorial.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+ """)
46
+
47
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
48
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
49
+ msg = gr.Textbox(label = "Chat with the bot")
50
+ # clear = gr.ClearButton([msg, interface])
51
+ state = gr.State()
52
+
53
+
54
+ def cp_helper(message, history, state):
55
+ global chats, last_update
56
+ current_time = time.time()
57
+ day = 24 * 60 * 60
58
+ if (current_time - last_update >= day):
59
+ last_update=current_time
60
+ chats=1000
61
+
62
+ if (chats <= 0):
63
+ history.append((message, "Too many chats today, try again later."))
64
+ return "", history, state
65
+
66
+ chats-=1
67
+
68
+ global ind
69
+ if (state is None):
70
+
71
+ # state = {'chatgpt': None,
72
+ # 'problem_url': None,
73
+ # 'edi_url' : None,
74
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
75
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
76
+ # 'first' : True,
77
+ # 'style' : 'hint'}
78
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
79
+ # print('Edi', state['edi_text'])
80
+ # assert(False)
81
+ # state = {'chatgpt': None,
82
+ # 'problem_url': None,
83
+ # 'edi_url' : None,
84
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
85
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
86
+ # 'first' : True,
87
+ # 'style' : 'hint'}
88
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
89
+ # print(state['problem_text'], state['edi_text'])
90
+ ind = random.randint(1, 100000)
91
+ state = {'chatgpt': None,
92
+ 'problem_url': None,
93
+ 'edi_url' : None,
94
+ 'problem_text' : '',
95
+ 'edi_text' : '',
96
+ 'first' : True,
97
+ 'style' : None,
98
+ 'id': '#' + str(ind)}
99
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
100
+ ind += 1
101
+ bot = ""
102
+ if (state['problem_text'] == ''):
103
+ try:
104
+ state['problem_url'] = message
105
+ if ('usaco' in message.lower()):
106
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
107
+ elif ('codeforces' in message.lower()):
108
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
109
+ else:
110
+ assert(False)
111
+
112
+ bot = "Thanks! Now could you provide a link to the editorial."
113
+ print(state['problem_text'])
114
+ except Exception as e:
115
+ print(e)
116
+ bot = "There was something wrong with the link. Could you paste it again?"
117
+ elif (state['edi_text'] == ''):
118
+ try:
119
+ state['edi_url'] = message
120
+ if ('usaco' in message.lower()):
121
+ # cbot = Chat(observation_generation)
122
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
123
+ elif ('codeforces' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
126
+ else:
127
+ assert(False)
128
+
129
+ assert(len(state['edi_text']))
130
+ print('EDITORIAL\n', state['edi_text'])
131
+ state['first']=True
132
+
133
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
134
+
135
+ except Exception as e:
136
+ print(e)
137
+ bot = "There was something wrong with the link. Could you paste it again?"
138
+
139
+ elif (state['style'] == None):
140
+ message = message.lower()
141
+ if ('editorial' in message):
142
+ state['style'] = 'editorial'
143
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
144
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
145
+ elif ('hint' in message):
146
+ state['style'] = 'hint'
147
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
148
+ bot += "What are you current thoughts on the problem? "
149
+ else:
150
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
151
+
152
+
153
+
154
+ if (len(bot) == 0):
155
+ if (state['style'] == 'hint'):
156
+ if (state['first']):
157
+ state['first']=False
158
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
159
+ else:
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
161
+ else:
162
+ state['first']=False
163
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
164
+
165
+
166
+ history.append((message, bot))
167
+
168
+ chat_history = ''
169
+ for i in history:
170
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
171
+
172
+ with open(CHAT_PATH + state['id'], 'w') as f:
173
+ f.write(chat_history)
174
+
175
+ return "", history, state
176
+
177
+
178
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
179
+
180
+ def sub_rating(state, option):
181
+ if (state['id'] == None):
182
+ return
183
+ with open(CHAT_PATH + 'meta', 'a') as f:
184
+ f.write(f"{state['id']}: {option}\n")
185
+
186
+ return state, "Choose an Option"
187
+
188
+ rating = gr.Dropdown([
189
+ "Choose an Option",
190
+ "1 - Very Unhelpful",
191
+ "2 - Unhelpful",
192
+ "3 - Neutral",
193
+ "4 - Helpful",
194
+ "5 - Very Helpful"
195
+ ], value = "Choose an Option", label='Rate the Conversation')
196
+
197
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
198
+
199
+ def sub_feedback(state, message):
200
+ if (state['id'] == None):
201
+ return
202
+ with open(CHAT_PATH + state['id'], 'a') as f:
203
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
204
+ with open(CHAT_PATH + 'feedback', 'a') as f:
205
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
206
+
207
+ return state, ""
208
+
209
+ feedback = gr.Textbox(label = "Submit Feedback")
210
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
211
+
212
+
213
+
214
+
215
+ demo.launch()
.history/app_20240731171145.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Get a simplified summary with step-by-step observations to solve the problem, and chat with the bot about questions you have about the editorial.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+ """)
46
+
47
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
48
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
49
+ msg = gr.Textbox(label = "Chat with the bot")
50
+ # clear = gr.ClearButton([msg, interface])
51
+ state = gr.State()
52
+
53
+
54
+ def cp_helper(message, history, state):
55
+ global chats, last_update
56
+ current_time = time.time()
57
+ day = 24 * 60 * 60
58
+ if (current_time - last_update >= day):
59
+ last_update=current_time
60
+ chats=1000
61
+
62
+ if (chats <= 0):
63
+ history.append((message, "Too many chats today, try again later."))
64
+ return "", history, state
65
+
66
+ chats-=1
67
+
68
+ global ind
69
+ if (state is None):
70
+
71
+ # state = {'chatgpt': None,
72
+ # 'problem_url': None,
73
+ # 'edi_url' : None,
74
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
75
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
76
+ # 'first' : True,
77
+ # 'style' : 'hint'}
78
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
79
+ # print('Edi', state['edi_text'])
80
+ # assert(False)
81
+ # state = {'chatgpt': None,
82
+ # 'problem_url': None,
83
+ # 'edi_url' : None,
84
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
85
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
86
+ # 'first' : True,
87
+ # 'style' : 'hint'}
88
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
89
+ # print(state['problem_text'], state['edi_text'])
90
+ ind = random.randint(1, 100000)
91
+ state = {'chatgpt': None,
92
+ 'problem_url': None,
93
+ 'edi_url' : None,
94
+ 'problem_text' : '',
95
+ 'edi_text' : '',
96
+ 'first' : True,
97
+ 'style' : None,
98
+ 'id': '#' + str(ind)}
99
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
100
+ ind += 1
101
+ bot = ""
102
+ if (state['problem_text'] == ''):
103
+ try:
104
+ state['problem_url'] = message
105
+ if ('usaco' in message.lower()):
106
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
107
+ elif ('codeforces' in message.lower()):
108
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
109
+ else:
110
+ assert(False)
111
+
112
+ bot = "Thanks! Now could you provide a link to the editorial."
113
+ print(state['problem_text'])
114
+ except Exception as e:
115
+ print(e)
116
+ bot = "There was something wrong with the link. Could you paste it again?"
117
+ elif (state['edi_text'] == ''):
118
+ try:
119
+ state['edi_url'] = message
120
+ if ('usaco' in message.lower()):
121
+ # cbot = Chat(observation_generation)
122
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
123
+ elif ('codeforces' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
126
+ else:
127
+ assert(False)
128
+
129
+ assert(len(state['edi_text']))
130
+ print('EDITORIAL\n', state['edi_text'])
131
+ state['first']=True
132
+
133
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
134
+
135
+ except Exception as e:
136
+ print(e)
137
+ bot = "There was something wrong with the link. Could you paste it again?"
138
+
139
+ elif (state['style'] == None):
140
+ message = message.lower()
141
+ if ('editorial' in message):
142
+ state['style'] = 'editorial'
143
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
144
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
145
+ elif ('hint' in message):
146
+ state['style'] = 'hint'
147
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
148
+ bot += "What are you current thoughts on the problem? "
149
+ else:
150
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
151
+
152
+
153
+
154
+ if (len(bot) == 0):
155
+ if (state['style'] == 'hint'):
156
+ if (state['first']):
157
+ state['first']=False
158
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
159
+ else:
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
161
+ else:
162
+ state['first']=False
163
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
164
+
165
+
166
+ history.append((message, bot))
167
+
168
+ chat_history = ''
169
+ for i in history:
170
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
171
+
172
+ with open(CHAT_PATH + state['id'], 'w') as f:
173
+ f.write(chat_history)
174
+
175
+ return "", history, state
176
+
177
+
178
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
179
+
180
+ def sub_rating(state, option):
181
+ if (state['id'] == None):
182
+ return
183
+ with open(CHAT_PATH + 'meta', 'a') as f:
184
+ f.write(f"{state['id']}: {option}\n")
185
+
186
+ return state, "Choose an Option"
187
+
188
+ rating = gr.Dropdown([
189
+ "Choose an Option",
190
+ "1 - Very Unhelpful",
191
+ "2 - Unhelpful",
192
+ "3 - Neutral",
193
+ "4 - Helpful",
194
+ "5 - Very Helpful"
195
+ ], value = "Choose an Option", label='Rate the Conversation')
196
+
197
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
198
+
199
+ def sub_feedback(state, message):
200
+ if (state['id'] == None):
201
+ return
202
+ with open(CHAT_PATH + state['id'], 'a') as f:
203
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
204
+ with open(CHAT_PATH + 'feedback', 'a') as f:
205
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
206
+
207
+ return state, ""
208
+
209
+ feedback = gr.Textbox(label = "Submit Feedback")
210
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
211
+
212
+
213
+
214
+
215
+ demo.launch()
.history/app_20240731171202.py ADDED
@@ -0,0 +1,215 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+ """)
46
+
47
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
48
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
49
+ msg = gr.Textbox(label = "Chat with the bot")
50
+ # clear = gr.ClearButton([msg, interface])
51
+ state = gr.State()
52
+
53
+
54
+ def cp_helper(message, history, state):
55
+ global chats, last_update
56
+ current_time = time.time()
57
+ day = 24 * 60 * 60
58
+ if (current_time - last_update >= day):
59
+ last_update=current_time
60
+ chats=1000
61
+
62
+ if (chats <= 0):
63
+ history.append((message, "Too many chats today, try again later."))
64
+ return "", history, state
65
+
66
+ chats-=1
67
+
68
+ global ind
69
+ if (state is None):
70
+
71
+ # state = {'chatgpt': None,
72
+ # 'problem_url': None,
73
+ # 'edi_url' : None,
74
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
75
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
76
+ # 'first' : True,
77
+ # 'style' : 'hint'}
78
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
79
+ # print('Edi', state['edi_text'])
80
+ # assert(False)
81
+ # state = {'chatgpt': None,
82
+ # 'problem_url': None,
83
+ # 'edi_url' : None,
84
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
85
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
86
+ # 'first' : True,
87
+ # 'style' : 'hint'}
88
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
89
+ # print(state['problem_text'], state['edi_text'])
90
+ ind = random.randint(1, 100000)
91
+ state = {'chatgpt': None,
92
+ 'problem_url': None,
93
+ 'edi_url' : None,
94
+ 'problem_text' : '',
95
+ 'edi_text' : '',
96
+ 'first' : True,
97
+ 'style' : None,
98
+ 'id': '#' + str(ind)}
99
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
100
+ ind += 1
101
+ bot = ""
102
+ if (state['problem_text'] == ''):
103
+ try:
104
+ state['problem_url'] = message
105
+ if ('usaco' in message.lower()):
106
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
107
+ elif ('codeforces' in message.lower()):
108
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
109
+ else:
110
+ assert(False)
111
+
112
+ bot = "Thanks! Now could you provide a link to the editorial."
113
+ print(state['problem_text'])
114
+ except Exception as e:
115
+ print(e)
116
+ bot = "There was something wrong with the link. Could you paste it again?"
117
+ elif (state['edi_text'] == ''):
118
+ try:
119
+ state['edi_url'] = message
120
+ if ('usaco' in message.lower()):
121
+ # cbot = Chat(observation_generation)
122
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
123
+ elif ('codeforces' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
126
+ else:
127
+ assert(False)
128
+
129
+ assert(len(state['edi_text']))
130
+ print('EDITORIAL\n', state['edi_text'])
131
+ state['first']=True
132
+
133
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
134
+
135
+ except Exception as e:
136
+ print(e)
137
+ bot = "There was something wrong with the link. Could you paste it again?"
138
+
139
+ elif (state['style'] == None):
140
+ message = message.lower()
141
+ if ('editorial' in message):
142
+ state['style'] = 'editorial'
143
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
144
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
145
+ elif ('hint' in message):
146
+ state['style'] = 'hint'
147
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
148
+ bot += "What are you current thoughts on the problem? "
149
+ else:
150
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
151
+
152
+
153
+
154
+ if (len(bot) == 0):
155
+ if (state['style'] == 'hint'):
156
+ if (state['first']):
157
+ state['first']=False
158
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
159
+ else:
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
161
+ else:
162
+ state['first']=False
163
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
164
+
165
+
166
+ history.append((message, bot))
167
+
168
+ chat_history = ''
169
+ for i in history:
170
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
171
+
172
+ with open(CHAT_PATH + state['id'], 'w') as f:
173
+ f.write(chat_history)
174
+
175
+ return "", history, state
176
+
177
+
178
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
179
+
180
+ def sub_rating(state, option):
181
+ if (state['id'] == None):
182
+ return
183
+ with open(CHAT_PATH + 'meta', 'a') as f:
184
+ f.write(f"{state['id']}: {option}\n")
185
+
186
+ return state, "Choose an Option"
187
+
188
+ rating = gr.Dropdown([
189
+ "Choose an Option",
190
+ "1 - Very Unhelpful",
191
+ "2 - Unhelpful",
192
+ "3 - Neutral",
193
+ "4 - Helpful",
194
+ "5 - Very Helpful"
195
+ ], value = "Choose an Option", label='Rate the Conversation')
196
+
197
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
198
+
199
+ def sub_feedback(state, message):
200
+ if (state['id'] == None):
201
+ return
202
+ with open(CHAT_PATH + state['id'], 'a') as f:
203
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
204
+ with open(CHAT_PATH + 'feedback', 'a') as f:
205
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
206
+
207
+ return state, ""
208
+
209
+ feedback = gr.Textbox(label = "Submit Feedback")
210
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
211
+
212
+
213
+
214
+
215
+ demo.launch()
.history/app_20240731171223.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171244.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts),
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171249.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171300.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171309.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace or you can DM me on Discord (Sai#0001).
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171313.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace or you can DM me on Discord (Rithwik#0001).
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171320.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace or you can DM).
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171326.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, f
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171334.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171345.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171346.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171354.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by Rithwik Gupta (). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171447.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by Rithwik Gupta . If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171505.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by Rithwik Gupta. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171528.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta]. If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171534.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171600.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on CF). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171602.py ADDED
@@ -0,0 +1,217 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ """)
48
+
49
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
50
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
51
+ msg = gr.Textbox(label = "Chat with the bot")
52
+ # clear = gr.ClearButton([msg, interface])
53
+ state = gr.State()
54
+
55
+
56
+ def cp_helper(message, history, state):
57
+ global chats, last_update
58
+ current_time = time.time()
59
+ day = 24 * 60 * 60
60
+ if (current_time - last_update >= day):
61
+ last_update=current_time
62
+ chats=1000
63
+
64
+ if (chats <= 0):
65
+ history.append((message, "Too many chats today, try again later."))
66
+ return "", history, state
67
+
68
+ chats-=1
69
+
70
+ global ind
71
+ if (state is None):
72
+
73
+ # state = {'chatgpt': None,
74
+ # 'problem_url': None,
75
+ # 'edi_url' : None,
76
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
77
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
78
+ # 'first' : True,
79
+ # 'style' : 'hint'}
80
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
81
+ # print('Edi', state['edi_text'])
82
+ # assert(False)
83
+ # state = {'chatgpt': None,
84
+ # 'problem_url': None,
85
+ # 'edi_url' : None,
86
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
87
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
88
+ # 'first' : True,
89
+ # 'style' : 'hint'}
90
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
91
+ # print(state['problem_text'], state['edi_text'])
92
+ ind = random.randint(1, 100000)
93
+ state = {'chatgpt': None,
94
+ 'problem_url': None,
95
+ 'edi_url' : None,
96
+ 'problem_text' : '',
97
+ 'edi_text' : '',
98
+ 'first' : True,
99
+ 'style' : None,
100
+ 'id': '#' + str(ind)}
101
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
102
+ ind += 1
103
+ bot = ""
104
+ if (state['problem_text'] == ''):
105
+ try:
106
+ state['problem_url'] = message
107
+ if ('usaco' in message.lower()):
108
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
109
+ elif ('codeforces' in message.lower()):
110
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
111
+ else:
112
+ assert(False)
113
+
114
+ bot = "Thanks! Now could you provide a link to the editorial."
115
+ print(state['problem_text'])
116
+ except Exception as e:
117
+ print(e)
118
+ bot = "There was something wrong with the link. Could you paste it again?"
119
+ elif (state['edi_text'] == ''):
120
+ try:
121
+ state['edi_url'] = message
122
+ if ('usaco' in message.lower()):
123
+ # cbot = Chat(observation_generation)
124
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
125
+ elif ('codeforces' in message.lower()):
126
+ # cbot = Chat(observation_generation)
127
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
128
+ else:
129
+ assert(False)
130
+
131
+ assert(len(state['edi_text']))
132
+ print('EDITORIAL\n', state['edi_text'])
133
+ state['first']=True
134
+
135
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
136
+
137
+ except Exception as e:
138
+ print(e)
139
+ bot = "There was something wrong with the link. Could you paste it again?"
140
+
141
+ elif (state['style'] == None):
142
+ message = message.lower()
143
+ if ('editorial' in message):
144
+ state['style'] = 'editorial'
145
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
146
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
147
+ elif ('hint' in message):
148
+ state['style'] = 'hint'
149
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
150
+ bot += "What are you current thoughts on the problem? "
151
+ else:
152
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
153
+
154
+
155
+
156
+ if (len(bot) == 0):
157
+ if (state['style'] == 'hint'):
158
+ if (state['first']):
159
+ state['first']=False
160
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
161
+ else:
162
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
163
+ else:
164
+ state['first']=False
165
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
166
+
167
+
168
+ history.append((message, bot))
169
+
170
+ chat_history = ''
171
+ for i in history:
172
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
173
+
174
+ with open(CHAT_PATH + state['id'], 'w') as f:
175
+ f.write(chat_history)
176
+
177
+ return "", history, state
178
+
179
+
180
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
181
+
182
+ def sub_rating(state, option):
183
+ if (state['id'] == None):
184
+ return
185
+ with open(CHAT_PATH + 'meta', 'a') as f:
186
+ f.write(f"{state['id']}: {option}\n")
187
+
188
+ return state, "Choose an Option"
189
+
190
+ rating = gr.Dropdown([
191
+ "Choose an Option",
192
+ "1 - Very Unhelpful",
193
+ "2 - Unhelpful",
194
+ "3 - Neutral",
195
+ "4 - Helpful",
196
+ "5 - Very Helpful"
197
+ ], value = "Choose an Option", label='Rate the Conversation')
198
+
199
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
200
+
201
+ def sub_feedback(state, message):
202
+ if (state['id'] == None):
203
+ return
204
+ with open(CHAT_PATH + state['id'], 'a') as f:
205
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
206
+ with open(CHAT_PATH + 'feedback', 'a') as f:
207
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
208
+
209
+ return state, ""
210
+
211
+ feedback = gr.Textbox(label = "Submit Feedback")
212
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
213
+
214
+
215
+
216
+
217
+ demo.launch()
.history/app_20240731171608.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171609.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171613.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171615.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful,
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171617.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful, please consider starring the repository on GitHub.
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171618.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (most notably by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful, please consider starring the repository .
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171649.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (by adding scrapers, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful, please consider starring the repository .
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171652.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (by adding scrapers for other OJs, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful, please consider starring the repository .
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171657.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (by adding scrapers for other OJs, features, or prompts), you can make a pull request on HuggingFace. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful, please consider starring the repository .
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171701.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (by adding scrapers for other OJs, features, or prompts), you can make a pull request on HuggingFace community tab. If you want to collaborate, feel free to DM me on discord @Rithwik#1821.
47
+ If you found this bot useful, please consider starring the repository .
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171710.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (by adding scrapers for other OJs, features, or prompts), you can make a pull request on HuggingFace community tab. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
47
+ If you found this bot useful, please consider starring the repository .
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/app_20240731171723.py ADDED
@@ -0,0 +1,218 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from utils.scrapers import codeforces, usaco
2
+ from utils.chatbot import Chat
3
+ from utils.prompts import *
4
+ import os
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+
9
+
10
+ dir_path = os.path.dirname(os.path.realpath(__file__))
11
+ CHAT_PATH = dir_path + '/saved_chats/'
12
+ used_ids = os.listdir(CHAT_PATH)
13
+
14
+ ind = 0
15
+ for i in used_ids:
16
+ try:
17
+ ind = max(ind, int(i.split('#')[1]))
18
+ except:
19
+ pass
20
+
21
+ chats = 500
22
+ last_update = time.time()
23
+
24
+ with gr.Blocks(title = "CP Helper Bot") as demo:
25
+ ins = gr.Markdown("""
26
+ # Usage Instructions
27
+ First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
+
30
+ Choose between either 'hint' mode or 'editorial' mode
31
+
32
+
33
+ ## Hint Mode
34
+ Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
35
+
36
+
37
+ ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
+
40
+ # Feedback
41
+ Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
+ Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
+
44
+ Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (by adding scrapers for other OJs, features, or prompts), you can make a pull request on HuggingFace community tab. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
47
+ If you found this bot useful, please consider .
48
+ """)
49
+
50
+ interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
51
+ latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
52
+ msg = gr.Textbox(label = "Chat with the bot")
53
+ # clear = gr.ClearButton([msg, interface])
54
+ state = gr.State()
55
+
56
+
57
+ def cp_helper(message, history, state):
58
+ global chats, last_update
59
+ current_time = time.time()
60
+ day = 24 * 60 * 60
61
+ if (current_time - last_update >= day):
62
+ last_update=current_time
63
+ chats=1000
64
+
65
+ if (chats <= 0):
66
+ history.append((message, "Too many chats today, try again later."))
67
+ return "", history, state
68
+
69
+ chats-=1
70
+
71
+ global ind
72
+ if (state is None):
73
+
74
+ # state = {'chatgpt': None,
75
+ # 'problem_url': None,
76
+ # 'edi_url' : None,
77
+ # 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
78
+ # 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
79
+ # 'first' : True,
80
+ # 'style' : 'hint'}
81
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
82
+ # print('Edi', state['edi_text'])
83
+ # assert(False)
84
+ # state = {'chatgpt': None,
85
+ # 'problem_url': None,
86
+ # 'edi_url' : None,
87
+ # 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
88
+ # 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
89
+ # 'first' : True,
90
+ # 'style' : 'hint'}
91
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
92
+ # print(state['problem_text'], state['edi_text'])
93
+ ind = random.randint(1, 100000)
94
+ state = {'chatgpt': None,
95
+ 'problem_url': None,
96
+ 'edi_url' : None,
97
+ 'problem_text' : '',
98
+ 'edi_text' : '',
99
+ 'first' : True,
100
+ 'style' : None,
101
+ 'id': '#' + str(ind)}
102
+ # state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
103
+ ind += 1
104
+ bot = ""
105
+ if (state['problem_text'] == ''):
106
+ try:
107
+ state['problem_url'] = message
108
+ if ('usaco' in message.lower()):
109
+ state['problem_text'] = usaco.problem(state['problem_url'])['statement']
110
+ elif ('codeforces' in message.lower()):
111
+ state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
112
+ else:
113
+ assert(False)
114
+
115
+ bot = "Thanks! Now could you provide a link to the editorial."
116
+ print(state['problem_text'])
117
+ except Exception as e:
118
+ print(e)
119
+ bot = "There was something wrong with the link. Could you paste it again?"
120
+ elif (state['edi_text'] == ''):
121
+ try:
122
+ state['edi_url'] = message
123
+ if ('usaco' in message.lower()):
124
+ # cbot = Chat(observation_generation)
125
+ state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
126
+ elif ('codeforces' in message.lower()):
127
+ # cbot = Chat(observation_generation)
128
+ state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
129
+ else:
130
+ assert(False)
131
+
132
+ assert(len(state['edi_text']))
133
+ print('EDITORIAL\n', state['edi_text'])
134
+ state['first']=True
135
+
136
+ bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
137
+
138
+ except Exception as e:
139
+ print(e)
140
+ bot = "There was something wrong with the link. Could you paste it again?"
141
+
142
+ elif (state['style'] == None):
143
+ message = message.lower()
144
+ if ('editorial' in message):
145
+ state['style'] = 'editorial'
146
+ state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
147
+ bot = state['edi_text'] + '\nLet me know if you have any questions.'
148
+ elif ('hint' in message):
149
+ state['style'] = 'hint'
150
+ state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
151
+ bot += "What are you current thoughts on the problem? "
152
+ else:
153
+ bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
154
+
155
+
156
+
157
+ if (len(bot) == 0):
158
+ if (state['style'] == 'hint'):
159
+ if (state['first']):
160
+ state['first']=False
161
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
162
+ else:
163
+ bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
164
+ else:
165
+ state['first']=False
166
+ bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
167
+
168
+
169
+ history.append((message, bot))
170
+
171
+ chat_history = ''
172
+ for i in history:
173
+ chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
174
+
175
+ with open(CHAT_PATH + state['id'], 'w') as f:
176
+ f.write(chat_history)
177
+
178
+ return "", history, state
179
+
180
+
181
+ msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
182
+
183
+ def sub_rating(state, option):
184
+ if (state['id'] == None):
185
+ return
186
+ with open(CHAT_PATH + 'meta', 'a') as f:
187
+ f.write(f"{state['id']}: {option}\n")
188
+
189
+ return state, "Choose an Option"
190
+
191
+ rating = gr.Dropdown([
192
+ "Choose an Option",
193
+ "1 - Very Unhelpful",
194
+ "2 - Unhelpful",
195
+ "3 - Neutral",
196
+ "4 - Helpful",
197
+ "5 - Very Helpful"
198
+ ], value = "Choose an Option", label='Rate the Conversation')
199
+
200
+ gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
201
+
202
+ def sub_feedback(state, message):
203
+ if (state['id'] == None):
204
+ return
205
+ with open(CHAT_PATH + state['id'], 'a') as f:
206
+ f.write(f"FEEDBACK: {message}\n\n\n\n")
207
+ with open(CHAT_PATH + 'feedback', 'a') as f:
208
+ f.write(f"FEEDBACK {state['id']}: {message}\n\n")
209
+
210
+ return state, ""
211
+
212
+ feedback = gr.Textbox(label = "Submit Feedback")
213
+ feedback.submit(sub_feedback, [state, feedback], [state, feedback])
214
+
215
+
216
+
217
+
218
+ demo.launch()
.history/utils/chatbot_20240731171803.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import openai
2
+ from openai import OpenAI
3
+ import os
4
+ openai.api_key = os.environ['OPENAI_KEY']
5
+
6
+ class Chat:
7
+
8
+ def __init__(self, system_message, lim=0.005):
9
+ self.messages = [
10
+ {"role": "system", "content" : system_message}
11
+ ]
12
+ self.left = lim
13
+ self.client = OpenAI(api_key=API_KEY)
14
+
15
+ def chat(self, query):
16
+ if (self.left <= 0):
17
+ return "You have reached the chat limit."
18
+ if (len(query) > 20000):
19
+ return "Chat input is too long"
20
+
21
+ self.messages.append({"role" : "user", "content" : query})
22
+
23
+ # print(self.messages)
24
+
25
+ response = self.client.chat.completions.create(
26
+ model="gpt-4o-mini",
27
+ messages=self.messages,
28
+ temperature=0.3
29
+ )
30
+ # del self.messages[-1]
31
+ result = response.choices[0].message.content
32
+
33
+ self.messages.append(response.choices[0].message)
34
+ # print(response)
35
+ # print([result])
36
+
37
+ result = result.replace('\\(', '$')
38
+ result = result.replace('\\)', '$')
39
+ # print([result])
40
+
41
+ self.left -= 0.6 * response.usage.completion_tokens / (10**6) + 0.15 * response.usage.prompt_tokens /(10**6)
42
+ print(self.left)
43
+
44
+ return (result)
app.py CHANGED
@@ -25,7 +25,7 @@ with gr.Blocks(title = "CP Helper Bot") as demo:
25
  ins = gr.Markdown("""
26
  # Usage Instructions
27
  First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
- Then, paste the link to an official editorial for the problem.
29
 
30
  Choose between either 'hint' mode or 'editorial' mode
31
 
@@ -35,13 +35,16 @@ with gr.Blocks(title = "CP Helper Bot") as demo:
35
 
36
 
37
  ## Editorial Mode
38
- Get a simplified summary with step-by-step observations to solve the problem, and chat with the bot about questions you have about the editorial.
39
 
40
  # Feedback
41
  Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
  Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
 
44
  Thanks for using this bot, and I hope you found it helpful!
 
 
 
45
  """)
46
 
47
  interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
 
25
  ins = gr.Markdown("""
26
  # Usage Instructions
27
  First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
28
+ Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
29
 
30
  Choose between either 'hint' mode or 'editorial' mode
31
 
 
35
 
36
 
37
  ## Editorial Mode
38
+ Chat with the bot about the editorial to the problem.
39
 
40
  # Feedback
41
  Please provide written feedback, maybe there was an issue with the bot or you really liked some response. You can give written feedback while chatting with the bot; it is recorded in real time with the chat history. Use the second text box for this.
42
  Please also provide feedback on the bot's helpfulness. Use the first dropdown (after you completed your chat) to rate how helpful the bot was. Consider how the bot's responses helped your understand faster and more effectively.
43
 
44
  Thanks for using this bot, and I hope you found it helpful!
45
+
46
+ This is an open source project maintained by [Rithwik Gupta](github.com/Rithwik-G) (beaboss on codeforces). If you would like to contribute (by adding scrapers for other OJs, features, or prompts), you can make a pull request on HuggingFace community tab. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
47
+ If you found this bot useful, please consider .
48
  """)
49
 
50
  interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
utils/chatbot.py CHANGED
@@ -1,7 +1,7 @@
1
  import openai
2
  from openai import OpenAI
3
  import os
4
- # openai.api_key = os.environ['OPENAI_KEY']
5
 
6
  class Chat:
7
 
 
1
  import openai
2
  from openai import OpenAI
3
  import os
4
+ openai.api_key = os.environ['OPENAI_KEY']
5
 
6
  class Chat:
7