Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
from tensorflow.keras.models import load_model, save_model
|
| 4 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
| 5 |
+
from tensorflow.keras.preprocessing.text import tokenizer_from_json
|
| 6 |
+
from tensorflow.keras.utils import to_categorical
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Load the pre-trained model and tokenizer
|
| 10 |
+
model = load_model("code_language_cnn.keras") # Replace with your model path
|
| 11 |
+
with open("tokenizer.json", "r") as f:
|
| 12 |
+
tokenizer_data = f.read() # Read the JSON file as a string
|
| 13 |
+
tokenizer = tokenizer_from_json(tokenizer_data)
|
| 14 |
+
|
| 15 |
+
max_sequence_length = 500 # Adjust based on your model
|
| 16 |
+
languages = ["C", "C++", "JAVA", "Python"] # Replace with your language labels
|
| 17 |
+
|
| 18 |
+
# Load or initialize feedback data
|
| 19 |
+
try:
|
| 20 |
+
with open("feedback.json", "r") as f:
|
| 21 |
+
feedback_data = json.load(f)
|
| 22 |
+
except FileNotFoundError:
|
| 23 |
+
feedback_data = []
|
| 24 |
+
|
| 25 |
+
# Define the prediction function
|
| 26 |
+
def predict_language(code_snippet):
|
| 27 |
+
seq = tokenizer.texts_to_sequences([code_snippet])
|
| 28 |
+
padded_seq = pad_sequences(seq, maxlen=max_sequence_length, padding='post', truncating='post')
|
| 29 |
+
predictions = model.predict(padded_seq)[0]
|
| 30 |
+
confidence_scores = {languages[i]: f"{predictions[i] * 100:.2f}%" for i in range(len(languages))}
|
| 31 |
+
predicted_language = languages[np.argmax(predictions)]
|
| 32 |
+
return predicted_language, confidence_scores
|
| 33 |
+
|
| 34 |
+
# Feedback handling function
|
| 35 |
+
def provide_feedback(code_snippet, predicted_language, feedback, correct_language=None):
|
| 36 |
+
global feedback_data
|
| 37 |
+
|
| 38 |
+
feedback_entry = {
|
| 39 |
+
"code": code_snippet,
|
| 40 |
+
"predicted_language": predicted_language,
|
| 41 |
+
"feedback": feedback,
|
| 42 |
+
"correct_language": correct_language if feedback == "Incorrect" else predicted_language
|
| 43 |
+
}
|
| 44 |
+
feedback_data.append(feedback_entry)
|
| 45 |
+
|
| 46 |
+
# Save feedback to file
|
| 47 |
+
with open("feedback.json", "w") as f:
|
| 48 |
+
json.dump(feedback_data, f, indent=4)
|
| 49 |
+
|
| 50 |
+
# If feedback is "Incorrect", retrain the model
|
| 51 |
+
if feedback == "Incorrect":
|
| 52 |
+
retrain_model(code_snippet, correct_language)
|
| 53 |
+
|
| 54 |
+
return "Thank you for your feedback!"
|
| 55 |
+
|
| 56 |
+
# Retraining the model based on feedback
|
| 57 |
+
def retrain_model():
|
| 58 |
+
global model
|
| 59 |
+
# Prepare the feedback data (new training data)
|
| 60 |
+
if feedback_data.count("Incorrect") < 10: # Minimum 10 incorrect feedbacks required to retrain
|
| 61 |
+
return
|
| 62 |
+
feedback_texts = [entry["code"] for entry in feedback_data]
|
| 63 |
+
feedback_labels = [entry["correct_language"] for entry in feedback_data]
|
| 64 |
+
|
| 65 |
+
# Tokenize and pad the new data
|
| 66 |
+
seq = tokenizer.texts_to_sequences(feedback_texts)
|
| 67 |
+
padded_seq = pad_sequences(seq, maxlen=max_sequence_length, padding='post', truncating='post')
|
| 68 |
+
|
| 69 |
+
# Convert labels to categorical (one-hot encoding)
|
| 70 |
+
labels = [languages.index(lang) for lang in feedback_labels]
|
| 71 |
+
labels = to_categorical(labels, num_classes=len(languages))
|
| 72 |
+
|
| 73 |
+
# Retrain the model
|
| 74 |
+
model.fit(padded_seq, labels, epochs=2, batch_size=32, verbose=1)
|
| 75 |
+
feedback_data = [] # Clear the feedback data after retraining
|
| 76 |
+
# Save the retrained model
|
| 77 |
+
# model.save("code_language_cnn_retrained.keras")
|
| 78 |
+
print("Model retrained")
|
| 79 |
+
|
| 80 |
+
# Define Gradio components
|
| 81 |
+
def interface_func(code_snippet):
|
| 82 |
+
predicted_language, confidence_scores = predict_language(code_snippet)
|
| 83 |
+
return predicted_language, confidence_scores
|
| 84 |
+
|
| 85 |
+
# Build Gradio interface
|
| 86 |
+
with gr.Blocks() as demo:
|
| 87 |
+
gr.Markdown("### Programming Language Detection with Feedback")
|
| 88 |
+
code_input = gr.Textbox(label="Enter Code Snippet")
|
| 89 |
+
predict_button = gr.Button("Predict")
|
| 90 |
+
predicted_label = gr.Label(label="Predicted Language")
|
| 91 |
+
confidence_output = gr.JSON(label="Confidence Scores")
|
| 92 |
+
|
| 93 |
+
feedback_dropdown = gr.Radio(["Correct", "Incorrect"], label="Was the prediction correct?")
|
| 94 |
+
correct_language_dropdown = gr.Dropdown(languages, label="If incorrect, select the correct language (optional)")
|
| 95 |
+
feedback_button = gr.Button("Submit Feedback")
|
| 96 |
+
feedback_message = gr.Label(label="Feedback Status")
|
| 97 |
+
|
| 98 |
+
# Prediction workflow
|
| 99 |
+
predict_button.click(
|
| 100 |
+
interface_func,
|
| 101 |
+
inputs=[code_input],
|
| 102 |
+
outputs=[predicted_label, confidence_output]
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Feedback workflow
|
| 106 |
+
feedback_button.click(
|
| 107 |
+
provide_feedback,
|
| 108 |
+
inputs=[code_input, predicted_label, feedback_dropdown, correct_language_dropdown],
|
| 109 |
+
outputs=[feedback_message]
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
# Launch the interface
|
| 113 |
+
demo.launch(share=True)
|