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

Fixed page size

Browse files
.history/app_20240731162317.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.
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='80vh', 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_20240731164900.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.
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='80vh', 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_20240731164903.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.
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='500px', 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_20240731164950.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.
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/utils/chatbot_20240731164917.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
@@ -44,7 +44,7 @@ with gr.Blocks(title = "CP Helper Bot") as demo:
44
  Thanks for using this bot, and I hope you found it helpful!
45
  """)
46
 
47
- interface = gr.Chatbot(height='80vh', 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])
 
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])
utils/__pycache__/chatbot.cpython-312.pyc CHANGED
Binary files a/utils/__pycache__/chatbot.cpython-312.pyc and b/utils/__pycache__/chatbot.cpython-312.pyc differ
 
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