cloudcoder99 commited on
Commit
9361e39
·
verified ·
1 Parent(s): b73f0e7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -56
app.py CHANGED
@@ -9,7 +9,6 @@ from transformers import (
9
  pipeline,
10
  )
11
 
12
-
13
  # Ensure the HF_API_TOKEN environment variable is set
14
  hf_token = os.getenv("HF_API_TOKEN")
15
  if hf_token is None:
@@ -20,73 +19,66 @@ model_name = "cloudcoder99/RoyalGPT_Instr"
20
 
21
  # Define the system message for the assistant
22
  system_message = (
23
- "আপনি একজন সহায়ক, শ্রদ্ধাশীল এবং সৎ সহকারী। নিরাপদ থাকাকালীন সর্বদা যথাসম্ভব সহায়কভাবে উত্তর দিন। "
 
24
  "আপনার উত্তরগুলিতে কোনও ক্ষতিকারক, অনৈতিক, বর্ণবাদী, যৌনতাবাদী, বিষাক্ত, বিপজ্জনক বা অবৈধ সামগ্রী অন্তর্ভুক্ত করা উচিত নয়৷ "
25
- "অনুগ্রহ করে নিশ্চিত করুন যে আপনার প্রতিক্রিয়াগুলি সামাজিকভাবে নিরপেক্ষ এবং ইতিবাচক প্রকৃতির।"
26
- "\n\nযদি কোনও প্রশ্নের কোনও অর্থ না হয় বা বাস্তবে সুসংগত না হয়, তাহলে কিছু ভুল উত্তর দেওয়ার পরিবর্তে কেন ব্যাখ্যা করুন। "
27
  "আপনি যদি একটি প্রশ্নের উত্তর না জানেন, তাহলে অনুগ্রহ করে মিথ্যা তথ্য শেয়ার করবেন না।"
28
  )
29
 
30
- # Function to load the model and tokenizer
31
- @spaces.GPU()
32
- def load_model_and_tokenizer():
33
- # Configuration for bitsandbytes
34
- use_4bit = True
35
- bnb_4bit_compute_dtype = "float16"
36
- bnb_4bit_quant_type = "nf4"
37
- use_nested_quant = False
38
-
39
- # Setup device and bitsandbytes configuration
40
- device_map = {"": 0}
41
- compute_dtype = getattr(torch, bnb_4bit_compute_dtype)
42
-
43
- bnb_config = BitsAndBytesConfig(
44
- load_in_4bit=use_4bit,
45
- bnb_4bit_quant_type=bnb_4bit_quant_type,
46
- bnb_4bit_compute_dtype=compute_dtype,
47
- bnb_4bit_use_double_quant=use_nested_quant,
48
- )
49
 
50
- # Load the model and tokenizer
51
- model = AutoModelForCausalLM.from_pretrained(
52
- model_name,
53
- quantization_config=bnb_config,
54
- token=hf_token
55
- )
56
 
57
- tokenizer = AutoTokenizer.from_pretrained(
58
- model_name,
59
- trust_remote_code=True,
60
- token=hf_token
61
- )
62
-
63
- return model, tokenizer
64
 
65
- # Function to create the pipeline
66
- @spaces.GPU()
67
- def create_pipeline(model, tokenizer):
68
- pipe = pipeline(
69
- task="text-generation",
70
- model=model,
71
- tokenizer=tokenizer,
72
- max_length=512,
73
- temperature=0.2,
74
- top_p=0.95,
75
- repetition_penalty=1.15,
76
- do_sample=True,
77
- )
78
- return pipe
 
 
 
 
 
 
 
 
 
 
79
 
80
  # Function to generate a response from the Llama model
81
  @spaces.GPU()
82
  def get_llama_response(message: str, history: list) -> str:
83
- model, tokenizer = load_model_and_tokenizer()
84
- pipe = create_pipeline(model, tokenizer)
85
-
86
  input_text = f"<s>[INST]<<SYS>> {system_message} <</SYS>> {message} [/INST]"
87
- result = pipe(input_text, truncation=True)[0]['generated_text']
88
- generated_text = result.split('[/INST] ')[-1]
89
- return generated_text.strip()
 
 
 
90
 
91
  # Launch the Gradio interface
92
  gr.ChatInterface(get_llama_response).launch(share=True)
 
9
  pipeline,
10
  )
11
 
 
12
  # Ensure the HF_API_TOKEN environment variable is set
13
  hf_token = os.getenv("HF_API_TOKEN")
14
  if hf_token is None:
 
19
 
20
  # Define the system message for the assistant
21
  system_message = (
22
+ "আপনি একজন সহায়ক, শ্রদ্ধাশীল এবং সৎ সহকারী। "
23
+ "নিরাপদ থাকাকালীন সর্বদা যথাসম্ভব সহায়কভাবে উত্তর দিন। "
24
  "আপনার উত্তরগুলিতে কোনও ক্ষতিকারক, অনৈতিক, বর্ণবাদী, যৌনতাবাদী, বিষাক্ত, বিপজ্জনক বা অবৈধ সামগ্রী অন্তর্ভুক্ত করা উচিত নয়৷ "
25
+ "অনুগ্রহ করে নিশ্চিত করুন যে আপনার প্রতিক্রিয়াগুলি সামাজিকভাবে নিরপেক্ষ এবং ইতিবাচক প্রকৃতির। "
26
+ "যদি কোনও প্রশ্নের কোনও অর্থ না হয় বা বাস্তবে সুসংগত না হয়, তাহলে কিছু ভুল উত্তর দেওয়ার পরিবর্তে কেন ব্যাখ্যা করুন। "
27
  "আপনি যদি একটি প্রশ্নের উত্তর না জানেন, তাহলে অনুগ্রহ করে মিথ্যা তথ্য শেয়ার করবেন না।"
28
  )
29
 
30
+ # Configuration for bitsandbytes
31
+ use_4bit = True
32
+ bnb_4bit_compute_dtype = "float16"
33
+ bnb_4bit_quant_type = "nf4"
34
+ use_nested_quant = False
 
 
 
 
 
 
 
 
 
 
 
 
 
 
35
 
36
+ # Setup device and bitsandbytes configuration
37
+ device_map = {"": 0}
38
+ compute_dtype = getattr(torch, bnb_4bit_compute_dtype)
 
 
 
39
 
40
+ bnb_config = BitsAndBytesConfig(
41
+ load_in_4bit=use_4bit,
42
+ bnb_4bit_quant_type=bnb_4bit_quant_type,
43
+ bnb_4bit_compute_dtype=compute_dtype,
44
+ bnb_4bit_use_double_quant=use_nested_quant,
45
+ )
 
46
 
47
+ # Load the model and tokenizer once
48
+ model = AutoModelForCausalLM.from_pretrained(
49
+ model_name,
50
+ quantization_config=bnb_config,
51
+ token=hf_token
52
+ )
53
+
54
+ tokenizer = AutoTokenizer.from_pretrained(
55
+ model_name,
56
+ trust_remote_code=True,
57
+ token=hf_token
58
+ )
59
+
60
+ # Create the pipeline once
61
+ pipe = pipeline(
62
+ task="text-generation",
63
+ model=model,
64
+ tokenizer=tokenizer,
65
+ max_length=1024,
66
+ temperature=0.7, # Adjust temperature
67
+ top_p=0.9, # Adjust top_p
68
+ repetition_penalty=1.2, # Adjust repetition penalty
69
+ do_sample=True,
70
+ )
71
 
72
  # Function to generate a response from the Llama model
73
  @spaces.GPU()
74
  def get_llama_response(message: str, history: list) -> str:
 
 
 
75
  input_text = f"<s>[INST]<<SYS>> {system_message} <</SYS>> {message} [/INST]"
76
+ try:
77
+ result = pipe(input_text, truncation=True)[0]['generated_text']
78
+ generated_text = result.split('[/INST] ')[-1]
79
+ return generated_text.strip()
80
+ except Exception as e:
81
+ return f"Error: {str(e)}"
82
 
83
  # Launch the Gradio interface
84
  gr.ChatInterface(get_llama_response).launch(share=True)