Spaces:
Sleeping
Sleeping
additional ai judge updated
Browse files- app.py +1 -1
- utils/helper_functions.py +21 -0
app.py
CHANGED
@@ -129,7 +129,7 @@ if prompt := st.chat_input("Tell me about YSA"):
|
|
129 |
for i in range(final_ref.shape[0]):
|
130 |
this_quest = question
|
131 |
this_content = final_ref["answers"][i]
|
132 |
-
this_score =
|
133 |
independent_ai_judge_score.append(this_score)
|
134 |
|
135 |
final_ref["ai_judge"] = independent_ai_judge_score
|
|
|
129 |
for i in range(final_ref.shape[0]):
|
130 |
this_quest = question
|
131 |
this_content = final_ref["answers"][i]
|
132 |
+
this_score = calculate_sts_openai_score(question, this_content)
|
133 |
independent_ai_judge_score.append(this_score)
|
134 |
|
135 |
final_ref["ai_judge"] = independent_ai_judge_score
|
utils/helper_functions.py
CHANGED
@@ -48,6 +48,27 @@ def call_chatgpt(prompt: str) -> str:
|
|
48 |
return ans
|
49 |
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def ai_judge(sentence1: str, sentence2: str) -> float:
|
52 |
API_URL = "https://laazu6ral9w37pfb.us-east-1.aws.endpoints.huggingface.cloud"
|
53 |
headers = {"Accept": "application/json", "Content-Type": "application/json"}
|
|
|
48 |
return ans
|
49 |
|
50 |
|
51 |
+
def openai_text_embedding(prompt: str) -> str:
|
52 |
+
return openai.Embedding.create(input=prompt, model="text-embedding-ada-002")[
|
53 |
+
"data"
|
54 |
+
][0]["embedding"]
|
55 |
+
|
56 |
+
|
57 |
+
def calculate_sts_openai_score(sentence1: str, sentence2: str) -> float:
|
58 |
+
# Compute sentence embeddings
|
59 |
+
embedding1 = openai_text_embedding(sentence1) # Flatten the embedding array
|
60 |
+
embedding2 = openai_text_embedding(sentence2) # Flatten the embedding array
|
61 |
+
|
62 |
+
# Convert to array
|
63 |
+
embedding1 = np.asarray(embedding1)
|
64 |
+
embedding2 = np.asarray(embedding2)
|
65 |
+
|
66 |
+
# Calculate cosine similarity between the embeddings
|
67 |
+
similarity_score = 1 - cosine(embedding1, embedding2)
|
68 |
+
|
69 |
+
return similarity_score
|
70 |
+
|
71 |
+
|
72 |
def ai_judge(sentence1: str, sentence2: str) -> float:
|
73 |
API_URL = "https://laazu6ral9w37pfb.us-east-1.aws.endpoints.huggingface.cloud"
|
74 |
headers = {"Accept": "application/json", "Content-Type": "application/json"}
|