Spaces:
Sleeping
Sleeping
Updated instructions
Browse files- .history/app_20240731174109.py +217 -0
- .history/app_20240731174119.py +217 -0
- .history/app_20240731174121.py +217 -0
- .history/app_20240731174133.py +217 -0
- .history/app_20240731174136.py +217 -0
- .history/app_20240731174137.py +217 -0
- .history/app_20240731174340.py +218 -0
- .history/app_20240731174352.py +218 -0
- app.py +2 -2
.history/app_20240731174109.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.scrapers import codeforces, usaco
|
2 |
+
from utils.chatbot import Chat
|
3 |
+
from utils.prompts import *
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
|
9 |
+
|
10 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
11 |
+
CHAT_PATH = dir_path + '/saved_chats/'
|
12 |
+
used_ids = os.listdir(CHAT_PATH)
|
13 |
+
|
14 |
+
ind = 0
|
15 |
+
for i in used_ids:
|
16 |
+
try:
|
17 |
+
ind = max(ind, int(i.split('#')[1]))
|
18 |
+
except:
|
19 |
+
pass
|
20 |
+
|
21 |
+
chats = 500
|
22 |
+
last_update = time.time()
|
23 |
+
|
24 |
+
with gr.Blocks(title = "CP Helper Bot") as demo:
|
25 |
+
ins = gr.Markdown("""
|
26 |
+
# Usage Instructions
|
27 |
+
First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
|
28 |
+
Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
|
29 |
+
|
30 |
+
Choose between either 'hint' mode or 'editorial' mode
|
31 |
+
|
32 |
+
## Hint Mode
|
33 |
+
Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
|
34 |
+
|
35 |
+
|
36 |
+
## Editorial Mode
|
37 |
+
Chat with the bot about the editorial to the problem.
|
38 |
+
|
39 |
+
# Feedback
|
40 |
+
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.
|
41 |
+
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.
|
42 |
+
|
43 |
+
Thanks for using this bot, and I hope you found it helpful!
|
44 |
+
|
45 |
+
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 or GitHub. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
|
46 |
+
If you found this bot useful, please star the GitHub repo :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
47 |
+
""")
|
48 |
+
|
49 |
+
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
50 |
+
latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
|
51 |
+
msg = gr.Textbox(label = "Chat with the bot")
|
52 |
+
# clear = gr.ClearButton([msg, interface])
|
53 |
+
state = gr.State()
|
54 |
+
|
55 |
+
|
56 |
+
def cp_helper(message, history, state):
|
57 |
+
global chats, last_update
|
58 |
+
current_time = time.time()
|
59 |
+
day = 24 * 60 * 60
|
60 |
+
if (current_time - last_update >= day):
|
61 |
+
last_update=current_time
|
62 |
+
chats=1000
|
63 |
+
|
64 |
+
if (chats <= 0):
|
65 |
+
history.append((message, "Too many chats today, try again later."))
|
66 |
+
return "", history, state
|
67 |
+
|
68 |
+
chats-=1
|
69 |
+
|
70 |
+
global ind
|
71 |
+
if (state is None):
|
72 |
+
|
73 |
+
# state = {'chatgpt': None,
|
74 |
+
# 'problem_url': None,
|
75 |
+
# 'edi_url' : None,
|
76 |
+
# 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
|
77 |
+
# 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
|
78 |
+
# 'first' : True,
|
79 |
+
# 'style' : 'hint'}
|
80 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
|
81 |
+
# print('Edi', state['edi_text'])
|
82 |
+
# assert(False)
|
83 |
+
# state = {'chatgpt': None,
|
84 |
+
# 'problem_url': None,
|
85 |
+
# 'edi_url' : None,
|
86 |
+
# 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
|
87 |
+
# 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
|
88 |
+
# 'first' : True,
|
89 |
+
# 'style' : 'hint'}
|
90 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
|
91 |
+
# print(state['problem_text'], state['edi_text'])
|
92 |
+
ind = random.randint(1, 100000)
|
93 |
+
state = {'chatgpt': None,
|
94 |
+
'problem_url': None,
|
95 |
+
'edi_url' : None,
|
96 |
+
'problem_text' : '',
|
97 |
+
'edi_text' : '',
|
98 |
+
'first' : True,
|
99 |
+
'style' : None,
|
100 |
+
'id': '#' + str(ind)}
|
101 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
102 |
+
ind += 1
|
103 |
+
bot = ""
|
104 |
+
if (state['problem_text'] == ''):
|
105 |
+
try:
|
106 |
+
state['problem_url'] = message
|
107 |
+
if ('usaco' in message.lower()):
|
108 |
+
state['problem_text'] = usaco.problem(state['problem_url'])['statement']
|
109 |
+
elif ('codeforces' in message.lower()):
|
110 |
+
state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
|
111 |
+
else:
|
112 |
+
assert(False)
|
113 |
+
|
114 |
+
bot = "Thanks! Now could you provide a link to the editorial."
|
115 |
+
print(state['problem_text'])
|
116 |
+
except Exception as e:
|
117 |
+
print(e)
|
118 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
119 |
+
elif (state['edi_text'] == ''):
|
120 |
+
try:
|
121 |
+
state['edi_url'] = message
|
122 |
+
if ('usaco' in message.lower()):
|
123 |
+
# cbot = Chat(observation_generation)
|
124 |
+
state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
|
125 |
+
elif ('codeforces' in message.lower()):
|
126 |
+
# cbot = Chat(observation_generation)
|
127 |
+
state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
|
128 |
+
else:
|
129 |
+
assert(False)
|
130 |
+
|
131 |
+
assert(len(state['edi_text']))
|
132 |
+
print('EDITORIAL\n', state['edi_text'])
|
133 |
+
state['first']=True
|
134 |
+
|
135 |
+
bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
print(e)
|
139 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
140 |
+
|
141 |
+
elif (state['style'] == None):
|
142 |
+
message = message.lower()
|
143 |
+
if ('editorial' in message):
|
144 |
+
state['style'] = 'editorial'
|
145 |
+
state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
|
146 |
+
bot = state['edi_text'] + '\nLet me know if you have any questions.'
|
147 |
+
elif ('hint' in message):
|
148 |
+
state['style'] = 'hint'
|
149 |
+
state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
150 |
+
bot += "What are you current thoughts on the problem? "
|
151 |
+
else:
|
152 |
+
bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
if (len(bot) == 0):
|
157 |
+
if (state['style'] == 'hint'):
|
158 |
+
if (state['first']):
|
159 |
+
state['first']=False
|
160 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
|
161 |
+
else:
|
162 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
|
163 |
+
else:
|
164 |
+
state['first']=False
|
165 |
+
bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
|
166 |
+
|
167 |
+
|
168 |
+
history.append((message, bot))
|
169 |
+
|
170 |
+
chat_history = ''
|
171 |
+
for i in history:
|
172 |
+
chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
|
173 |
+
|
174 |
+
with open(CHAT_PATH + state['id'], 'w') as f:
|
175 |
+
f.write(chat_history)
|
176 |
+
|
177 |
+
return "", history, state
|
178 |
+
|
179 |
+
|
180 |
+
msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
|
181 |
+
|
182 |
+
def sub_rating(state, option):
|
183 |
+
if (state['id'] == None):
|
184 |
+
return
|
185 |
+
with open(CHAT_PATH + 'meta', 'a') as f:
|
186 |
+
f.write(f"{state['id']}: {option}\n")
|
187 |
+
|
188 |
+
return state, "Choose an Option"
|
189 |
+
|
190 |
+
rating = gr.Dropdown([
|
191 |
+
"Choose an Option",
|
192 |
+
"1 - Very Unhelpful",
|
193 |
+
"2 - Unhelpful",
|
194 |
+
"3 - Neutral",
|
195 |
+
"4 - Helpful",
|
196 |
+
"5 - Very Helpful"
|
197 |
+
], value = "Choose an Option", label='Rate the Conversation')
|
198 |
+
|
199 |
+
gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
|
200 |
+
|
201 |
+
def sub_feedback(state, message):
|
202 |
+
if (state['id'] == None):
|
203 |
+
return
|
204 |
+
with open(CHAT_PATH + state['id'], 'a') as f:
|
205 |
+
f.write(f"FEEDBACK: {message}\n\n\n\n")
|
206 |
+
with open(CHAT_PATH + 'feedback', 'a') as f:
|
207 |
+
f.write(f"FEEDBACK {state['id']}: {message}\n\n")
|
208 |
+
|
209 |
+
return state, ""
|
210 |
+
|
211 |
+
feedback = gr.Textbox(label = "Submit Feedback")
|
212 |
+
feedback.submit(sub_feedback, [state, feedback], [state, feedback])
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
+
demo.launch()
|
.history/app_20240731174119.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.scrapers import codeforces, usaco
|
2 |
+
from utils.chatbot import Chat
|
3 |
+
from utils.prompts import *
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
|
9 |
+
|
10 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
11 |
+
CHAT_PATH = dir_path + '/saved_chats/'
|
12 |
+
used_ids = os.listdir(CHAT_PATH)
|
13 |
+
|
14 |
+
ind = 0
|
15 |
+
for i in used_ids:
|
16 |
+
try:
|
17 |
+
ind = max(ind, int(i.split('#')[1]))
|
18 |
+
except:
|
19 |
+
pass
|
20 |
+
|
21 |
+
chats = 500
|
22 |
+
last_update = time.time()
|
23 |
+
|
24 |
+
with gr.Blocks(title = "CP Helper Bot") as demo:
|
25 |
+
ins = gr.Markdown("""
|
26 |
+
# Usage Instructions
|
27 |
+
First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
|
28 |
+
Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
|
29 |
+
|
30 |
+
Choose between either 'hint' mode or 'editorial' mode. You have to reload the page to
|
31 |
+
|
32 |
+
## Hint Mode
|
33 |
+
Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
|
34 |
+
|
35 |
+
|
36 |
+
## Editorial Mode
|
37 |
+
Chat with the bot about the editorial to the problem.
|
38 |
+
|
39 |
+
# Feedback
|
40 |
+
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.
|
41 |
+
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.
|
42 |
+
|
43 |
+
Thanks for using this bot, and I hope you found it helpful!
|
44 |
+
|
45 |
+
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 or GitHub. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
|
46 |
+
If you found this bot useful, please star the GitHub repo :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
47 |
+
""")
|
48 |
+
|
49 |
+
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
50 |
+
latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
|
51 |
+
msg = gr.Textbox(label = "Chat with the bot")
|
52 |
+
# clear = gr.ClearButton([msg, interface])
|
53 |
+
state = gr.State()
|
54 |
+
|
55 |
+
|
56 |
+
def cp_helper(message, history, state):
|
57 |
+
global chats, last_update
|
58 |
+
current_time = time.time()
|
59 |
+
day = 24 * 60 * 60
|
60 |
+
if (current_time - last_update >= day):
|
61 |
+
last_update=current_time
|
62 |
+
chats=1000
|
63 |
+
|
64 |
+
if (chats <= 0):
|
65 |
+
history.append((message, "Too many chats today, try again later."))
|
66 |
+
return "", history, state
|
67 |
+
|
68 |
+
chats-=1
|
69 |
+
|
70 |
+
global ind
|
71 |
+
if (state is None):
|
72 |
+
|
73 |
+
# state = {'chatgpt': None,
|
74 |
+
# 'problem_url': None,
|
75 |
+
# 'edi_url' : None,
|
76 |
+
# 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
|
77 |
+
# 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
|
78 |
+
# 'first' : True,
|
79 |
+
# 'style' : 'hint'}
|
80 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
|
81 |
+
# print('Edi', state['edi_text'])
|
82 |
+
# assert(False)
|
83 |
+
# state = {'chatgpt': None,
|
84 |
+
# 'problem_url': None,
|
85 |
+
# 'edi_url' : None,
|
86 |
+
# 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
|
87 |
+
# 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
|
88 |
+
# 'first' : True,
|
89 |
+
# 'style' : 'hint'}
|
90 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
|
91 |
+
# print(state['problem_text'], state['edi_text'])
|
92 |
+
ind = random.randint(1, 100000)
|
93 |
+
state = {'chatgpt': None,
|
94 |
+
'problem_url': None,
|
95 |
+
'edi_url' : None,
|
96 |
+
'problem_text' : '',
|
97 |
+
'edi_text' : '',
|
98 |
+
'first' : True,
|
99 |
+
'style' : None,
|
100 |
+
'id': '#' + str(ind)}
|
101 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
102 |
+
ind += 1
|
103 |
+
bot = ""
|
104 |
+
if (state['problem_text'] == ''):
|
105 |
+
try:
|
106 |
+
state['problem_url'] = message
|
107 |
+
if ('usaco' in message.lower()):
|
108 |
+
state['problem_text'] = usaco.problem(state['problem_url'])['statement']
|
109 |
+
elif ('codeforces' in message.lower()):
|
110 |
+
state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
|
111 |
+
else:
|
112 |
+
assert(False)
|
113 |
+
|
114 |
+
bot = "Thanks! Now could you provide a link to the editorial."
|
115 |
+
print(state['problem_text'])
|
116 |
+
except Exception as e:
|
117 |
+
print(e)
|
118 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
119 |
+
elif (state['edi_text'] == ''):
|
120 |
+
try:
|
121 |
+
state['edi_url'] = message
|
122 |
+
if ('usaco' in message.lower()):
|
123 |
+
# cbot = Chat(observation_generation)
|
124 |
+
state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
|
125 |
+
elif ('codeforces' in message.lower()):
|
126 |
+
# cbot = Chat(observation_generation)
|
127 |
+
state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
|
128 |
+
else:
|
129 |
+
assert(False)
|
130 |
+
|
131 |
+
assert(len(state['edi_text']))
|
132 |
+
print('EDITORIAL\n', state['edi_text'])
|
133 |
+
state['first']=True
|
134 |
+
|
135 |
+
bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
print(e)
|
139 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
140 |
+
|
141 |
+
elif (state['style'] == None):
|
142 |
+
message = message.lower()
|
143 |
+
if ('editorial' in message):
|
144 |
+
state['style'] = 'editorial'
|
145 |
+
state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
|
146 |
+
bot = state['edi_text'] + '\nLet me know if you have any questions.'
|
147 |
+
elif ('hint' in message):
|
148 |
+
state['style'] = 'hint'
|
149 |
+
state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
150 |
+
bot += "What are you current thoughts on the problem? "
|
151 |
+
else:
|
152 |
+
bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
if (len(bot) == 0):
|
157 |
+
if (state['style'] == 'hint'):
|
158 |
+
if (state['first']):
|
159 |
+
state['first']=False
|
160 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
|
161 |
+
else:
|
162 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
|
163 |
+
else:
|
164 |
+
state['first']=False
|
165 |
+
bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
|
166 |
+
|
167 |
+
|
168 |
+
history.append((message, bot))
|
169 |
+
|
170 |
+
chat_history = ''
|
171 |
+
for i in history:
|
172 |
+
chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
|
173 |
+
|
174 |
+
with open(CHAT_PATH + state['id'], 'w') as f:
|
175 |
+
f.write(chat_history)
|
176 |
+
|
177 |
+
return "", history, state
|
178 |
+
|
179 |
+
|
180 |
+
msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
|
181 |
+
|
182 |
+
def sub_rating(state, option):
|
183 |
+
if (state['id'] == None):
|
184 |
+
return
|
185 |
+
with open(CHAT_PATH + 'meta', 'a') as f:
|
186 |
+
f.write(f"{state['id']}: {option}\n")
|
187 |
+
|
188 |
+
return state, "Choose an Option"
|
189 |
+
|
190 |
+
rating = gr.Dropdown([
|
191 |
+
"Choose an Option",
|
192 |
+
"1 - Very Unhelpful",
|
193 |
+
"2 - Unhelpful",
|
194 |
+
"3 - Neutral",
|
195 |
+
"4 - Helpful",
|
196 |
+
"5 - Very Helpful"
|
197 |
+
], value = "Choose an Option", label='Rate the Conversation')
|
198 |
+
|
199 |
+
gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
|
200 |
+
|
201 |
+
def sub_feedback(state, message):
|
202 |
+
if (state['id'] == None):
|
203 |
+
return
|
204 |
+
with open(CHAT_PATH + state['id'], 'a') as f:
|
205 |
+
f.write(f"FEEDBACK: {message}\n\n\n\n")
|
206 |
+
with open(CHAT_PATH + 'feedback', 'a') as f:
|
207 |
+
f.write(f"FEEDBACK {state['id']}: {message}\n\n")
|
208 |
+
|
209 |
+
return state, ""
|
210 |
+
|
211 |
+
feedback = gr.Textbox(label = "Submit Feedback")
|
212 |
+
feedback.submit(sub_feedback, [state, feedback], [state, feedback])
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
+
demo.launch()
|
.history/app_20240731174121.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.scrapers import codeforces, usaco
|
2 |
+
from utils.chatbot import Chat
|
3 |
+
from utils.prompts import *
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
|
9 |
+
|
10 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
11 |
+
CHAT_PATH = dir_path + '/saved_chats/'
|
12 |
+
used_ids = os.listdir(CHAT_PATH)
|
13 |
+
|
14 |
+
ind = 0
|
15 |
+
for i in used_ids:
|
16 |
+
try:
|
17 |
+
ind = max(ind, int(i.split('#')[1]))
|
18 |
+
except:
|
19 |
+
pass
|
20 |
+
|
21 |
+
chats = 500
|
22 |
+
last_update = time.time()
|
23 |
+
|
24 |
+
with gr.Blocks(title = "CP Helper Bot") as demo:
|
25 |
+
ins = gr.Markdown("""
|
26 |
+
# Usage Instructions
|
27 |
+
First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
|
28 |
+
Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
|
29 |
+
|
30 |
+
Choose between either 'hint' mode or 'editorial' mode. You have to reload the page to start a new conversation.
|
31 |
+
|
32 |
+
## Hint Mode
|
33 |
+
Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
|
34 |
+
|
35 |
+
|
36 |
+
## Editorial Mode
|
37 |
+
Chat with the bot about the editorial to the problem.
|
38 |
+
|
39 |
+
# Feedback
|
40 |
+
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.
|
41 |
+
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.
|
42 |
+
|
43 |
+
Thanks for using this bot, and I hope you found it helpful!
|
44 |
+
|
45 |
+
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 or GitHub. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
|
46 |
+
If you found this bot useful, please star the GitHub repo :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
47 |
+
""")
|
48 |
+
|
49 |
+
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
50 |
+
latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
|
51 |
+
msg = gr.Textbox(label = "Chat with the bot")
|
52 |
+
# clear = gr.ClearButton([msg, interface])
|
53 |
+
state = gr.State()
|
54 |
+
|
55 |
+
|
56 |
+
def cp_helper(message, history, state):
|
57 |
+
global chats, last_update
|
58 |
+
current_time = time.time()
|
59 |
+
day = 24 * 60 * 60
|
60 |
+
if (current_time - last_update >= day):
|
61 |
+
last_update=current_time
|
62 |
+
chats=1000
|
63 |
+
|
64 |
+
if (chats <= 0):
|
65 |
+
history.append((message, "Too many chats today, try again later."))
|
66 |
+
return "", history, state
|
67 |
+
|
68 |
+
chats-=1
|
69 |
+
|
70 |
+
global ind
|
71 |
+
if (state is None):
|
72 |
+
|
73 |
+
# state = {'chatgpt': None,
|
74 |
+
# 'problem_url': None,
|
75 |
+
# 'edi_url' : None,
|
76 |
+
# 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
|
77 |
+
# 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
|
78 |
+
# 'first' : True,
|
79 |
+
# 'style' : 'hint'}
|
80 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
|
81 |
+
# print('Edi', state['edi_text'])
|
82 |
+
# assert(False)
|
83 |
+
# state = {'chatgpt': None,
|
84 |
+
# 'problem_url': None,
|
85 |
+
# 'edi_url' : None,
|
86 |
+
# 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
|
87 |
+
# 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
|
88 |
+
# 'first' : True,
|
89 |
+
# 'style' : 'hint'}
|
90 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
|
91 |
+
# print(state['problem_text'], state['edi_text'])
|
92 |
+
ind = random.randint(1, 100000)
|
93 |
+
state = {'chatgpt': None,
|
94 |
+
'problem_url': None,
|
95 |
+
'edi_url' : None,
|
96 |
+
'problem_text' : '',
|
97 |
+
'edi_text' : '',
|
98 |
+
'first' : True,
|
99 |
+
'style' : None,
|
100 |
+
'id': '#' + str(ind)}
|
101 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
102 |
+
ind += 1
|
103 |
+
bot = ""
|
104 |
+
if (state['problem_text'] == ''):
|
105 |
+
try:
|
106 |
+
state['problem_url'] = message
|
107 |
+
if ('usaco' in message.lower()):
|
108 |
+
state['problem_text'] = usaco.problem(state['problem_url'])['statement']
|
109 |
+
elif ('codeforces' in message.lower()):
|
110 |
+
state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
|
111 |
+
else:
|
112 |
+
assert(False)
|
113 |
+
|
114 |
+
bot = "Thanks! Now could you provide a link to the editorial."
|
115 |
+
print(state['problem_text'])
|
116 |
+
except Exception as e:
|
117 |
+
print(e)
|
118 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
119 |
+
elif (state['edi_text'] == ''):
|
120 |
+
try:
|
121 |
+
state['edi_url'] = message
|
122 |
+
if ('usaco' in message.lower()):
|
123 |
+
# cbot = Chat(observation_generation)
|
124 |
+
state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
|
125 |
+
elif ('codeforces' in message.lower()):
|
126 |
+
# cbot = Chat(observation_generation)
|
127 |
+
state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
|
128 |
+
else:
|
129 |
+
assert(False)
|
130 |
+
|
131 |
+
assert(len(state['edi_text']))
|
132 |
+
print('EDITORIAL\n', state['edi_text'])
|
133 |
+
state['first']=True
|
134 |
+
|
135 |
+
bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
print(e)
|
139 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
140 |
+
|
141 |
+
elif (state['style'] == None):
|
142 |
+
message = message.lower()
|
143 |
+
if ('editorial' in message):
|
144 |
+
state['style'] = 'editorial'
|
145 |
+
state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
|
146 |
+
bot = state['edi_text'] + '\nLet me know if you have any questions.'
|
147 |
+
elif ('hint' in message):
|
148 |
+
state['style'] = 'hint'
|
149 |
+
state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
150 |
+
bot += "What are you current thoughts on the problem? "
|
151 |
+
else:
|
152 |
+
bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
if (len(bot) == 0):
|
157 |
+
if (state['style'] == 'hint'):
|
158 |
+
if (state['first']):
|
159 |
+
state['first']=False
|
160 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
|
161 |
+
else:
|
162 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
|
163 |
+
else:
|
164 |
+
state['first']=False
|
165 |
+
bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
|
166 |
+
|
167 |
+
|
168 |
+
history.append((message, bot))
|
169 |
+
|
170 |
+
chat_history = ''
|
171 |
+
for i in history:
|
172 |
+
chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
|
173 |
+
|
174 |
+
with open(CHAT_PATH + state['id'], 'w') as f:
|
175 |
+
f.write(chat_history)
|
176 |
+
|
177 |
+
return "", history, state
|
178 |
+
|
179 |
+
|
180 |
+
msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
|
181 |
+
|
182 |
+
def sub_rating(state, option):
|
183 |
+
if (state['id'] == None):
|
184 |
+
return
|
185 |
+
with open(CHAT_PATH + 'meta', 'a') as f:
|
186 |
+
f.write(f"{state['id']}: {option}\n")
|
187 |
+
|
188 |
+
return state, "Choose an Option"
|
189 |
+
|
190 |
+
rating = gr.Dropdown([
|
191 |
+
"Choose an Option",
|
192 |
+
"1 - Very Unhelpful",
|
193 |
+
"2 - Unhelpful",
|
194 |
+
"3 - Neutral",
|
195 |
+
"4 - Helpful",
|
196 |
+
"5 - Very Helpful"
|
197 |
+
], value = "Choose an Option", label='Rate the Conversation')
|
198 |
+
|
199 |
+
gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
|
200 |
+
|
201 |
+
def sub_feedback(state, message):
|
202 |
+
if (state['id'] == None):
|
203 |
+
return
|
204 |
+
with open(CHAT_PATH + state['id'], 'a') as f:
|
205 |
+
f.write(f"FEEDBACK: {message}\n\n\n\n")
|
206 |
+
with open(CHAT_PATH + 'feedback', 'a') as f:
|
207 |
+
f.write(f"FEEDBACK {state['id']}: {message}\n\n")
|
208 |
+
|
209 |
+
return state, ""
|
210 |
+
|
211 |
+
feedback = gr.Textbox(label = "Submit Feedback")
|
212 |
+
feedback.submit(sub_feedback, [state, feedback], [state, feedback])
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
+
demo.launch()
|
.history/app_20240731174133.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.scrapers import codeforces, usaco
|
2 |
+
from utils.chatbot import Chat
|
3 |
+
from utils.prompts import *
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
|
9 |
+
|
10 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
11 |
+
CHAT_PATH = dir_path + '/saved_chats/'
|
12 |
+
used_ids = os.listdir(CHAT_PATH)
|
13 |
+
|
14 |
+
ind = 0
|
15 |
+
for i in used_ids:
|
16 |
+
try:
|
17 |
+
ind = max(ind, int(i.split('#')[1]))
|
18 |
+
except:
|
19 |
+
pass
|
20 |
+
|
21 |
+
chats = 500
|
22 |
+
last_update = time.time()
|
23 |
+
|
24 |
+
with gr.Blocks(title = "CP Helper Bot") as demo:
|
25 |
+
ins = gr.Markdown("""
|
26 |
+
# Usage Instructions
|
27 |
+
First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
|
28 |
+
Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
|
29 |
+
|
30 |
+
Choose between either 'hint' mode or 'editorial' mode. You have to reload the page to start a new conversation, and conversations are not linked with an "account"
|
31 |
+
|
32 |
+
## Hint Mode
|
33 |
+
Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
|
34 |
+
|
35 |
+
|
36 |
+
## Editorial Mode
|
37 |
+
Chat with the bot about the editorial to the problem.
|
38 |
+
|
39 |
+
# Feedback
|
40 |
+
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.
|
41 |
+
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.
|
42 |
+
|
43 |
+
Thanks for using this bot, and I hope you found it helpful!
|
44 |
+
|
45 |
+
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 or GitHub. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
|
46 |
+
If you found this bot useful, please star the GitHub repo :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
47 |
+
""")
|
48 |
+
|
49 |
+
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
50 |
+
latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
|
51 |
+
msg = gr.Textbox(label = "Chat with the bot")
|
52 |
+
# clear = gr.ClearButton([msg, interface])
|
53 |
+
state = gr.State()
|
54 |
+
|
55 |
+
|
56 |
+
def cp_helper(message, history, state):
|
57 |
+
global chats, last_update
|
58 |
+
current_time = time.time()
|
59 |
+
day = 24 * 60 * 60
|
60 |
+
if (current_time - last_update >= day):
|
61 |
+
last_update=current_time
|
62 |
+
chats=1000
|
63 |
+
|
64 |
+
if (chats <= 0):
|
65 |
+
history.append((message, "Too many chats today, try again later."))
|
66 |
+
return "", history, state
|
67 |
+
|
68 |
+
chats-=1
|
69 |
+
|
70 |
+
global ind
|
71 |
+
if (state is None):
|
72 |
+
|
73 |
+
# state = {'chatgpt': None,
|
74 |
+
# 'problem_url': None,
|
75 |
+
# 'edi_url' : None,
|
76 |
+
# 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
|
77 |
+
# 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
|
78 |
+
# 'first' : True,
|
79 |
+
# 'style' : 'hint'}
|
80 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
|
81 |
+
# print('Edi', state['edi_text'])
|
82 |
+
# assert(False)
|
83 |
+
# state = {'chatgpt': None,
|
84 |
+
# 'problem_url': None,
|
85 |
+
# 'edi_url' : None,
|
86 |
+
# 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
|
87 |
+
# 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
|
88 |
+
# 'first' : True,
|
89 |
+
# 'style' : 'hint'}
|
90 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
|
91 |
+
# print(state['problem_text'], state['edi_text'])
|
92 |
+
ind = random.randint(1, 100000)
|
93 |
+
state = {'chatgpt': None,
|
94 |
+
'problem_url': None,
|
95 |
+
'edi_url' : None,
|
96 |
+
'problem_text' : '',
|
97 |
+
'edi_text' : '',
|
98 |
+
'first' : True,
|
99 |
+
'style' : None,
|
100 |
+
'id': '#' + str(ind)}
|
101 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
102 |
+
ind += 1
|
103 |
+
bot = ""
|
104 |
+
if (state['problem_text'] == ''):
|
105 |
+
try:
|
106 |
+
state['problem_url'] = message
|
107 |
+
if ('usaco' in message.lower()):
|
108 |
+
state['problem_text'] = usaco.problem(state['problem_url'])['statement']
|
109 |
+
elif ('codeforces' in message.lower()):
|
110 |
+
state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
|
111 |
+
else:
|
112 |
+
assert(False)
|
113 |
+
|
114 |
+
bot = "Thanks! Now could you provide a link to the editorial."
|
115 |
+
print(state['problem_text'])
|
116 |
+
except Exception as e:
|
117 |
+
print(e)
|
118 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
119 |
+
elif (state['edi_text'] == ''):
|
120 |
+
try:
|
121 |
+
state['edi_url'] = message
|
122 |
+
if ('usaco' in message.lower()):
|
123 |
+
# cbot = Chat(observation_generation)
|
124 |
+
state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
|
125 |
+
elif ('codeforces' in message.lower()):
|
126 |
+
# cbot = Chat(observation_generation)
|
127 |
+
state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
|
128 |
+
else:
|
129 |
+
assert(False)
|
130 |
+
|
131 |
+
assert(len(state['edi_text']))
|
132 |
+
print('EDITORIAL\n', state['edi_text'])
|
133 |
+
state['first']=True
|
134 |
+
|
135 |
+
bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
print(e)
|
139 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
140 |
+
|
141 |
+
elif (state['style'] == None):
|
142 |
+
message = message.lower()
|
143 |
+
if ('editorial' in message):
|
144 |
+
state['style'] = 'editorial'
|
145 |
+
state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
|
146 |
+
bot = state['edi_text'] + '\nLet me know if you have any questions.'
|
147 |
+
elif ('hint' in message):
|
148 |
+
state['style'] = 'hint'
|
149 |
+
state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
150 |
+
bot += "What are you current thoughts on the problem? "
|
151 |
+
else:
|
152 |
+
bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
if (len(bot) == 0):
|
157 |
+
if (state['style'] == 'hint'):
|
158 |
+
if (state['first']):
|
159 |
+
state['first']=False
|
160 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
|
161 |
+
else:
|
162 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
|
163 |
+
else:
|
164 |
+
state['first']=False
|
165 |
+
bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
|
166 |
+
|
167 |
+
|
168 |
+
history.append((message, bot))
|
169 |
+
|
170 |
+
chat_history = ''
|
171 |
+
for i in history:
|
172 |
+
chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
|
173 |
+
|
174 |
+
with open(CHAT_PATH + state['id'], 'w') as f:
|
175 |
+
f.write(chat_history)
|
176 |
+
|
177 |
+
return "", history, state
|
178 |
+
|
179 |
+
|
180 |
+
msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
|
181 |
+
|
182 |
+
def sub_rating(state, option):
|
183 |
+
if (state['id'] == None):
|
184 |
+
return
|
185 |
+
with open(CHAT_PATH + 'meta', 'a') as f:
|
186 |
+
f.write(f"{state['id']}: {option}\n")
|
187 |
+
|
188 |
+
return state, "Choose an Option"
|
189 |
+
|
190 |
+
rating = gr.Dropdown([
|
191 |
+
"Choose an Option",
|
192 |
+
"1 - Very Unhelpful",
|
193 |
+
"2 - Unhelpful",
|
194 |
+
"3 - Neutral",
|
195 |
+
"4 - Helpful",
|
196 |
+
"5 - Very Helpful"
|
197 |
+
], value = "Choose an Option", label='Rate the Conversation')
|
198 |
+
|
199 |
+
gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
|
200 |
+
|
201 |
+
def sub_feedback(state, message):
|
202 |
+
if (state['id'] == None):
|
203 |
+
return
|
204 |
+
with open(CHAT_PATH + state['id'], 'a') as f:
|
205 |
+
f.write(f"FEEDBACK: {message}\n\n\n\n")
|
206 |
+
with open(CHAT_PATH + 'feedback', 'a') as f:
|
207 |
+
f.write(f"FEEDBACK {state['id']}: {message}\n\n")
|
208 |
+
|
209 |
+
return state, ""
|
210 |
+
|
211 |
+
feedback = gr.Textbox(label = "Submit Feedback")
|
212 |
+
feedback.submit(sub_feedback, [state, feedback], [state, feedback])
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
+
demo.launch()
|
.history/app_20240731174136.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.scrapers import codeforces, usaco
|
2 |
+
from utils.chatbot import Chat
|
3 |
+
from utils.prompts import *
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
|
9 |
+
|
10 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
11 |
+
CHAT_PATH = dir_path + '/saved_chats/'
|
12 |
+
used_ids = os.listdir(CHAT_PATH)
|
13 |
+
|
14 |
+
ind = 0
|
15 |
+
for i in used_ids:
|
16 |
+
try:
|
17 |
+
ind = max(ind, int(i.split('#')[1]))
|
18 |
+
except:
|
19 |
+
pass
|
20 |
+
|
21 |
+
chats = 500
|
22 |
+
last_update = time.time()
|
23 |
+
|
24 |
+
with gr.Blocks(title = "CP Helper Bot") as demo:
|
25 |
+
ins = gr.Markdown("""
|
26 |
+
# Usage Instructions
|
27 |
+
First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
|
28 |
+
Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
|
29 |
+
|
30 |
+
Choose between either 'hint' mode or 'editorial' mode. You have to reload the page to start a new conversation, and conversations are not linked with an "account" or "session".
|
31 |
+
|
32 |
+
## Hint Mode
|
33 |
+
Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
|
34 |
+
|
35 |
+
|
36 |
+
## Editorial Mode
|
37 |
+
Chat with the bot about the editorial to the problem.
|
38 |
+
|
39 |
+
# Feedback
|
40 |
+
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.
|
41 |
+
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.
|
42 |
+
|
43 |
+
Thanks for using this bot, and I hope you found it helpful!
|
44 |
+
|
45 |
+
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 or GitHub. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
|
46 |
+
If you found this bot useful, please star the GitHub repo :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
47 |
+
""")
|
48 |
+
|
49 |
+
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
50 |
+
latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
|
51 |
+
msg = gr.Textbox(label = "Chat with the bot")
|
52 |
+
# clear = gr.ClearButton([msg, interface])
|
53 |
+
state = gr.State()
|
54 |
+
|
55 |
+
|
56 |
+
def cp_helper(message, history, state):
|
57 |
+
global chats, last_update
|
58 |
+
current_time = time.time()
|
59 |
+
day = 24 * 60 * 60
|
60 |
+
if (current_time - last_update >= day):
|
61 |
+
last_update=current_time
|
62 |
+
chats=1000
|
63 |
+
|
64 |
+
if (chats <= 0):
|
65 |
+
history.append((message, "Too many chats today, try again later."))
|
66 |
+
return "", history, state
|
67 |
+
|
68 |
+
chats-=1
|
69 |
+
|
70 |
+
global ind
|
71 |
+
if (state is None):
|
72 |
+
|
73 |
+
# state = {'chatgpt': None,
|
74 |
+
# 'problem_url': None,
|
75 |
+
# 'edi_url' : None,
|
76 |
+
# 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
|
77 |
+
# 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
|
78 |
+
# 'first' : True,
|
79 |
+
# 'style' : 'hint'}
|
80 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
|
81 |
+
# print('Edi', state['edi_text'])
|
82 |
+
# assert(False)
|
83 |
+
# state = {'chatgpt': None,
|
84 |
+
# 'problem_url': None,
|
85 |
+
# 'edi_url' : None,
|
86 |
+
# 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
|
87 |
+
# 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
|
88 |
+
# 'first' : True,
|
89 |
+
# 'style' : 'hint'}
|
90 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
|
91 |
+
# print(state['problem_text'], state['edi_text'])
|
92 |
+
ind = random.randint(1, 100000)
|
93 |
+
state = {'chatgpt': None,
|
94 |
+
'problem_url': None,
|
95 |
+
'edi_url' : None,
|
96 |
+
'problem_text' : '',
|
97 |
+
'edi_text' : '',
|
98 |
+
'first' : True,
|
99 |
+
'style' : None,
|
100 |
+
'id': '#' + str(ind)}
|
101 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
102 |
+
ind += 1
|
103 |
+
bot = ""
|
104 |
+
if (state['problem_text'] == ''):
|
105 |
+
try:
|
106 |
+
state['problem_url'] = message
|
107 |
+
if ('usaco' in message.lower()):
|
108 |
+
state['problem_text'] = usaco.problem(state['problem_url'])['statement']
|
109 |
+
elif ('codeforces' in message.lower()):
|
110 |
+
state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
|
111 |
+
else:
|
112 |
+
assert(False)
|
113 |
+
|
114 |
+
bot = "Thanks! Now could you provide a link to the editorial."
|
115 |
+
print(state['problem_text'])
|
116 |
+
except Exception as e:
|
117 |
+
print(e)
|
118 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
119 |
+
elif (state['edi_text'] == ''):
|
120 |
+
try:
|
121 |
+
state['edi_url'] = message
|
122 |
+
if ('usaco' in message.lower()):
|
123 |
+
# cbot = Chat(observation_generation)
|
124 |
+
state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
|
125 |
+
elif ('codeforces' in message.lower()):
|
126 |
+
# cbot = Chat(observation_generation)
|
127 |
+
state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
|
128 |
+
else:
|
129 |
+
assert(False)
|
130 |
+
|
131 |
+
assert(len(state['edi_text']))
|
132 |
+
print('EDITORIAL\n', state['edi_text'])
|
133 |
+
state['first']=True
|
134 |
+
|
135 |
+
bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
print(e)
|
139 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
140 |
+
|
141 |
+
elif (state['style'] == None):
|
142 |
+
message = message.lower()
|
143 |
+
if ('editorial' in message):
|
144 |
+
state['style'] = 'editorial'
|
145 |
+
state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
|
146 |
+
bot = state['edi_text'] + '\nLet me know if you have any questions.'
|
147 |
+
elif ('hint' in message):
|
148 |
+
state['style'] = 'hint'
|
149 |
+
state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
150 |
+
bot += "What are you current thoughts on the problem? "
|
151 |
+
else:
|
152 |
+
bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
if (len(bot) == 0):
|
157 |
+
if (state['style'] == 'hint'):
|
158 |
+
if (state['first']):
|
159 |
+
state['first']=False
|
160 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
|
161 |
+
else:
|
162 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
|
163 |
+
else:
|
164 |
+
state['first']=False
|
165 |
+
bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
|
166 |
+
|
167 |
+
|
168 |
+
history.append((message, bot))
|
169 |
+
|
170 |
+
chat_history = ''
|
171 |
+
for i in history:
|
172 |
+
chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
|
173 |
+
|
174 |
+
with open(CHAT_PATH + state['id'], 'w') as f:
|
175 |
+
f.write(chat_history)
|
176 |
+
|
177 |
+
return "", history, state
|
178 |
+
|
179 |
+
|
180 |
+
msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
|
181 |
+
|
182 |
+
def sub_rating(state, option):
|
183 |
+
if (state['id'] == None):
|
184 |
+
return
|
185 |
+
with open(CHAT_PATH + 'meta', 'a') as f:
|
186 |
+
f.write(f"{state['id']}: {option}\n")
|
187 |
+
|
188 |
+
return state, "Choose an Option"
|
189 |
+
|
190 |
+
rating = gr.Dropdown([
|
191 |
+
"Choose an Option",
|
192 |
+
"1 - Very Unhelpful",
|
193 |
+
"2 - Unhelpful",
|
194 |
+
"3 - Neutral",
|
195 |
+
"4 - Helpful",
|
196 |
+
"5 - Very Helpful"
|
197 |
+
], value = "Choose an Option", label='Rate the Conversation')
|
198 |
+
|
199 |
+
gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
|
200 |
+
|
201 |
+
def sub_feedback(state, message):
|
202 |
+
if (state['id'] == None):
|
203 |
+
return
|
204 |
+
with open(CHAT_PATH + state['id'], 'a') as f:
|
205 |
+
f.write(f"FEEDBACK: {message}\n\n\n\n")
|
206 |
+
with open(CHAT_PATH + 'feedback', 'a') as f:
|
207 |
+
f.write(f"FEEDBACK {state['id']}: {message}\n\n")
|
208 |
+
|
209 |
+
return state, ""
|
210 |
+
|
211 |
+
feedback = gr.Textbox(label = "Submit Feedback")
|
212 |
+
feedback.submit(sub_feedback, [state, feedback], [state, feedback])
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
+
demo.launch()
|
.history/app_20240731174137.py
ADDED
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from utils.scrapers import codeforces, usaco
|
2 |
+
from utils.chatbot import Chat
|
3 |
+
from utils.prompts import *
|
4 |
+
import os
|
5 |
+
import gradio as gr
|
6 |
+
import random
|
7 |
+
import time
|
8 |
+
|
9 |
+
|
10 |
+
dir_path = os.path.dirname(os.path.realpath(__file__))
|
11 |
+
CHAT_PATH = dir_path + '/saved_chats/'
|
12 |
+
used_ids = os.listdir(CHAT_PATH)
|
13 |
+
|
14 |
+
ind = 0
|
15 |
+
for i in used_ids:
|
16 |
+
try:
|
17 |
+
ind = max(ind, int(i.split('#')[1]))
|
18 |
+
except:
|
19 |
+
pass
|
20 |
+
|
21 |
+
chats = 500
|
22 |
+
last_update = time.time()
|
23 |
+
|
24 |
+
with gr.Blocks(title = "CP Helper Bot") as demo:
|
25 |
+
ins = gr.Markdown("""
|
26 |
+
# Usage Instructions
|
27 |
+
First, paste a valid USACO (https://usaco.org) or codeforces (https://codeforces.com) problem link into the first text box input.
|
28 |
+
Then, paste the link to an official editorial for the problem. Support for other platforms may (or may not) be coming soon.
|
29 |
+
|
30 |
+
Choose between either 'hint' mode or 'editorial' mode. You have to reload the page to start a new conversation, and conversations are not linked with an "account" or "session".
|
31 |
+
|
32 |
+
## Hint Mode
|
33 |
+
Chat with the bot about your ideas to solve the problem, and it will give you hints to guide your thinking.
|
34 |
+
|
35 |
+
|
36 |
+
## Editorial Mode
|
37 |
+
Chat with the bot about the editorial to the problem.
|
38 |
+
|
39 |
+
# Feedback
|
40 |
+
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.
|
41 |
+
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.
|
42 |
+
|
43 |
+
Thanks for using this bot, and I hope you found it helpful!
|
44 |
+
|
45 |
+
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 or GitHub. If you want to collaborate, feel free to DM me on discord @Rithwik#1821 or at rithwikca2020@gmail.com.
|
46 |
+
If you found this bot useful, please star the GitHub repo :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
47 |
+
""")
|
48 |
+
|
49 |
+
interface = gr.Chatbot(height='300px', placeholder="Provide a link for the problem you're solving.",
|
50 |
+
latex_delimiters = [{"left": "\\(", "right" : "\\)", 'display':False}, {"left": "$$$", "right" : "$$$", 'display':False}, {"left": "$", "right" : "$", 'display':False}])
|
51 |
+
msg = gr.Textbox(label = "Chat with the bot")
|
52 |
+
# clear = gr.ClearButton([msg, interface])
|
53 |
+
state = gr.State()
|
54 |
+
|
55 |
+
|
56 |
+
def cp_helper(message, history, state):
|
57 |
+
global chats, last_update
|
58 |
+
current_time = time.time()
|
59 |
+
day = 24 * 60 * 60
|
60 |
+
if (current_time - last_update >= day):
|
61 |
+
last_update=current_time
|
62 |
+
chats=1000
|
63 |
+
|
64 |
+
if (chats <= 0):
|
65 |
+
history.append((message, "Too many chats today, try again later."))
|
66 |
+
return "", history, state
|
67 |
+
|
68 |
+
chats-=1
|
69 |
+
|
70 |
+
global ind
|
71 |
+
if (state is None):
|
72 |
+
|
73 |
+
# state = {'chatgpt': None,
|
74 |
+
# 'problem_url': None,
|
75 |
+
# 'edi_url' : None,
|
76 |
+
# 'problem_text' : usaco.problem("https://usaco.org/index.php?page=viewproblem2&cpid=1421"),
|
77 |
+
# 'edi_text' : usaco.editorial("", "https://usaco.org/current/data/sol_prob3_bronze_open24.html"),
|
78 |
+
# 'first' : True,
|
79 |
+
# 'style' : 'hint'}
|
80 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) # HINT_MESSAGE(state['problem_text'], state['edi_text'])
|
81 |
+
# print('Edi', state['edi_text'])
|
82 |
+
# assert(False)
|
83 |
+
# state = {'chatgpt': None,
|
84 |
+
# 'problem_url': None,
|
85 |
+
# 'edi_url' : None,
|
86 |
+
# 'problem_text' : codeforces.problem("https://codeforces.com/contest/1592/problem/E"),
|
87 |
+
# 'edi_text' : codeforces.editorial("https://codeforces.com/contest/1592/problem/E", "https://codeforces.com/blog/entry/95583"),
|
88 |
+
# 'first' : True,
|
89 |
+
# 'style' : 'hint'}
|
90 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text'])) #
|
91 |
+
# print(state['problem_text'], state['edi_text'])
|
92 |
+
ind = random.randint(1, 100000)
|
93 |
+
state = {'chatgpt': None,
|
94 |
+
'problem_url': None,
|
95 |
+
'edi_url' : None,
|
96 |
+
'problem_text' : '',
|
97 |
+
'edi_text' : '',
|
98 |
+
'first' : True,
|
99 |
+
'style' : None,
|
100 |
+
'id': '#' + str(ind)}
|
101 |
+
# state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
102 |
+
ind += 1
|
103 |
+
bot = ""
|
104 |
+
if (state['problem_text'] == ''):
|
105 |
+
try:
|
106 |
+
state['problem_url'] = message
|
107 |
+
if ('usaco' in message.lower()):
|
108 |
+
state['problem_text'] = usaco.problem(state['problem_url'])['statement']
|
109 |
+
elif ('codeforces' in message.lower()):
|
110 |
+
state['problem_text'] = codeforces.problem(state['problem_url'])['statement']
|
111 |
+
else:
|
112 |
+
assert(False)
|
113 |
+
|
114 |
+
bot = "Thanks! Now could you provide a link to the editorial."
|
115 |
+
print(state['problem_text'])
|
116 |
+
except Exception as e:
|
117 |
+
print(e)
|
118 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
119 |
+
elif (state['edi_text'] == ''):
|
120 |
+
try:
|
121 |
+
state['edi_url'] = message
|
122 |
+
if ('usaco' in message.lower()):
|
123 |
+
# cbot = Chat(observation_generation)
|
124 |
+
state['edi_text'] = usaco.editorial(state['problem_url'], state['edi_url'])
|
125 |
+
elif ('codeforces' in message.lower()):
|
126 |
+
# cbot = Chat(observation_generation)
|
127 |
+
state['edi_text'] = codeforces.editorial(state['problem_url'], state['edi_url'])
|
128 |
+
else:
|
129 |
+
assert(False)
|
130 |
+
|
131 |
+
assert(len(state['edi_text']))
|
132 |
+
print('EDITORIAL\n', state['edi_text'])
|
133 |
+
state['first']=True
|
134 |
+
|
135 |
+
bot = "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
136 |
+
|
137 |
+
except Exception as e:
|
138 |
+
print(e)
|
139 |
+
bot = "There was something wrong with the link. Could you paste it again?"
|
140 |
+
|
141 |
+
elif (state['style'] == None):
|
142 |
+
message = message.lower()
|
143 |
+
if ('editorial' in message):
|
144 |
+
state['style'] = 'editorial'
|
145 |
+
state['chatgpt'] = Chat(EDI_MESSAGE(state['problem_text'], state['edi_text']))
|
146 |
+
bot = state['edi_text'] + '\nLet me know if you have any questions.'
|
147 |
+
elif ('hint' in message):
|
148 |
+
state['style'] = 'hint'
|
149 |
+
state['chatgpt'] = Chat(HINT_MESSAGE(state['problem_text'], state['edi_text']))
|
150 |
+
bot += "What are you current thoughts on the problem? "
|
151 |
+
else:
|
152 |
+
bot += "Would you like hints to help you solve the problem or an interactive explanation of the editorial (hint/editorial)? "
|
153 |
+
|
154 |
+
|
155 |
+
|
156 |
+
if (len(bot) == 0):
|
157 |
+
if (state['style'] == 'hint'):
|
158 |
+
if (state['first']):
|
159 |
+
state['first']=False
|
160 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))
|
161 |
+
else:
|
162 |
+
bot = state['chatgpt'].chat(HINT_TEXT(message, state['problem_text'], state['edi_text']))# + '\nExplain everything as if I have never seen the problem or editorial before in a maximum of 2 paragraphs.')
|
163 |
+
else:
|
164 |
+
state['first']=False
|
165 |
+
bot = state['chatgpt'].chat(message + '\nExplain everything in a maximum of 2 paragraphs.')
|
166 |
+
|
167 |
+
|
168 |
+
history.append((message, bot))
|
169 |
+
|
170 |
+
chat_history = ''
|
171 |
+
for i in history:
|
172 |
+
chat_history += (f"USER: {i[0]}\n\nBOT: {i[1]}\n\n\n\n");
|
173 |
+
|
174 |
+
with open(CHAT_PATH + state['id'], 'w') as f:
|
175 |
+
f.write(chat_history)
|
176 |
+
|
177 |
+
return "", history, state
|
178 |
+
|
179 |
+
|
180 |
+
msg.submit(cp_helper, [msg, interface, state], [msg, interface, state])
|
181 |
+
|
182 |
+
def sub_rating(state, option):
|
183 |
+
if (state['id'] == None):
|
184 |
+
return
|
185 |
+
with open(CHAT_PATH + 'meta', 'a') as f:
|
186 |
+
f.write(f"{state['id']}: {option}\n")
|
187 |
+
|
188 |
+
return state, "Choose an Option"
|
189 |
+
|
190 |
+
rating = gr.Dropdown([
|
191 |
+
"Choose an Option",
|
192 |
+
"1 - Very Unhelpful",
|
193 |
+
"2 - Unhelpful",
|
194 |
+
"3 - Neutral",
|
195 |
+
"4 - Helpful",
|
196 |
+
"5 - Very Helpful"
|
197 |
+
], value = "Choose an Option", label='Rate the Conversation')
|
198 |
+
|
199 |
+
gr.Interface(sub_rating, [state, rating], [state, rating], allow_flagging=False)
|
200 |
+
|
201 |
+
def sub_feedback(state, message):
|
202 |
+
if (state['id'] == None):
|
203 |
+
return
|
204 |
+
with open(CHAT_PATH + state['id'], 'a') as f:
|
205 |
+
f.write(f"FEEDBACK: {message}\n\n\n\n")
|
206 |
+
with open(CHAT_PATH + 'feedback', 'a') as f:
|
207 |
+
f.write(f"FEEDBACK {state['id']}: {message}\n\n")
|
208 |
+
|
209 |
+
return state, ""
|
210 |
+
|
211 |
+
feedback = gr.Textbox(label = "Submit Feedback")
|
212 |
+
feedback.submit(sub_feedback, [state, feedback], [state, feedback])
|
213 |
+
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
+
demo.launch()
|
.history/app_20240731174340.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. You have to reload the page to start a new conversation, and conversations are not linked with an "account" or "session".
|
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 or GitHub. 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 :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
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_20240731174352.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. You have to reload the page to start a new conversation, and conversations are not linked with an "account" or "session".
|
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 or GitHub. 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 :) github.com/Rithwik-G/Competitive-Programming-Bot.
|
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()
|
app.py
CHANGED
@@ -27,9 +27,9 @@ with gr.Blocks(title = "CP Helper Bot") as demo:
|
|
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 |
|
|
|
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. You have to reload the page to start a new conversation, and conversations are not linked with an "account" or "session".
|
|
|
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 |
|