Dhahlan2000 commited on
Commit
8873cd7
1 Parent(s): 686fefe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -15
app.py CHANGED
@@ -102,23 +102,37 @@ def transliterate_to_sinhala(text):
102
  latin_text = transliterate.process(source_script, target_script, text)
103
  return latin_text
104
 
105
- # Placeholder for conversation model loading and pipeline setup
106
- # pipe1 = pipeline("text-generation", model="microsoft/Phi-3-mini-4k-instruct", trust_remote_code=True)
107
-
108
- # interface = gr.Interface.load("huggingface/microsoft/Phi-3-mini-4k-instruct")
109
-
110
- # API_URL = "https://api-inference.huggingface.co/models/microsoft/Phi-3-mini-4k-instruct"
111
- # headers = {"Authorization": f"Bearer {access_token}"}
112
-
113
- ai_pipe = pipeline("text-generation", model="google/gemma-2b-it", token = access_token)
114
 
115
- # def query(payload):
116
- # response = requests.post(API_URL, headers=headers, json=payload)
117
- # return response.json()
118
 
119
- def conversation_predict(text):
120
- ai_response = ai_pipe([{"role": "user", "content": f"{text}"}])
121
- return ai_response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  def ai_predicted(user_input):
124
  user_input = translate_Singlish_to_sinhala(user_input)
 
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):
114
+ messages = [
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)