zzarif commited on
Commit
25a3fc8
·
1 Parent(s): 8816669

Initial commit

Browse files
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🐨
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
- sdk_version: 4.43.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
 
4
  colorFrom: purple
5
  colorTo: yellow
6
  sdk: gradio
7
+ sdk_version: 3.50.0
8
  app_file: app.py
9
  pinned: false
10
  license: mit
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import onnxruntime as ort
3
+ from transformers import AutoTokenizer
4
+ import numpy as np
5
+
6
+ # Load the ONNX model
7
+ onnx_model_path = "fine-tuned_all-distilroberta-v1_quantized.onnx"
8
+ ort_session = ort.InferenceSession(onnx_model_path)
9
+
10
+ # Load the tokenizer
11
+ tokenizer = AutoTokenizer.from_pretrained(
12
+ "sentence-transformers/all-MiniLM-L6-v2")
13
+
14
+
15
+ def predict_similarity(question, candidate_answer, ai_answer):
16
+ # Combine question and answers
17
+ candidate_combined = f"Question: {question} Answer: {candidate_answer}"
18
+ ai_combined = f"Question: {question} Answer: {ai_answer}"
19
+
20
+ # Tokenize inputs
21
+ inputs = tokenizer([candidate_combined, ai_combined],
22
+ padding=True, truncation=True, return_tensors="np")
23
+
24
+ # Run inference
25
+ ort_inputs = {
26
+ "input_ids": inputs["input_ids"],
27
+ "attention_mask": inputs["attention_mask"]
28
+ }
29
+ ort_outputs = ort_session.run(None, ort_inputs)
30
+
31
+ # Calculate cosine similarity
32
+ embeddings = ort_outputs[0]
33
+ similarity = np.dot(embeddings[0], embeddings[1]) / \
34
+ (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1]))
35
+
36
+ return float(similarity)
37
+
38
+
39
+ # Create Gradio interface
40
+ iface = gr.Interface(
41
+ fn=predict_similarity,
42
+ inputs=[
43
+ gr.Textbox(label="Coding Question"),
44
+ gr.Textbox(label="Candidate's Answer"),
45
+ gr.Textbox(label="AI-generated Answer")
46
+ ],
47
+ outputs=gr.Number(label="Similarity Score"),
48
+ title="AI Code Detector",
49
+ description="Detect similarity between human-written and AI-generated coding answers."
50
+ )
51
+
52
+ # Launch the app
53
+ iface.launch()
fine-tuned_all-distilroberta-v1_quantized.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:b3df341367812546bc086c5d3fa809bbde3a35dc89cc34cd5bff32dd0bd711f3
3
+ size 82520985
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ gradio==3.50.0
2
+ onnxruntime==1.19.2
3
+ torch==2.4.1
4
+ transformers==4.44.2