Spaces:
Running
Running
| import gradio as gr | |
| import onnxruntime as ort | |
| from transformers import AutoTokenizer | |
| import numpy as np | |
| # Load the ONNX model | |
| onnx_model_path = "fine-tuned_all-distilroberta-v1_quantized.onnx" | |
| ort_session = ort.InferenceSession(onnx_model_path) | |
| # Load the tokenizer | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| "sentence-transformers/all-MiniLM-L6-v2") | |
| def predict_similarity(question, candidate_answer, ai_answer): | |
| # Combine question and answers | |
| candidate_combined = f"Question: {question} Answer: {candidate_answer}" | |
| ai_combined = f"Question: {question} Answer: {ai_answer}" | |
| # Tokenize inputs | |
| inputs = tokenizer([candidate_combined, ai_combined], | |
| padding=True, truncation=True, return_tensors="np") | |
| # Run inference | |
| ort_inputs = { | |
| "input_ids": inputs["input_ids"], | |
| "attention_mask": inputs["attention_mask"] | |
| } | |
| ort_outputs = ort_session.run(None, ort_inputs) | |
| # Calculate cosine similarity | |
| embeddings = ort_outputs[0] | |
| similarity = np.dot(embeddings[0], embeddings[1]) / \ | |
| (np.linalg.norm(embeddings[0]) * np.linalg.norm(embeddings[1])) | |
| return float(similarity) | |
| # Create Gradio interface | |
| iface = gr.Interface( | |
| fn=predict_similarity, | |
| inputs=[ | |
| gr.Textbox(label="Coding Question"), | |
| gr.Textbox(label="Candidate's Answer"), | |
| gr.Textbox(label="AI-generated Answer") | |
| ], | |
| outputs=gr.Number(label="Similarity Score"), | |
| title="AI Code Detector", | |
| description="Detect similarity between human-written and AI-generated coding answers." | |
| ) | |
| # Launch the app | |
| iface.launch() | |