aabdoo234 commited on
Commit
7d05134
·
verified ·
1 Parent(s): ae85728

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -10
app.py CHANGED
@@ -1,4 +1,3 @@
1
- !pip install tensorflow
2
  import gradio as gr
3
  import numpy as np
4
  from tensorflow.keras.models import load_model, save_model
@@ -8,22 +7,20 @@ from tensorflow.keras.utils import to_categorical
8
  import json
9
 
10
  # Load the pre-trained model and tokenizer
11
- model = load_model("code_language_cnn.keras") # Replace with your model path
12
  with open("tokenizer.json", "r") as f:
13
- tokenizer_data = f.read() # Read the JSON file as a string
14
  tokenizer = tokenizer_from_json(tokenizer_data)
15
 
16
- max_sequence_length = 500 # Adjust based on your model
17
- languages = ["C", "C++", "JAVA", "Python"] # Replace with your language labels
18
 
19
- # Load or initialize feedback data
20
  try:
21
  with open("feedback.json", "r") as f:
22
  feedback_data = json.load(f)
23
  except FileNotFoundError:
24
  feedback_data = []
25
 
26
- # Define the prediction function
27
  def predict_language(code_snippet):
28
  seq = tokenizer.texts_to_sequences([code_snippet])
29
  padded_seq = pad_sequences(seq, maxlen=max_sequence_length, padding='post', truncating='post')
@@ -32,7 +29,6 @@ def predict_language(code_snippet):
32
  predicted_language = languages[np.argmax(predictions)]
33
  return predicted_language, confidence_scores
34
 
35
- # Feedback handling function
36
  def provide_feedback(code_snippet, predicted_language, feedback, correct_language=None):
37
  global feedback_data
38
 
@@ -48,13 +44,11 @@ def provide_feedback(code_snippet, predicted_language, feedback, correct_languag
48
  with open("feedback.json", "w") as f:
49
  json.dump(feedback_data, f, indent=4)
50
 
51
- # If feedback is "Incorrect", retrain the model
52
  if feedback == "Incorrect":
53
  retrain_model(code_snippet, correct_language)
54
 
55
  return "Thank you for your feedback!"
56
 
57
- # Retraining the model based on feedback
58
  def retrain_model():
59
  global model
60
  # Prepare the feedback data (new training data)
 
 
1
  import gradio as gr
2
  import numpy as np
3
  from tensorflow.keras.models import load_model, save_model
 
7
  import json
8
 
9
  # Load the pre-trained model and tokenizer
10
+ model = load_model("code_language_cnn.keras")
11
  with open("tokenizer.json", "r") as f:
12
+ tokenizer_data = f.read()
13
  tokenizer = tokenizer_from_json(tokenizer_data)
14
 
15
+ max_sequence_length = 500
16
+ languages = ["C", "C++", "JAVA", "Python"]
17
 
 
18
  try:
19
  with open("feedback.json", "r") as f:
20
  feedback_data = json.load(f)
21
  except FileNotFoundError:
22
  feedback_data = []
23
 
 
24
  def predict_language(code_snippet):
25
  seq = tokenizer.texts_to_sequences([code_snippet])
26
  padded_seq = pad_sequences(seq, maxlen=max_sequence_length, padding='post', truncating='post')
 
29
  predicted_language = languages[np.argmax(predictions)]
30
  return predicted_language, confidence_scores
31
 
 
32
  def provide_feedback(code_snippet, predicted_language, feedback, correct_language=None):
33
  global feedback_data
34
 
 
44
  with open("feedback.json", "w") as f:
45
  json.dump(feedback_data, f, indent=4)
46
 
 
47
  if feedback == "Incorrect":
48
  retrain_model(code_snippet, correct_language)
49
 
50
  return "Thank you for your feedback!"
51
 
 
52
  def retrain_model():
53
  global model
54
  # Prepare the feedback data (new training data)