Spaces:
Running
Running
require chrome driver
Browse files- .DS_Store +0 -0
- .history/app_20240731171937.py +218 -0
- .history/app_20240731171947.py +218 -0
- .history/app_20240731171957.py +218 -0
- .history/app_20240731172000.py +218 -0
- .history/app_20240731172129.py +218 -0
- .history/app_20240731172136.py +218 -0
- .history/app_20240731172140.py +218 -0
- .history/app_20240731172151.py +218 -0
- .history/packages_20240731172952.txt +0 -0
- .history/packages_20240731172953.txt +0 -0
- .history/packages_20240731172954.txt +1 -0
- .history/utils/chatbot_20240731172749.py +44 -0
- .history/utils/chatbot_20240731173008.py +44 -0
- app.py +2 -2
- packages.txt +1 -0
- saved_chats/#29622 +6 -0
- utils/__pycache__/chatbot.cpython-312.pyc +0 -0
- utils/scrapers/prescraped/codeforces/Problems/1997D +1 -0
.DS_Store
CHANGED
Binary files a/.DS_Store and b/.DS_Store differ
|
|
.history/app_20240731171937.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, .
|
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_20240731171947.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 star the GitHub repo.
|
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_20240731171957.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 star the GitHub repo.
|
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_20240731172000.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 star the GitHub repo at.
|
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_20240731172129.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, 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 star the GitHub repo at.
|
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_20240731172136.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, adjusting 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 star the GitHub repo at.
|
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_20240731172140.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, adding features, adjusting 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 star the GitHub repo at.
|
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_20240731172151.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, adding features, adjusting prompts, making the feedback system better, etc.), 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 star the GitHub repo at.
|
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/packages_20240731172952.txt
ADDED
File without changes
|
.history/packages_20240731172953.txt
ADDED
File without changes
|
.history/packages_20240731172954.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
chromium-driver
|
.history/utils/chatbot_20240731172749.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)
|
.history/utils/chatbot_20240731173008.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
@@ -43,8 +43,8 @@ with gr.Blocks(title = "CP Helper Bot") as demo:
|
|
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,
|
47 |
-
If you found this bot useful, please
|
48 |
""")
|
49 |
|
50 |
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
|
|
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, adding features, adjusting prompts, making the feedback system better, etc.), 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 star the GitHub repo at.
|
48 |
""")
|
49 |
|
50 |
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
packages.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
chromium-driver
|
saved_chats/#29622
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
USER: https://codeforces.com/problemset/problem/1997/D
|
2 |
+
|
3 |
+
BOT: Thanks! Now could you provide a link to the editorial.
|
4 |
+
|
5 |
+
|
6 |
+
|
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/scrapers/prescraped/codeforces/Problems/1997D
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
You are given a rooted tree, consisting of n vertices. The vertices in the tree are numbered from 1 to n, and the root is the vertex 1. The value ai is written at the i-th vertex.You can perform the following operation any number of times (possibly zero): choose a vertex v which has at least one child; increase av by 1; and decrease au by 1 for all vertices u that are in the subtree of v (except v itself). However, after each operation, the values on all vertices should be non-negative.Your task is to calculate the maximum possible value written at the root using the aforementioned operation.
|