Spaces:
Runtime error
Runtime error
Download app.py from cloudcoder99/RoyalGPT-chat: direct link, hf CLI and curl.
- Browser
- Download file 3.57 kB
-
https://huggingface.co/spaces/cloudcoder99/RoyalGPT-chat/resolve/main/app.py
- Command line
-
hf download hf://spaces/cloudcoder99/RoyalGPT-chat/app.py
-
curl -L -o app.py https://huggingface.co/spaces/cloudcoder99/RoyalGPT-chat/resolve/main/app.py
3.57 kB
| import os | |
| import spaces | |
| import torch | |
| import gradio as gr | |
| from transformers import ( | |
| AutoModelForCausalLM, | |
| AutoTokenizer, | |
| BitsAndBytesConfig, | |
| pipeline, | |
| ) | |
| # Ensure the HF_API_TOKEN environment variable is set | |
| hf_token = os.getenv("HF_API_TOKEN") | |
| if hf_token is None: | |
| raise ValueError("HF_API_TOKEN environment variable is not set.") | |
| # Define the model path | |
| model_name = "cloudcoder99/RoyalGPT_Instr" | |
| # Define the system message for the assistant | |
| system_message = ( | |
| "আপনি একজন সহায়ক, শ্রদ্ধাশীল এবং সৎ সহকারী। " | |
| "নিরাপদ থাকাকালীন সর্বদা যথাসম্ভব সহায়কভাবে উত্তর দিন। " | |
| "আপনার উত্তরগুলিতে কোনও ক্ষতিকারক, অনৈতিক, বর্ণবাদী, যৌনতাবাদী, বিষাক্ত, বিপজ্জনক বা অবৈধ সামগ্রী অন্তর্ভুক্ত করা উচিত নয়৷ " | |
| "অনুগ্রহ করে নিশ্চিত করুন যে আপনার প্রতিক্রিয়াগুলি সামাজিকভাবে নিরপেক্ষ এবং ইতিবাচক প্রকৃতির। " | |
| "যদি কোনও প্রশ্নের কোনও অর্থ না হয় বা বাস্তবে সুসংগত না হয়, তাহলে কিছু ভুল উত্তর দেওয়ার পরিবর্তে কেন ব্যাখ্যা করুন। " | |
| "আপনি যদি একটি প্রশ্নের উত্তর না জানেন, তাহলে অনুগ্রহ করে মিথ্যা তথ্য শেয়ার করবেন না।" | |
| ) | |
| # Configuration for bitsandbytes | |
| use_4bit = True | |
| bnb_4bit_compute_dtype = "float16" | |
| bnb_4bit_quant_type = "nf4" | |
| use_nested_quant = False | |
| # Setup device and bitsandbytes configuration | |
| device_map = {"": 0} | |
| compute_dtype = getattr(torch, bnb_4bit_compute_dtype) | |
| bnb_config = BitsAndBytesConfig( | |
| load_in_4bit=use_4bit, | |
| bnb_4bit_quant_type=bnb_4bit_quant_type, | |
| bnb_4bit_compute_dtype=compute_dtype, | |
| bnb_4bit_use_double_quant=use_nested_quant, | |
| ) | |
| # Load the model and tokenizer once | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| quantization_config=bnb_config, | |
| token=hf_token | |
| ) | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| model_name, | |
| trust_remote_code=True, | |
| token=hf_token | |
| ) | |
| # Create the pipeline once | |
| pipe = pipeline( | |
| task="text-generation", | |
| model=model, | |
| tokenizer=tokenizer, | |
| max_length=1024, | |
| temperature=0.1, # Adjust temperature | |
| top_p=0.9,# Adjust top_p | |
| repetition_penalty=1.1, # Increase repetition penalty | |
| no_repeat_ngram_size=3, # Prevent repeating n-grams of size 3 | |
| do_sample=True, # Set to False for deterministic output | |
| ) | |
| # Function to generate a response from the Llama model | |
| def get_llama_response(message: str, history: list) -> str: | |
| input_text = f"<s>[INST]<<SYS>> {system_message} <</SYS>> {message} [/INST]" | |
| try: | |
| result = pipe(input_text, truncation=True)[0]['generated_text'] | |
| generated_text = result.split('[/INST] ')[-1] | |
| return generated_text.strip() | |
| except Exception as e: | |
| return f"Error: {str(e)}" | |
| # Launch the Gradio interface | |
| gr.ChatInterface(get_llama_response).launch(share=True) | |