cloudcoder99 commited on
Commit
32ad4f3
·
verified ·
1 Parent(s): bfa9d58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -4
app.py CHANGED
@@ -1,7 +1,66 @@
 
1
  import gradio as gr
 
 
 
 
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
  import gradio as gr
3
+ from transformers import (
4
+ AutoModelForCausalLM,
5
+ AutoTokenizer,
6
+ BitsAndBytesConfig,
7
+ pipeline,
8
+ )
9
 
10
+ # Define the model path
11
+ model_name = "cloudcoder99/RoyalGPT_Instr"
12
 
13
+ # Configuration for bitsandbytes
14
+ # use_4bit = True
15
+ # bnb_4bit_compute_dtype = "float16"
16
+ # bnb_4bit_quant_type = "nf4"
17
+ # use_nested_quant = False
18
+
19
+ # Setup device and bitsandbytes configuration
20
+ device_map = {"": 0}
21
+ # compute_dtype = getattr(torch, bnb_4bit_compute_dtype)
22
+
23
+ # bnb_config = BitsAndBytesConfig(
24
+ # load_in_4bit=use_4bit,
25
+ # bnb_4bit_quant_type=bnb_4bit_quant_type,
26
+ # bnb_4bit_compute_dtype=compute_dtype,
27
+ # bnb_4bit_use_double_quant=use_nested_quant,
28
+ # )
29
+
30
+ # Load the model and tokenizer
31
+ model = AutoModelForCausalLM.from_pretrained(
32
+ model_name,
33
+ # quantization_config=bnb_config,
34
+ device_map=device_map
35
+ )
36
+ tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
37
+
38
+ # Setup the text generation pipeline
39
+ pipe = pipeline(
40
+ task="text-generation",
41
+ model=model,
42
+ tokenizer=tokenizer,
43
+ max_length=512,
44
+ temperature=0.2,
45
+ top_p=0.95,
46
+ repetition_penalty=1.15,
47
+ do_sample=True,
48
+ )
49
+
50
+ # Define the system message for the assistant
51
+ system_message = (
52
+ "আপনি একজন সহায়ক, শ্রদ্ধাশীল এবং সৎ সহকারী। নিরাপদ থাকাকালীন সর্বদা যথাসম্ভব সহায়কভাবে উত্তর দিন।
53
+ আপনার উত্তরগুলিতে কোনও ক্ষতিকারক, অনৈতিক, বর্ণবাদী, যৌনতাবাদী, বিষাক্ত, বিপজ্জনক বা অবৈধ সামগ্রী অন্তর্ভুক্ত করা উচিত নয়৷
54
+ অনুগ্রহ করে নিশ্চিত করুন যে আপনার প্রতিক্রিয়াগুলি সামাজিকভাবে নিরপেক্ষ এবং ইতিবাচক প্রকৃতির।
55
+ \n\nযদি কোনও প্রশ্নের কোনও অর্থ না হয় বা বাস্তবে সুসংগত না হয়, তাহলে কিছু ভুল উত্তর দেওয়ার পরিবর্তে কেন ব্যাখ্যা করুন।
56
+ আপনি যদি একটি প্রশ্নের উত্তর না জানেন, তাহলে অনুগ্রহ করে মিথ্যা তথ্য শেয়ার করবেন না।"
57
+ )
58
+
59
+ # Function to generate a response from the Llama model
60
+ def get_llama_response(message: str, history: list) -> str:
61
+ result = pipe(f"<s>[INST]<<SYS>> {system_message} <</SYS>> {message} [/INST]")[0]['generated_text']
62
+ generated_text = result.split('[/INST] ')[-1]
63
+ return generated_text.strip()
64
+
65
+ # Launch the Gradio interface
66
+ gr.ChatInterface(get_llama_response).launch(share=True)