Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,51 +1,57 @@
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
-
from transformers import
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
-
# Load model
|
8 |
-
|
9 |
-
|
10 |
-
hf_token = os.environ.get("HF_TOKEN")
|
11 |
|
12 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
|
13 |
-
model =
|
14 |
-
model.to("cpu") # move model to CPU (if not using GPU Space)
|
15 |
|
16 |
-
#
|
|
|
|
|
|
|
17 |
def generate_prompt(original, translation):
|
18 |
return f"### Task: Machine Translation Quality Estimation\n\nSource: {original}\nTranslation: {translation}\n\nScore (0-1):"
|
19 |
|
20 |
-
#
|
21 |
def predict_scores(file):
|
22 |
df = pd.read_csv(file.name, sep="\t")
|
23 |
scores = []
|
24 |
|
25 |
for _, row in df.iterrows():
|
26 |
prompt = generate_prompt(row["original"], row["translation"])
|
|
|
|
|
27 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
28 |
outputs = model.generate(**inputs, max_new_tokens=10)
|
|
|
|
|
29 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
30 |
|
31 |
-
# Extract float value (
|
32 |
score = response.split("Score")[-1].strip()
|
33 |
try:
|
34 |
score_val = float(score.split()[0])
|
35 |
except:
|
36 |
-
score_val = -1 #
|
37 |
|
38 |
scores.append(score_val)
|
39 |
|
40 |
df["predicted_score"] = scores
|
41 |
return df
|
42 |
|
43 |
-
# Gradio
|
44 |
iface = gr.Interface(
|
45 |
fn=predict_scores,
|
46 |
inputs=gr.File(label="Upload dev.tsv"),
|
47 |
outputs=gr.Dataframe(label="QE Output with Predicted Score"),
|
48 |
-
title="MT QE with
|
49 |
)
|
50 |
|
|
|
51 |
iface.launch()
|
|
|
1 |
import pandas as pd
|
2 |
import gradio as gr
|
3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
4 |
import torch
|
5 |
import os
|
6 |
|
7 |
+
# Load the model (flan-t5-base) and tokenizer
|
8 |
+
model_name = "google/flan-t5-base"
|
9 |
+
hf_token = os.environ.get("HF_TOKEN") # Ensure your token is securely set as a secret
|
|
|
10 |
|
11 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_auth_token=hf_token)
|
12 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name, use_auth_token=hf_token)
|
|
|
13 |
|
14 |
+
# Move the model to CPU (or GPU if available)
|
15 |
+
model.to("cpu")
|
16 |
+
|
17 |
+
# Function to generate the prompt for MT QE
|
18 |
def generate_prompt(original, translation):
|
19 |
return f"### Task: Machine Translation Quality Estimation\n\nSource: {original}\nTranslation: {translation}\n\nScore (0-1):"
|
20 |
|
21 |
+
# Function to predict quality scores from the file
|
22 |
def predict_scores(file):
|
23 |
df = pd.read_csv(file.name, sep="\t")
|
24 |
scores = []
|
25 |
|
26 |
for _, row in df.iterrows():
|
27 |
prompt = generate_prompt(row["original"], row["translation"])
|
28 |
+
|
29 |
+
# Tokenize and generate outputs
|
30 |
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
31 |
outputs = model.generate(**inputs, max_new_tokens=10)
|
32 |
+
|
33 |
+
# Decode and extract the score from the response
|
34 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
35 |
|
36 |
+
# Extract float value (simple way to extract score from response)
|
37 |
score = response.split("Score")[-1].strip()
|
38 |
try:
|
39 |
score_val = float(score.split()[0])
|
40 |
except:
|
41 |
+
score_val = -1 # Fallback in case of error
|
42 |
|
43 |
scores.append(score_val)
|
44 |
|
45 |
df["predicted_score"] = scores
|
46 |
return df
|
47 |
|
48 |
+
# Set up the Gradio interface
|
49 |
iface = gr.Interface(
|
50 |
fn=predict_scores,
|
51 |
inputs=gr.File(label="Upload dev.tsv"),
|
52 |
outputs=gr.Dataframe(label="QE Output with Predicted Score"),
|
53 |
+
title="MT QE with Google FLAN-T5-Base",
|
54 |
)
|
55 |
|
56 |
+
# Launch the Gradio interface
|
57 |
iface.launch()
|