Spaces:
Paused
Paused
File size: 1,319 Bytes
41833af 1b36844 e62b4f7 41833af e62b4f7 41833af e62b4f7 41833af 1b36844 41833af 1b36844 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import gradio as gr
import json
from sentence_transformers import SentenceTransformer, util
# Load the model
model = SentenceTransformer('BAAI/bge-small-en-v1.5')
def grade_logic(student_json, correct_json):
try:
s_list = json.loads(student_json)
c_list = json.loads(correct_json)
s_embs = model.encode(s_list, normalize_embeddings=True, convert_to_tensor=True)
c_embs = model.encode(c_list, normalize_embeddings=True, convert_to_tensor=True)
scores = []
for i in range(len(s_list)):
ans = str(s_list[i]).strip()
if not ans:
scores.append(0)
continue
sim = util.cos_sim(s_embs[i], c_embs[i]).item()
if sim >= 0.80:
scores.append(100)
elif sim <= 0.40:
scores.append(0)
else:
scores.append(int(((sim - 0.40) / (0.80 - 0.40)) * 100))
return json.dumps(scores)
except Exception as e:
return json.dumps([-1, str(e)])
# Create the interface and disable the queue here instead of in launch()
demo = gr.Interface(
fn=grade_logic,
inputs=["text", "text"],
outputs="text",
api_name="grade"
)
# Launch with only valid Gradio 4 parameters
demo.launch(share=False) |