amiguel commited on
Commit
3e88324
·
verified ·
1 Parent(s): d64ef24

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -33
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import streamlit as st
2
- from transformers import AutoModel, AutoTokenizer
3
  from huggingface_hub import login
4
  import PyPDF2
5
  import pandas as pd
@@ -17,17 +17,7 @@ st.set_page_config(
17
  )
18
 
19
  # Model name
20
- MODEL_NAME = "deepseek-ai/DeepSeek-V3-0324"
21
-
22
- # Translation prompt template
23
- TRANSLATION_PROMPT = """
24
- You are a professional translator specializing in English-to-French translation. Translate the following text accurately and naturally into French, preserving the original meaning and tone:
25
-
26
- **Text to translate:**
27
- {input_text}
28
-
29
- **French translation:**
30
- """
31
 
32
  # Title with rocket emojis
33
  st.title("🚀 English to French Translator 🚀")
@@ -80,22 +70,19 @@ def load_model(hf_token):
80
 
81
  login(token=hf_token)
82
 
83
- # Load tokenizer with trust_remote_code=True
84
  tokenizer = AutoTokenizer.from_pretrained(
85
  MODEL_NAME,
86
- token=hf_token,
87
- trust_remote_code=True
88
  )
89
 
90
  # Load the model with appropriate dtype for CPU/GPU compatibility
91
  dtype = torch.float16 if DEVICE == "cuda" else torch.float32
92
- model = AutoModel.from_pretrained(
93
  MODEL_NAME,
94
  token=hf_token,
95
  torch_dtype=dtype,
96
- device_map="auto", # Automatically maps to CPU or GPU
97
- quantization_config=None, # Disable FP8 quantization
98
- trust_remote_code=True # Allow custom code execution
99
  )
100
 
101
  return model, tokenizer
@@ -107,11 +94,8 @@ def load_model(hf_token):
107
  # Generation function for translation
108
  def generate_translation(input_text, model, tokenizer):
109
  try:
110
- # Prepare the prompt
111
- full_prompt = TRANSLATION_PROMPT.format(input_text=input_text)
112
-
113
- # Tokenize the input
114
- inputs = tokenizer(full_prompt, return_tensors="pt", padding=True, truncation=True, max_length=512)
115
  inputs = inputs.to(DEVICE)
116
 
117
  # Generate translation
@@ -120,19 +104,14 @@ def generate_translation(input_text, model, tokenizer):
120
  outputs = model.generate(
121
  input_ids=inputs["input_ids"],
122
  attention_mask=inputs["attention_mask"],
123
- max_new_tokens=512,
124
- temperature=0.7,
125
- top_p=0.9,
126
- repetition_penalty=1.1,
127
- do_sample=True,
128
- num_return_sequences=1
129
  )
130
 
131
  # Decode the output
132
  translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
133
-
134
- # Extract the French translation part (after the prompt)
135
- translation = translation.split("**French translation:**")[-1].strip()
136
  return translation
137
 
138
  except Exception as e:
 
1
  import streamlit as st
2
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
3
  from huggingface_hub import login
4
  import PyPDF2
5
  import pandas as pd
 
17
  )
18
 
19
  # Model name
20
+ MODEL_NAME = "Helsinki-NLP/opus-mt-en-fr"
 
 
 
 
 
 
 
 
 
 
21
 
22
  # Title with rocket emojis
23
  st.title("🚀 English to French Translator 🚀")
 
70
 
71
  login(token=hf_token)
72
 
73
+ # Load tokenizer
74
  tokenizer = AutoTokenizer.from_pretrained(
75
  MODEL_NAME,
76
+ token=hf_token
 
77
  )
78
 
79
  # Load the model with appropriate dtype for CPU/GPU compatibility
80
  dtype = torch.float16 if DEVICE == "cuda" else torch.float32
81
+ model = AutoModelForSeq2SeqLM.from_pretrained(
82
  MODEL_NAME,
83
  token=hf_token,
84
  torch_dtype=dtype,
85
+ device_map="auto" # Automatically maps to CPU or GPU
 
 
86
  )
87
 
88
  return model, tokenizer
 
94
  # Generation function for translation
95
  def generate_translation(input_text, model, tokenizer):
96
  try:
97
+ # Tokenize the input (no prompt needed for seq2seq translation models)
98
+ inputs = tokenizer(input_text, return_tensors="pt", padding=True, truncation=True, max_length=512)
 
 
 
99
  inputs = inputs.to(DEVICE)
100
 
101
  # Generate translation
 
104
  outputs = model.generate(
105
  input_ids=inputs["input_ids"],
106
  attention_mask=inputs["attention_mask"],
107
+ max_length=512,
108
+ num_beams=5,
109
+ length_penalty=1.0,
110
+ early_stopping=True
 
 
111
  )
112
 
113
  # Decode the output
114
  translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
 
 
 
115
  return translation
116
 
117
  except Exception as e: