ananya-prakash commited on
Commit
71b6169
1 Parent(s): d306985

Upload 5 files

Browse files
Files changed (5) hide show
  1. AI-only_model.pkl +3 -0
  2. Dockerfile +21 -0
  3. Human-in-the-loop_model.pkl +3 -0
  4. app.py +118 -0
  5. requirements.txt +8 -0
AI-only_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f96b099ef6175584823e331e985de53af52d50a7e22617d9308a25196db69c58
3
+ size 267873906
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Use the official Python image as a base image
2
+ FROM python:3.9
3
+
4
+ WORKDIR /code
5
+
6
+ COPY ./requirements.txt /code/requirements.txt
7
+
8
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
9
+
10
+ COPY . .
11
+
12
+ # Set environment variables
13
+ ENV FLASK_APP=app.py
14
+ ENV FLASK_RUN_HOST=0.0.0.0
15
+ ENV FLASK_RUN_PORT=5001
16
+
17
+ # Expose port 5000 to allow external connections
18
+ EXPOSE 5001
19
+
20
+ # Run the Flask app
21
+ CMD ["flask", "run"]
Human-in-the-loop_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f22501f82dbd0fc88295abe5b828b302f12a67350521d36eb1f456f097188e30
3
+ size 267873906
app.py ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify, request, send_from_directory
2
+ from flask_cors import CORS
3
+ import pandas as pd
4
+ import os
5
+ import pickle
6
+ from transformers import DistilBertTokenizer
7
+ import torch
8
+ import numpy as np
9
+ # from your_model import load_model, predict_response # assuming these functions are implemented
10
+
11
+ app = Flask(__name__)
12
+
13
+ # Load your machine learning model
14
+ # ai_only = load_model('ai_model_path.pkl')
15
+ # hitl = load_model('hitl_model_path.pkl')
16
+
17
+ # Load your questions and responses
18
+ questions_df = pd.read_csv('data/questions.csv')
19
+ # responses_df = pd.read_csv('data/responses.csv')
20
+
21
+ CORS(app, resources={r"/api/*": {"origins": "*"}})
22
+
23
+ react_folder= 'frontend'
24
+ directory= os.getcwd()+ f'/{react_folder}/build/static/'
25
+
26
+ def load_model(name):
27
+ print("Loading model")
28
+
29
+ with open(f'{name}_model.pkl', 'rb') as f:
30
+ loaded_model = pickle.load(f)
31
+ print("model loaded successfully")
32
+ return loaded_model
33
+
34
+
35
+ def make_prediction(model, data):
36
+ model= load_model(model)
37
+
38
+ tokenizer = DistilBertTokenizer.from_pretrained('distilbert-base-uncased')
39
+ inputs = tokenizer(data, return_tensors="pt", truncation=True, padding=True, max_length=136)
40
+
41
+ # Forward pass through the model to obtain logits
42
+ with torch.no_grad():
43
+ outputs = model(**inputs)
44
+
45
+ # Get predicted probabilities using softmax function
46
+ probs = torch.softmax(outputs.logits, dim=1)
47
+
48
+ # Get predicted class (0 or 1)
49
+ predicted_class = torch.argmax(probs, dim=1).item()
50
+
51
+ # Map predicted class to corresponding label
52
+ predicted_label = "AI" if predicted_class == 0 else "Human"
53
+ print("Predicted Label:", predicted_label)
54
+ print("Probability:", probs)
55
+
56
+ return [predicted_label, max(probs[0][0], probs[0][1])]
57
+
58
+
59
+
60
+
61
+
62
+ @app.route('/DilemmaAI', endpoint='func1')
63
+ def index():
64
+ path = os.getcwd()+f'/{react_folder}/build'
65
+ print(path)
66
+ return send_from_directory(directory=path, path='index.html')
67
+
68
+ @app.route('/static/<folder>/<file>')
69
+ def css(folder,file):
70
+ path = folder+'/'+file
71
+ return send_from_directory(directory= directory, path= path)
72
+
73
+ @app.route('/assets/<file>', endpoint='func2')
74
+ def css(folder,file):
75
+ directory = os.getcwd()+f'/{react_folder}/build/assets'
76
+ path = file
77
+ return send_from_directory(directory=directory, path= path)
78
+
79
+ # @app.route('/api/questions', methods=['GET'])
80
+ # def get_questions():
81
+ # # Sending a list of questions along with their IDs
82
+ # questions = questions_df[['question_id', 'question']].to_dict(orient='records')
83
+ # return jsonify(questions)
84
+
85
+
86
+ @app.route('/api/random-question', methods=['GET'])
87
+ def get_random_question():
88
+ # Fetching a random question from the dataframe
89
+ random_question = questions_df.sample().iloc[0][1]
90
+ print(random_question)
91
+ return jsonify(random_question)
92
+
93
+ @app.route('/api/predict', methods=['POST'])
94
+ def predict():
95
+ # Get response from the request
96
+ data = request.json
97
+ response_text = data['response']
98
+ model_type = data['mode']
99
+ print('input = ', response_text)
100
+ print('mode=',model_type)
101
+
102
+ #make_prediction(model_type,response_text)
103
+ # Perform prediction
104
+ # prediction_label, confidence = predict_response(model, response_text)
105
+ prediction_label, confidence = make_prediction(model_type, response_text)
106
+ if not prediction_label:
107
+ prediction_label = 'AI'
108
+ if not confidence:
109
+ confidence = 0
110
+
111
+ return jsonify({
112
+ 'prediction': prediction_label,
113
+ 'confidence': f'{confidence*100:.2f}%'
114
+ })
115
+
116
+
117
+ if __name__ == '__main__':
118
+ app.run(host='0.0.0.0', port=5001)
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ python-dotenv
2
+ flask == 2.2.2
3
+ werkzeug == 2.2.2
4
+ flask-cors
5
+ pandas
6
+ torch
7
+ transformers == 4.40.0
8
+