Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import torch
|
| 2 |
import os
|
| 3 |
import gradio as gr
|
| 4 |
from transformers import (
|
|
@@ -15,34 +14,6 @@ if hf_token is None:
|
|
| 15 |
# Define the model path
|
| 16 |
model_name = "cloudcoder99/RoyalGPT_Instr"
|
| 17 |
|
| 18 |
-
# Setup device map
|
| 19 |
-
device_map = {"": 0}
|
| 20 |
-
|
| 21 |
-
# Load the model and tokenizer
|
| 22 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
-
model_name,
|
| 24 |
-
device_map=device_map,
|
| 25 |
-
token=hf_token
|
| 26 |
-
)
|
| 27 |
-
|
| 28 |
-
tokenizer = AutoTokenizer.from_pretrained(
|
| 29 |
-
model_name,
|
| 30 |
-
trust_remote_code=True,
|
| 31 |
-
token=hf_token
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
-
# Setup the text generation pipeline
|
| 35 |
-
pipe = pipeline(
|
| 36 |
-
task="text-generation",
|
| 37 |
-
model=model,
|
| 38 |
-
tokenizer=tokenizer,
|
| 39 |
-
max_length=512,
|
| 40 |
-
temperature=0.2,
|
| 41 |
-
top_p=0.95,
|
| 42 |
-
repetition_penalty=1.15,
|
| 43 |
-
do_sample=True,
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
# Define the system message for the assistant
|
| 47 |
system_message = (
|
| 48 |
"আপনি একজন সহায়ক, শ্রদ্ধাশীল এবং সৎ সহকারী। নিরাপদ থাকাকালীন সর্বদা যথাসম্ভব সহায়কভাবে উত্তর দিন। "
|
|
@@ -52,8 +23,44 @@ system_message = (
|
|
| 52 |
"আপনি যদি একটি প্রশ্নের উত্তর না জানেন, তাহলে অনুগ্রহ করে মিথ্যা তথ্য শেয়ার করবেন না।"
|
| 53 |
)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
# Function to generate a response from the Llama model
|
|
|
|
| 56 |
def get_llama_response(message: str, history: list) -> str:
|
|
|
|
|
|
|
|
|
|
| 57 |
input_text = f"<s>[INST]<<SYS>> {system_message} <</SYS>> {message} [/INST]"
|
| 58 |
result = pipe(input_text, truncation=True)[0]['generated_text']
|
| 59 |
generated_text = result.split('[/INST] ')[-1]
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import (
|
|
|
|
| 14 |
# Define the model path
|
| 15 |
model_name = "cloudcoder99/RoyalGPT_Instr"
|
| 16 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Define the system message for the assistant
|
| 18 |
system_message = (
|
| 19 |
"আপনি একজন সহায়ক, শ্রদ্ধাশীল এবং সৎ সহকারী। নিরাপদ থাকাকালীন সর্বদা যথাসম্ভব সহায়কভাবে উত্তর দিন। "
|
|
|
|
| 23 |
"আপনি যদি একটি প্রশ্নের উত্তর না জানেন, তাহলে অনুগ্রহ করে মিথ্যা তথ্য শেয়ার করবেন না।"
|
| 24 |
)
|
| 25 |
|
| 26 |
+
# Function to load the model and tokenizer
|
| 27 |
+
@spaces.GPU()
|
| 28 |
+
def load_model_and_tokenizer():
|
| 29 |
+
# Load the model and tokenizer
|
| 30 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 31 |
+
model_name,
|
| 32 |
+
token=hf_token
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 36 |
+
model_name,
|
| 37 |
+
trust_remote_code=True,
|
| 38 |
+
token=hf_token
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
return model, tokenizer
|
| 42 |
+
|
| 43 |
+
# Function to create the pipeline
|
| 44 |
+
@spaces.GPU()
|
| 45 |
+
def create_pipeline(model, tokenizer):
|
| 46 |
+
pipe = pipeline(
|
| 47 |
+
task="text-generation",
|
| 48 |
+
model=model,
|
| 49 |
+
tokenizer=tokenizer,
|
| 50 |
+
max_length=512,
|
| 51 |
+
temperature=0.2,
|
| 52 |
+
top_p=0.95,
|
| 53 |
+
repetition_penalty=1.15,
|
| 54 |
+
do_sample=True,
|
| 55 |
+
)
|
| 56 |
+
return pipe
|
| 57 |
+
|
| 58 |
# Function to generate a response from the Llama model
|
| 59 |
+
@spaces.GPU()
|
| 60 |
def get_llama_response(message: str, history: list) -> str:
|
| 61 |
+
model, tokenizer = load_model_and_tokenizer()
|
| 62 |
+
pipe = create_pipeline(model, tokenizer)
|
| 63 |
+
|
| 64 |
input_text = f"<s>[INST]<<SYS>> {system_message} <</SYS>> {message} [/INST]"
|
| 65 |
result = pipe(input_text, truncation=True)[0]['generated_text']
|
| 66 |
generated_text = result.split('[/INST] ')[-1]
|