Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
The problem is that you're splitting the GET parameter using "##" even though the negative parameter value doesn’t contain that delimiter. This means negative ends up as a one‐element list (with the entire string) that doesn’t match any titles. (For example, positive contains an element that exactly equals one title from your list, but negative never does.) One change to fix it would be to either: • Pass multiple negative values from the client (so that the GET request includes "negative=…&negative=…") and then use request.args.getlist('negative') instead of splitting on "##", or • Change the delimiter in the split to one that is actually present in the negative value. Here’s one example fixing it using getlist: @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '').split("##") negative = request.args.getlist('negative') # expects multiple negative parameters keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) This change ensures that negative is processed as a proper list and that you'll see the expected "OK 1 1" when there's a match.
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? The problem is that you're splitting the GET parameter using "##" even though the negative parameter value doesn’t contain that delimiter. This means negative ends up as a one‐element list (with the entire string) that doesn’t match any titles. (For example, positive contains an element that exactly equals one title from your list, but negative never does.) One change to fix it would be to either: • Pass multiple negative values from the client (so that the GET request includes "negative=…&negative=…") and then use request.args.getlist('negative') instead of splitting on "##", or • Change the delimiter in the split to one that is actually present in the negative value. Here’s one example fixing it using getlist: @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args', request.args) positive = request.args.get('positive', '').split("##") negative = request.args.getlist('negative') # expects multiple negative parameters keep_yes = [i for i, x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i, x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) This change ensures that negative is processed as a proper list and that you'll see the expected "OK 1 1" when there's a match. Answer either 'The student passes' or 'The student fails'.
The student fails.
Testing if the previous output contains the string student passes
: False
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Why isn't this working
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @app.route('/getsimilar', methods=['GET']) def getsimilar(): print('args',request.args) positive = request.args.get('positive', '') positive = positive.split("##") negative = request.args.get('negative', '') negative = negative.split("##") keep_yes = [i for i,x in enumerate(titles) if x in positive] set1_yes = emb[keep_yes] keep_no = [i for i,x in enumerate(titles) if x in negative] set1_no = emb[keep_no] print("OK", len(set1_yes), len(set1_no)) |
1 2 3 | args ImmutableMultiDict([('positive', 'Title: Towards Evaluating the Robustness of Neural Networks Authors: Nicholas Carlini; David Wagner'), ('negative', 'Title: On Evaluating Adversarial Robustness Authors: Nicholas Carlini; Anish Athalye; Nicolas Papernot; Wieland Brendel; Jonas Rauber; Dimitris Tsipras; Ian Goodfellow; Aleksander Madry; Alexey Kurakin}')]) OK 1 0 |
Reason: The negative parameter’s string isn’t being split into the expected parts because the delimiter "##" isn’t present in the query string, so the resulting list contains one long string that doesn’t exactly match any title in your titles list. Fix: Either change the delimiter in the split (or remove it altogether) so that negative’s string matches the title format—e.g., if the delimiter is a newline, use .split("\n") instead of .split("##").
I asked a question about why some code was incorrect. The answer is "because the input contains an extra curly brace }". Does this student answer say that? Reason: The negative parameter’s string isn’t being split into the expected parts because the delimiter "##" isn’t present in the query string, so the resulting list contains one long string that doesn’t exactly match any title in your titles list. Fix: Either change the delimiter in the split (or remove it altogether) so that negative’s string matches the title format—e.g., if the delimiter is a newline, use .split("\n") instead of .split("##"). Answer either 'The student passes' or 'The student fails'.
The student fails.
Testing if the previous output contains the string student passes
: False