Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pandas as pd
|
2 |
+
import gradio as gr
|
3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Load model and tokenizer
|
7 |
+
model_name = "meta-llama/Meta-Llama-3.2-3B-Instruct"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
10 |
+
model_name,
|
11 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
12 |
+
device_map="auto"
|
13 |
+
)
|
14 |
+
|
15 |
+
def generate_prompt(original, translation):
|
16 |
+
return f"### Task: Machine Translation Quality Estimation\n\nSource: {original}\nTranslation: {translation}\n\nScore (0-1):"
|
17 |
+
|
18 |
+
def predict_scores(file):
|
19 |
+
df = pd.read_csv(file.name, sep="\t")
|
20 |
+
scores = []
|
21 |
+
|
22 |
+
for _, row in df.iterrows():
|
23 |
+
prompt = generate_prompt(row["original"], row["translation"])
|
24 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
25 |
+
outputs = model.generate(**inputs, max_new_tokens=10)
|
26 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
27 |
+
|
28 |
+
# Extract float value (naive way)
|
29 |
+
score = response.split("Score")[-1].strip()
|
30 |
+
try:
|
31 |
+
score_val = float(score.split()[0])
|
32 |
+
except:
|
33 |
+
score_val = -1 # fallback
|
34 |
+
|
35 |
+
scores.append(score_val)
|
36 |
+
|
37 |
+
df["predicted_score"] = scores
|
38 |
+
return df
|
39 |
+
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=predict_scores,
|
42 |
+
inputs=gr.File(label="Upload dev.tsv"),
|
43 |
+
outputs=gr.Dataframe(label="QE Output with Predicted Score"),
|
44 |
+
title="MT QE with LLaMA-3.2-3B-Instruct"
|
45 |
+
)
|
46 |
+
|
47 |
+
iface.launch()
|