File size: 4,460 Bytes
22c1c50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""

Benchmark Evaluation Script for Model Text Similarity

=========================================================

Compares generated results with reference texts using text embeddings.

"""

import numpy as np
import requests
import subprocess
import os
from pathlib import Path
from typing import List

MODEL_NAME = "grok-4.1-fast"
MODEL_RESULTS_PATH = "/path/to/your/model_generate_results_dir/%s/" % MODEL_NAME
TEST_DATA_PATH = "/path/to/your/DATA_PATH/test/"


class TextSimilarityCalculator:
    def __init__(self, model_name="bge-m3", ollama_host="http://localhost:11434"):
        self.model_name = model_name
        self.ollama_host = ollama_host

    def get_embedding(self, text: str) -> List[float]:
        try:
            response = requests.get(f"{self.ollama_host}/api/tags")
            if response.status_code != 200:
                return None

            payload = {"model": self.model_name, "prompt": text, "stream": False}
            response = requests.post(f"{self.ollama_host}/api/embeddings", json=payload, timeout=30)

            if response.status_code == 200:
                return response.json().get("embedding", [])
            return None
        except:
            return None

    def cosine_similarity(self, vec1: List[float], vec2: List[float]) -> float:
        if not vec1 or not vec2:
            return 0.0

        vec1, vec2 = np.array(vec1), np.array(vec2)
        norm1, norm2 = np.linalg.norm(vec1), np.linalg.norm(vec2)

        if norm1 == 0 or norm2 == 0:
            return 0.0

        return np.dot(vec1, vec2) / (norm1 * norm2)

    def calculate_similarity(self, text1: str, text2: str) -> float:
        embedding1, embedding2 = self.get_embedding(text1), self.get_embedding(text2)
        if embedding1 is None or embedding2 is None:
            return 0.0
        return float(self.cosine_similarity(embedding1, embedding2))

    def check_ollama_installation(self):
        try:
            result = subprocess.run(["ollama", "--version"], capture_output=True, text=True)
            if result.returncode == 0:
                result = subprocess.run(["ollama", "list"], capture_output=True, text=True)
                return self.model_name in result.stdout
            return False
        except:
            return False


def find_matching_txt_files(ref_dir, test_dir):
    matches = []

    ref_txt_files = list(Path(ref_dir).glob("*.txt"))

    for txt_path in Path(test_dir).rglob("*.txt"):
        txt_name = txt_path.name

        matching_ref = [ref for ref in ref_txt_files if ref.name == txt_name]

        if matching_ref:
            for ref_file in matching_ref:
                matches.append((ref_file, txt_path))

    return matches


def read_file_content(file_path):
    try:
        with open(file_path, 'r', encoding='utf-8') as f:
            return f.read().strip()
    except:
        return ""


def main():
    matches = find_matching_txt_files(MODEL_RESULTS_PATH, TEST_DATA_PATH)

    if not matches:
        print("No matching txt files found")
        return

    print(f"Found {len(matches)} matching txt file pairs")
    print("-" * 50)

    calculator = TextSimilarityCalculator()

    if not calculator.check_ollama_installation():
        print("Ollama environment check failed")
        return

    similarities = []

    for i, (ref_path, test_path) in enumerate(matches, 1):
        ref_content = read_file_content(ref_path)
        test_content = read_file_content(test_path)

        if not ref_content or not test_content:
            print(f"File {ref_path.name}: Skipped (empty content)")
            continue

        similarity = calculator.calculate_similarity(ref_content, test_content)
        similarities.append(similarity)

        print(f"Pair {i}: {ref_path.name}")
        print(f"  Reference file: {ref_path}")
        print(f"  Target file: {test_path}")
        print(f"  Similarity: {similarity:.4f}")
        print("-" * 30)

    if similarities:
        avg_similarity = np.mean(similarities)
        print("=" * 50)
        print(f"Total file pairs: {len(similarities)}")
        print(f"Average similarity: {avg_similarity:.4f}")
    else:
        print("No valid file pairs for similarity calculation")


if __name__ == "__main__":
    main()