Spaces:
Runtime error
Runtime error
File size: 3,572 Bytes
986d536 06979d0 830e634 bfa9d58 32ad4f3 b73f0e7 32ad4f3 79584a9 445b1c0 bfa9d58 32ad4f3 bfa9d58 32ad4f3 9361e39 79584a9 9361e39 79584a9 32ad4f3 9361e39 82b9bad 9361e39 f7bbdf9 9361e39 f7bbdf9 9361e39 3f2f592 9361e39 f7bbdf9 3f2f592 32ad4f3 f7bbdf9 32ad4f3 79584a9 9361e39 32ad4f3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | 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
@spaces.GPU()
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)
|