Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -2,33 +2,34 @@ import streamlit as st
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
|
|
5 |
# Initialize the model and tokenizer
|
6 |
@st.cache_resource
|
7 |
-
def load_model():
|
8 |
tokenizer = AutoTokenizer.from_pretrained('tirthadagr8/Japanese_to_english_gpt2CasualLM_GemmaTokenizer')
|
9 |
model = AutoModelForCausalLM.from_pretrained('tirthadagr8/Japanese_to_english_gpt2CasualLM_GemmaTokenizer')
|
10 |
-
model.
|
11 |
return tokenizer, model
|
12 |
|
13 |
-
tokenizer, model = load_model()
|
14 |
|
15 |
# Streamlit App UI
|
16 |
st.title("Japanese to English Translation")
|
17 |
st.subheader("Using a Hugging Face GPT-2 model")
|
18 |
|
19 |
# Input text box
|
20 |
-
src_text = st.text_area("Enter Japanese text for translation:"
|
21 |
-
|
22 |
if st.button("Translate"):
|
23 |
if src_text.strip():
|
24 |
with st.spinner("Translating..."):
|
25 |
# Prepare the input for the model
|
26 |
prompt = f"Translate the following Japanese sentence to English:\n\nJapanese:{src_text}\nEnglish:"
|
27 |
-
input_ids = tokenizer.encode(prompt, return_tensors='pt').
|
28 |
-
|
29 |
# Generate translation
|
30 |
output_ids = model.generate(input_ids, max_length=128)
|
31 |
-
translation = tokenizer.batch_decode(output_ids[:, input_ids.size(-1)
|
32 |
|
33 |
st.success("Translation completed!")
|
34 |
st.write("**English Translation:**")
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
|
5 |
+
device=torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
6 |
# Initialize the model and tokenizer
|
7 |
@st.cache_resource
|
8 |
+
def load_model(device):
|
9 |
tokenizer = AutoTokenizer.from_pretrained('tirthadagr8/Japanese_to_english_gpt2CasualLM_GemmaTokenizer')
|
10 |
model = AutoModelForCausalLM.from_pretrained('tirthadagr8/Japanese_to_english_gpt2CasualLM_GemmaTokenizer')
|
11 |
+
model.to(device) # Ensure the model runs on GPU if available
|
12 |
return tokenizer, model
|
13 |
|
14 |
+
tokenizer, model = load_model(device)
|
15 |
|
16 |
# Streamlit App UI
|
17 |
st.title("Japanese to English Translation")
|
18 |
st.subheader("Using a Hugging Face GPT-2 model")
|
19 |
|
20 |
# Input text box
|
21 |
+
src_text = st.text_area("Enter Japanese text for translation:")
|
22 |
+
print(src_text)
|
23 |
if st.button("Translate"):
|
24 |
if src_text.strip():
|
25 |
with st.spinner("Translating..."):
|
26 |
# Prepare the input for the model
|
27 |
prompt = f"Translate the following Japanese sentence to English:\n\nJapanese:{src_text}\nEnglish:"
|
28 |
+
input_ids = tokenizer.encode(prompt, return_tensors='pt')[:,:-1].to(device)
|
29 |
+
print(tokenizer.batch_decode(input_ids))
|
30 |
# Generate translation
|
31 |
output_ids = model.generate(input_ids, max_length=128)
|
32 |
+
translation = tokenizer.batch_decode(output_ids[:, input_ids.size(-1):-1])[0]
|
33 |
|
34 |
st.success("Translation completed!")
|
35 |
st.write("**English Translation:**")
|