File size: 1,987 Bytes
acc8d06
0aa33ec
358f50f
 
 
 
 
 
 
 
 
 
 
 
 
acc8d06
358f50f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
acc8d06
6721032
0aa33ec
358f50f
 
0aa33ec
6721032
0aa33ec
 
 
358f50f
0aa33ec
6721032
acc8d06
6721032
 
 
 
 
 
 
 
 
 
 
 
 
acc8d06
 
6721032
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
import gradio as gr
import sys
import subprocess
import pkg_resources

def install_package(package):
    try:
        subprocess.check_call([sys.executable, "-m", "pip", "install", package])
        return True
    except:
        return False

# Check and install missing dependencies
required_packages = ["torch", "comet"]
missing_packages = []

for package in required_packages:
    try:
        pkg_resources.get_distribution(package)
    except pkg_resources.DistributionNotFound:
        missing_packages.append(package)

if missing_packages:
    print(f"Missing packages: {', '.join(missing_packages)}")
    for package in missing_packages:
        if install_package(package):
            print(f"Successfully installed {package}")
        else:
            print(f"Failed to install {package}")
            print(f"Please install manually: pip install {' '.join(required_packages)}")
            sys.exit(1)

# Now import torch and comet after ensuring they're installed
import torch
from comet import download_model, load_from_checkpoint

def evaluate_translation(src_text, mt_text):
    if not hasattr(evaluate_translation, "model"):
        model_path = download_model("wasanx/ComeTH")
        evaluate_translation.model = load_from_checkpoint(model_path)
    
    translations = [{"src": src_text, "mt": mt_text}]
    results = evaluate_translation.model.predict(
        translations, 
        batch_size=1, 
        gpus=0
    )
    return float(results['scores'][0])

demo = gr.Interface(
    fn=evaluate_translation,
    inputs=[
        gr.Textbox(label="English Source Text"),
        gr.Textbox(label="Thai Translation")
    ],
    outputs=gr.Number(label="Quality Score"),
    examples=[
        ["This is a test sentence.", "นี่คือประโยคทดสอบ"],
        ["The weather is nice today.", "อากาศดีมากวันนี้"]
    ],
    title="ComeTH Translator Evaluator"
)

if __name__ == "__main__":
    demo.launch()