Dhahlan2000 commited on
Commit
b549a09
1 Parent(s): 609cc40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -22
app.py CHANGED
@@ -24,9 +24,6 @@ translator = pipeline('translation', model=trans_model, tokenizer=eng_trans_toke
24
  # Initialize translation pipelines
25
  pipe = pipeline("translation", model="thilina/mt5-sinhalese-english")
26
 
27
- trans_model = MBartForConditionalGeneration.from_pretrained("facebook/mbart-large-50-one-to-many-mmt")
28
- eng_trans_tokenizer = MBart50TokenizerFast.from_pretrained("facebook/mbart-large-50-one-to-many-mmt", src_lang="en_XX")
29
-
30
  sin_trans_model = AutoModelForSeq2SeqLM.from_pretrained("thilina/mt5-sinhalese-english")
31
  si_trans_tokenizer = AutoTokenizer.from_pretrained("thilina/mt5-sinhalese-english")
32
 
@@ -102,12 +99,13 @@ def transliterate_to_sinhala(text):
102
  latin_text = transliterate.process(source_script, target_script, text)
103
  return latin_text
104
 
105
- ai_model = AutoModelForCausalLM.from_pretrained(
106
- "Qwen/Qwen2-0.5B-Instruct-GPTQ-Int4",
107
- torch_dtype="auto",
108
- device_map="auto"
 
109
  )
110
- ai_tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct-GPTQ-Int4")
111
 
112
 
113
  def conversation_predict(prompt):
@@ -115,24 +113,22 @@ def conversation_predict(prompt):
115
  {"role": "system", "content": "You are a helpful assistant."},
116
  {"role": "user", "content": prompt}
117
  ]
118
- text = ai_tokenizer.apply_chat_template(
119
- messages,
120
- tokenize=False,
121
- add_generation_prompt=True
122
  )
123
- model_inputs = ai_tokenizer([text], return_tensors="pt").to(device)
124
 
125
- generated_ids = ai_model.generate(
126
- model_inputs.input_ids,
127
- max_new_tokens=512
128
- )
129
- generated_ids = [
130
- output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
131
- ]
132
 
133
- response = ai_tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
134
 
135
- return response
136
 
137
  def ai_predicted(user_input):
138
  user_input = translate_Singlish_to_sinhala(user_input)
 
24
  # Initialize translation pipelines
25
  pipe = pipeline("translation", model="thilina/mt5-sinhalese-english")
26
 
 
 
 
27
  sin_trans_model = AutoModelForSeq2SeqLM.from_pretrained("thilina/mt5-sinhalese-english")
28
  si_trans_tokenizer = AutoTokenizer.from_pretrained("thilina/mt5-sinhalese-english")
29
 
 
99
  latin_text = transliterate.process(source_script, target_script, text)
100
  return latin_text
101
 
102
+ model = AutoModelForCausalLM.from_pretrained(
103
+ "microsoft/Phi-3-mini-4k-instruct",
104
+ device_map="cuda",
105
+ torch_dtype="auto",
106
+ trust_remote_code=True,
107
  )
108
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
109
 
110
 
111
  def conversation_predict(prompt):
 
113
  {"role": "system", "content": "You are a helpful assistant."},
114
  {"role": "user", "content": prompt}
115
  ]
116
+ pipe = pipeline(
117
+ "text-generation",
118
+ model=model,
119
+ tokenizer=tokenizer,
120
  )
 
121
 
122
+ generation_args = {
123
+ "max_new_tokens": 500,
124
+ "return_full_text": False,
125
+ "temperature": 0.0,
126
+ "do_sample": False,
127
+ }
 
128
 
129
+ output = pipe(messages, **generation_args)
130
 
131
+ return output[0]['generated_text']
132
 
133
  def ai_predicted(user_input):
134
  user_input = translate_Singlish_to_sinhala(user_input)