[Python] I am getting a GET 404 error and can't find the problem

Discussão em 'Python' iniciado por Stack, Março 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am working on a project and can't seem to fix this GET 404 error. In the console it looks like there it is messing up somewhere in between the javascript and html file, but I haven't been able to pinpoint the issue. If anyone has any advice it'd be super appreciated.

    HTML code: file name > index.html

    <html>
    <head>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <script src="../static/mywebscript.js"></script>
    <style>
    .center-heading {
    text-align: center;
    }
    </style>
    </head>
    <body>
    <div class="card">
    <div class="card-body">
    <h1 class="center-heading">NLP - Emotion Detection</h1>
    <div style="padding: 25px 25px 25px 25px;">
    <h2 class="mb-3">
    <label class="form-label">Please enter the text to be analyzed</label>
    <input type="textarea" class="form-control" id="textToAnalyze">
    </h2>

    <div style="padding: 25px 25px 25px 25px;">
    <button onclick="RunSentimentAnalysis()" class="btn btn-secondary">Run Sentiment Analysis</button>
    </div>
    <div style="padding: 25px 25px 25px 25px;"></div>
    <h2 class="mb-3">
    <label class="form-label">Result of Emotion Detection</label>
    </h2>

    <div id="system_response" style="padding: 25px 25px 25px 25px;"></div>
    </div>
    </div>
    </body>
    </html>


    JavaScript code: file name > mywebscript.js

    let RunSentimentAnalysis = ()=>{
    textToAnalyze = document.getElementById("textToAnalyze").value;

    let xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
    document.getElementById("system_response").innerHTML = xhttp.responseText;
    }
    };
    xhttp.open("GET", "emotionDetector?textToAnalyze"+"="+textToAnalyze, true);
    xhttp.send();
    }


    Python code: file name > emotion_detection.py

    import requests
    import json

    def emotion_detector(text_to_analyze):
    url ='https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict'
    header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"}
    input_json = { "raw_document": { "text": text_to_analyze } }
    response = requests.post(url, json=input_json, headers=header)
    status_code = response.status_code

    emotions = {}

    if status_code == 200:
    formatted_response = json.loads(response.text)
    emotions = formatted_response['emotionPredictions'][0]['emotion']
    dominant_emotion = max(emotions.items(), key=lambda x: x[1])
    emotions['dominant_emotion'] = dominant_emotion[0]
    elif status_code == 400:
    emotions['anger'] = None
    emotions['disgust'] = None
    emotions['fear'] = None
    emotions['joy'] = None
    emotions['sadness'] = None
    emotions['dominant_emotion'] = None
    return emotions


    and file name > server.py

    from flask import Flask, request, render_template
    from EmotionDetection.emotion_detection import emotion_detector

    app = Flask("Emotion Detector")

    @app.route("/emotionDetector")
    def emotion_analyzer():
    text_to_analyse = request.args.get('textToAnalyze')
    emotion_result = emotion_detector(text_to_analyse)
    anger = emotion_result['anger']
    disgust = emotion_result['disgust']
    fear = emotion_result['fear']
    joy = emotion_result['joy']
    sadness = emotion_result['sadness']
    dominant_emotion = emotion_result['dominant_emotion']

    if dominant_emotion is None:
    return "Invalid text! Please try again"

    response_str = f"""For the given statement, the system response is
    'anger': {anger}, 'disgust': {disgust}, 'fear': {fear}, 'joy': {joy}, 'sadness': {sadness}.
    The dominant emotion is <strong>{dominant_emotion}</strong>."""
    return response_str

    @app.route("/")
    def render_index_page():
    return render_template('index.html')

    if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

    Continue reading...

Compartilhe esta Página