Update app.py
Browse files11233 unmodified lines78911101113121314161715161719181920212223262425262 unmodified lines293031353233349 unmodified lines44454651474849505157525354!pip install tensorflowimport gradio as grimport numpy as npfrom tensorflow.keras.models import load_model, save_model3 unmodified linesimport json
# Load the pre-trained model and tokenizermodel = load_model("code_language_cnn.keras") # Replace with your model pathmodel = load_model("code_language_cnn.keras") with open("tokenizer.json", "r") as f: tokenizer_data = f.read() # Read the JSON file as a string tokenizer_data = f.read() tokenizer = tokenizer_from_json(tokenizer_data)
max_sequence_length = 500 # Adjust based on your modellanguages = ["C", "C++", "JAVA", "Python"] # Replace with your language labelsmax_sequence_length = 500 languages = ["C", "C++", "JAVA", "Python"]
# Load or initialize feedback datatry: with open("feedback.json", "r") as f: feedback_data = json.load(f)except FileNotFoundError: feedback_data = []
# Define the prediction functiondef predict_language(code_snippet): seq = tokenizer.texts_to_sequences([code_snippet]) padded_seq = pad_sequences(seq, maxlen=max_sequence_length, padding='post', truncating='post')2 unmodified lines predicted_language = languages[np.argmax(predictions)] return predicted_language, confidence_scores
# Feedback handling functiondef provide_feedback(code_snippet, predicted_language, feedback, correct_language=None): global feedback_data
9 unmodified lines with open("feedback.json", "w") as f: json.dump(feedback_data, f, indent=4)
# If feedback is "Incorrect", retrain the model if feedback == "Incorrect": retrain_model(code_snippet, correct_language)
return "Thank you for your feedback!"
# Retraining the model based on feedbackdef retrain_model(): global model # Prepare the feedback data (new training data)app.py+4-10