Ayesh84 commited on
Commit
b0603cf
Β·
verified Β·
1 Parent(s): c6f5159

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -2,27 +2,33 @@ import gradio as gr
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
- # Load Sinhala LLM
6
  model_name = "polyglots/SinLlama_v01"
7
- tokenizer = AutoTokenizer.from_pretrained(model_name)
 
8
  model = AutoModelForCausalLM.from_pretrained(
9
  model_name,
10
  torch_dtype=torch.float16,
11
  device_map="auto"
12
  )
13
 
14
- def chat_with_sinllama(user_input, history=[]):
15
- inputs = tokenizer(user_input, return_tensors="pt").to(model.device)
16
- outputs = model.generate(**inputs, max_new_tokens=200)
 
 
 
 
 
17
  reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
18
- history.append((user_input, reply))
19
  return history, history
20
 
21
- # Gradio Chatbot UI
22
  with gr.Blocks() as demo:
23
- chatbot = gr.Chatbot()
24
- msg = gr.Textbox(label="Type your question in Sinhala")
25
- clear = gr.Button("Clear Chat")
26
 
27
  msg.submit(chat_with_sinllama, [msg, chatbot], [chatbot, chatbot])
28
  clear.click(lambda: None, None, chatbot, queue=False)
 
2
  from transformers import AutoModelForCausalLM, AutoTokenizer
3
  import torch
4
 
5
+ # Use LLaMA architecture explicitly
6
  model_name = "polyglots/SinLlama_v01"
7
+
8
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False)
9
  model = AutoModelForCausalLM.from_pretrained(
10
  model_name,
11
  torch_dtype=torch.float16,
12
  device_map="auto"
13
  )
14
 
15
+ def chat_with_sinllama(message, history=[]):
16
+ inputs = tokenizer(message, return_tensors="pt").to(model.device)
17
+ outputs = model.generate(
18
+ **inputs,
19
+ max_new_tokens=200,
20
+ temperature=0.7,
21
+ do_sample=True
22
+ )
23
  reply = tokenizer.decode(outputs[0], skip_special_tokens=True)
24
+ history.append((message, reply))
25
  return history, history
26
 
27
+ # Gradio app
28
  with gr.Blocks() as demo:
29
+ chatbot = gr.Chatbot(label="SinLlama (Sinhala Chatbot)")
30
+ msg = gr.Textbox(label="Type your message in Sinhala")
31
+ clear = gr.Button("Clear")
32
 
33
  msg.submit(chat_with_sinllama, [msg, chatbot], [chatbot, chatbot])
34
  clear.click(lambda: None, None, chatbot, queue=False)